Skip to content

Commit ace0165

Browse files
[9.x] Invalidate REST API cache when Runway models are saved or deleted (#826)
1 parent 9514b3d commit ace0165

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/ServiceProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\LazyCollection;
1313
use Illuminate\Support\Str;
1414
use Spatie\ErrorSolutions\Contracts\SolutionProviderRepository;
15+
use Statamic\API\Cacher;
1516
use Statamic\API\Middleware\Cache;
1617
use Statamic\Console\Commands\StaticWarm;
1718
use Statamic\Exceptions\NotFoundHttpException;
@@ -276,11 +277,23 @@ protected function bootModelEventListeners(): self
276277
->each(function ($class) {
277278
Event::listen('eloquent.saved: '.$class, queueable(fn ($model) => Search::updateWithinIndexes(new Searchable($model))));
278279
Event::listen('eloquent.deleted: '.$class, queueable(fn ($model) => Search::deleteFromIndexes(new Searchable($model))));
280+
281+
Event::listen('eloquent.saved: '.$class, fn () => $this->invalidateApiCache());
282+
Event::listen('eloquent.deleted: '.$class, fn () => $this->invalidateApiCache());
279283
});
280284

281285
return $this;
282286
}
283287

288+
protected function invalidateApiCache(): void
289+
{
290+
if (! config('statamic.api.enabled') || ! config('statamic.api.cache.expiry')) {
291+
return;
292+
}
293+
294+
app(Cacher::class)->handleInvalidationEvent(new class extends \Statamic\Events\Event {});
295+
}
296+
284297
protected function bootDataRepository(): self
285298
{
286299
if (Runway::usesRouting()) {

0 commit comments

Comments
 (0)