From a7c230e3b6a4b46f756593ab8db5bd6bcb5304a9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 9 Mar 2023 15:45:37 +1100 Subject: [PATCH] Redis pipelines --- app/Jobs/Ninja/AdjustEmailQuota.php | 41 ++++++++++++++++++++++++----- app/Models/Account.php | 9 ++++--- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/app/Jobs/Ninja/AdjustEmailQuota.php b/app/Jobs/Ninja/AdjustEmailQuota.php index 7e0a1cddab35..1cec63ab0d04 100644 --- a/app/Jobs/Ninja/AdjustEmailQuota.php +++ b/app/Jobs/Ninja/AdjustEmailQuota.php @@ -22,7 +22,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Cache; use Turbo124\Beacon\Facades\LightLogs; - +use Illuminate\Support\Facades\Redis; class AdjustEmailQuota implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -61,18 +61,47 @@ class AdjustEmailQuota implements ShouldQueue Account::query()->cursor()->each(function ($account) { nlog("resetting email quota for {$account->key}"); - $email_count = Cache::get($account->key); + $email_count = Cache::get("email_quota".$account->key); if ($email_count > 0) { try { - LightLogs::create(new EmailCount($email_count, $account->key))->send(); + LightLogs::create(new EmailCount($email_count, $account->key))->send(); // this runs syncronously } catch(\Exception $e) { nlog($e->getMessage()); } } - - Cache::forget($account->key); - Cache::forget("throttle_notified:{$account->key}"); }); + + /** Use redis pipelines to execute bulk deletes efficiently */ + $redis = Redis::connection('sentinel-cache'); + $prefix = config('cache.prefix'). ":email_quota*"; + + $keys = $redis->keys($prefix); + + if(is_array($keys)) + { + $redis->pipeline(function ($pipe) use ($keys) { + + foreach ($keys as $key) { + $pipe->del($key); + } + + }); + } + $keys = null; + + $prefix = config('cache.prefix'). ":throttle_notified*"; + + $keys = $redis->keys($prefix); + + if (is_array($keys)) { + $redis->pipeline(function ($pipe) use ($keys) { + foreach ($keys as $key) { + $pipe->del($key); + } + }); + } + + } } diff --git a/app/Models/Account.php b/app/Models/Account.php index d57a3225c6b3..7eb9bd228c5a 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -339,7 +339,8 @@ class Account extends BaseModel return false; } - if ($this->plan_expires && Carbon::parse($this->plan_expires)->lt(now())) { + // 09-03-2023 - winds forward expiry checks to ensure we don't cut off users prior to billing cycle being commenced + if ($this->plan_expires && Carbon::parse($this->plan_expires)->lt(now()->subHours(12))) { return false; } @@ -352,7 +353,7 @@ class Account extends BaseModel return false; } - if ($this->plan_expires && Carbon::parse($this->plan_expires)->lt(now())) { + if ($this->plan_expires && Carbon::parse($this->plan_expires)->lt(now()->subHours(12))) { return true; } @@ -526,12 +527,12 @@ class Account extends BaseModel public function emailQuotaExceeded() :bool { - if (is_null(Cache::get($this->key))) { + if (is_null(Cache::get("email_quota".$this->key))) { return false; } try { - if (Cache::get($this->key) > $this->getDailyEmailLimit()) { + if (Cache::get("email_quota".$this->key) > $this->getDailyEmailLimit()) { if (is_null(Cache::get("throttle_notified:{$this->key}"))) { App::forgetInstance('translator'); $t = app('translator');