Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate individual models so they can be unique on the queue #14

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Jobs/InvalidateAutoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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;

Expand All @@ -27,4 +28,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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +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 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 $model)
{
}

Expand All @@ -25,6 +26,14 @@ public function __construct(public $models)
*/
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);
}
}
28 changes: 16 additions & 12 deletions src/Store/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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
Expand Down Expand Up @@ -129,30 +129,34 @@ public function getMappingData(?string $url = null): bool

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(function (Autocache $model) {
Event::listen(function (UrlInvalidated $event) use ($model) {
if ($event->url == $model->url) {
$model->delete();
}
});
$models->each(fn ($model) => $this->invalidateModel($model));
}

$this->invalidateCacheForUrl($model->url);
public function invalidateModel(Autocache $model): void
{
Event::listen(function (UrlInvalidated $event) use ($model) {
if ($event->url == $model->url) {
$model->delete();
}
});

$this->invalidateCacheForUrl($model->url);
}

public function invalidateCacheForUrl(string $url): void
Expand Down