From ac1e093e179a17da84f9ef938b4067feb35c6410 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 26 Aug 2024 09:07:35 +0100 Subject: [PATCH 1/3] Invalidate individual models so they can be unique on the queue --- src/Jobs/InvalidateAutoCache.php | 15 +++++--- ...Chunk.php => InvalidateAutoCacheModel.php} | 17 ++++++--- src/ServiceProvider.php | 1 - src/Store/Manager.php | 35 +++++++++++-------- 4 files changed, 43 insertions(+), 25 deletions(-) rename src/Jobs/{InvalidateAutoCacheChunk.php => InvalidateAutoCacheModel.php} (60%) diff --git a/src/Jobs/InvalidateAutoCache.php b/src/Jobs/InvalidateAutoCache.php index ac13846..6c5620b 100644 --- a/src/Jobs/InvalidateAutoCache.php +++ b/src/Jobs/InvalidateAutoCache.php @@ -3,22 +3,21 @@ namespace Tv2regionerne\StatamicCache\Jobs; use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Tv2regionerne\StatamicCache\Facades\Store; -class InvalidateAutoCache implements ShouldQueue +class InvalidateAutoCache implements ShouldBeUnique, ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. */ - public function __construct(public array $tags) - { - } + public function __construct(public array $tags) {} /** * Execute the job. @@ -27,4 +26,12 @@ public function handle(): void { Store::invalidateContent($this->tags); } + + /** + * Get the unique ID for the job. + */ + public function uniqueId(): string + { + return md5(json_encode($this->tags)); + } } diff --git a/src/Jobs/InvalidateAutoCacheChunk.php b/src/Jobs/InvalidateAutoCacheModel.php similarity index 60% rename from src/Jobs/InvalidateAutoCacheChunk.php rename to src/Jobs/InvalidateAutoCacheModel.php index d4dbeff..679a4e7 100644 --- a/src/Jobs/InvalidateAutoCacheChunk.php +++ b/src/Jobs/InvalidateAutoCacheModel.php @@ -3,28 +3,35 @@ namespace Tv2regionerne\StatamicCache\Jobs; use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Tv2regionerne\StatamicCache\Facades\Store; -class InvalidateAutoCacheChunk implements ShouldQueue +class InvalidateAutoCacheModel implements ShouldBeUnique, ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. */ - public function __construct(public $models) - { - } + public function __construct(public $models) {} /** * Execute the job. */ public function handle(): void { - Store::invalidateModels($this->models); + Store::invalidateModel($this->model); + } + + /** + * Get the unique ID for the job. + */ + public function uniqueId(): string + { + return md5($model->url); } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 535f7c1..2891dc1 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,7 +2,6 @@ namespace Tv2regionerne\StatamicCache; -use Statamic\Facades\StaticCache; use Statamic\Providers\AddonServiceProvider; use Tv2regionerne\StatamicCache\Listeners\Subscriber; diff --git a/src/Store/Manager.php b/src/Store/Manager.php index db97ad2..9dbeb0a 100644 --- a/src/Store/Manager.php +++ b/src/Store/Manager.php @@ -7,7 +7,7 @@ use Statamic\StaticCaching\Cacher; use Statamic\StaticCaching\StaticCacheManager; use Statamic\Support\Arr; -use Tv2regionerne\StatamicCache\Jobs\InvalidateAutoCacheChunk; +use Tv2regionerne\StatamicCache\Jobs\InvalidateAutoCacheModel; use Tv2regionerne\StatamicCache\Models\Autocache; class Manager @@ -118,31 +118,36 @@ public function addKeyMappingData($key): static public function invalidateContent($ids): static { - $query = Autocache::query() + Autocache::query() ->where(function ($query) use ($ids) { foreach ($ids as $index => $id) { $query->{($index == 0 ? 'where' : 'orWhere').'JsonContains'}('content', [$id]); } + }) + ->chunk(100, function ($models) { + $models->each(fn ($model) => InvalidateAutoCacheModel::dispatch($model)); }); - $query->chunk(100, function ($models) { - InvalidateAutoCacheChunk::dispatch($models); - }); return $this; } + /* @deprecated - use invalidateModel instead */ public function invalidateModels($models): void + { + $models->each(fn ($model) => $this->invalidateModel($model)); + } + + public function invalidateModel(Autocache $model): void { $cacher = app(Cacher::class); - $manager = app()->make(StaticCacheManager::class); - $cache = $manager->cacheStore(); - - $models->each(function (Autocache $model) use ($cacher, $cache) { - $parsed = parse_url($model->url); - $url = Arr::get($parsed, 'path', '/'); - $model->delete(); - $cacher->invalidateUrl($url); - $cache->forget('static-cache:responses:'.md5($model->url)); - }); + $cache = app()->make(StaticCacheManager::class)->cacheStore(); + + $parsed = parse_url($model->url); + $url = Arr::get($parsed, 'path', '/'); + + $model->delete(); + + $cacher->invalidateUrl($url); + $cache->forget('static-cache:responses:'.md5($model->url)); } } From 0c12c9068723c1612f7fc6aeb3c7b9d5e538a714 Mon Sep 17 00:00:00 2001 From: ryanmitchell Date: Mon, 26 Aug 2024 08:18:00 +0000 Subject: [PATCH 2/3] Apply pint changes --- src/Jobs/InvalidateAutoCache.php | 4 +++- src/Jobs/InvalidateAutoCacheModel.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Jobs/InvalidateAutoCache.php b/src/Jobs/InvalidateAutoCache.php index 6c5620b..0429e99 100644 --- a/src/Jobs/InvalidateAutoCache.php +++ b/src/Jobs/InvalidateAutoCache.php @@ -17,7 +17,9 @@ class InvalidateAutoCache implements ShouldBeUnique, ShouldQueue /** * Create a new job instance. */ - public function __construct(public array $tags) {} + public function __construct(public array $tags) + { + } /** * Execute the job. diff --git a/src/Jobs/InvalidateAutoCacheModel.php b/src/Jobs/InvalidateAutoCacheModel.php index 679a4e7..1dcd929 100644 --- a/src/Jobs/InvalidateAutoCacheModel.php +++ b/src/Jobs/InvalidateAutoCacheModel.php @@ -17,7 +17,9 @@ class InvalidateAutoCacheModel implements ShouldBeUnique, ShouldQueue /** * Create a new job instance. */ - public function __construct(public $models) {} + public function __construct(public $models) + { + } /** * Execute the job. From 0d43f4312dd712b9dab51a81f9c6d1974f31a1ea Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 26 Aug 2024 14:01:23 +0100 Subject: [PATCH 3/3] dope --- src/Jobs/InvalidateAutoCacheModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jobs/InvalidateAutoCacheModel.php b/src/Jobs/InvalidateAutoCacheModel.php index 1dcd929..6148f32 100644 --- a/src/Jobs/InvalidateAutoCacheModel.php +++ b/src/Jobs/InvalidateAutoCacheModel.php @@ -17,7 +17,7 @@ class InvalidateAutoCacheModel implements ShouldBeUnique, ShouldQueue /** * Create a new job instance. */ - public function __construct(public $models) + public function __construct(public $model) { }