Redis pipelines

This commit is contained in:
David Bomba 2023-03-09 15:45:37 +11:00
parent a18e55e1d1
commit a7c230e3b6
2 changed files with 40 additions and 10 deletions

View File

@ -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);
}
});
}
}
}

View File

@ -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');