Skip to content

Commit

Permalink
Merge pull request #32 from mitydigital/feature/scheduled-cache-inval…
Browse files Browse the repository at this point in the history
…idator

Listen for ScheduledCacheInvalidated event
  • Loading branch information
martyf authored Aug 28, 2024
2 parents e26ada1 + fe393c8 commit e8843ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Commands/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function handle()

$this->info('Ah-choo... feeds for '.$cleared.' are clear.');
} else {
// Clear specific feeds caches
// Clear all feeds caches
foreach (config('feedamic.feeds', []) as $feed => $config) {
foreach ($config['routes'] as $type => $route) {
Cache::forget(config('feedamic.cache').'.'.$feed.'.'.$type);
Expand Down
25 changes: 25 additions & 0 deletions src/Listeners/ScheduledCacheInvalidated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MityDigital\Feedamic\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use MityDigital\Feedamic\Commands\ClearCacheCommand;
use Statamic\Events\EntrySaved;

class ScheduledCacheInvalidated implements ShouldQueue
{
public function handle(\MityDigital\StatamicScheduledCacheInvalidator\Events\ScheduledCacheInvalidated $event) {
$feeds = collect(config('feedamic.feeds'))
->filter(function (array $config, string $key) use ($event) {
return array_intersect(Arr::get($config, 'collections', []), $event->collections);
})
->keys();

Artisan::call(ClearCacheCommand::class, [
'feeds' => $feeds
]);
}
}
4 changes: 4 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MityDigital\Feedamic\Commands\ClearCacheCommand;
use MityDigital\Feedamic\Listeners\ClearFeedamicCache;
use MityDigital\Feedamic\Listeners\ScheduledCacheInvalidated;
use MityDigital\Feedamic\Tags\Feedamic;
use Statamic\Events\EntrySaved;
use Statamic\Providers\AddonServiceProvider;
Expand All @@ -19,6 +20,9 @@ class ServiceProvider extends AddonServiceProvider
protected $listen = [
EntrySaved::class => [
ClearFeedamicCache::class,
],
\MityDigital\StatamicScheduledCacheInvalidator\Events\ScheduledCacheInvalidated::class => [
ScheduledCacheInvalidated::class
]
];

Expand Down

0 comments on commit e8843ac

Please sign in to comment.