Skip to content

Commit

Permalink
Fire invalidation as a job so it can be queued (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Jul 24, 2024
2 parents 4f9e845 + 56862d3 commit 4fc3d2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Jobs/InvalidateTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Thoughtco\StatamicCacheTracker\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Thoughtco\StatamicCacheTracker\Facades\Tracker;

class InvalidateTags implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public function __construct(public array $tags) {}

public function handle(): void
{
Tracker::invalidate($this->tags);
}
}
3 changes: 2 additions & 1 deletion src/Listeners/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Events;
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
use Thoughtco\StatamicCacheTracker\Jobs\InvalidateTags;

class Subscriber
{
Expand Down Expand Up @@ -115,6 +116,6 @@ public function invalidateAndDeleteTerm($event)

private function invalidateContent($tags)
{
Tracker::invalidate($tags);
InvalidateTags::dispatch($tags);
}
}
24 changes: 23 additions & 1 deletion tests/Unit/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Thoughtco\StatamicCacheTracker\Events\TrackContentTags;
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
use Thoughtco\StatamicCacheTracker\Http\Middleware\CacheTracker;
use Thoughtco\StatamicCacheTracker\Jobs\InvalidateTags;
use Thoughtco\StatamicCacheTracker\Tests\TestCase;

class EventListenerTest extends TestCase
Expand All @@ -23,8 +24,29 @@ public function it_tracks_tags_from_events()
};

$middleware = new CacheTracker();
$response = $middleware->handle($request, $next);
$middleware->handle($request, $next);

$this->assertSame(['test::tag'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
}

#[Test]
public function dispatching_job_clears_tags()
{
$request = Request::create('/');

$next = function () {
TrackContentTags::dispatch(['test::tag']);

return response('');
};

$middleware = new CacheTracker();
$middleware->handle($request, $next);

$this->assertSame(['test::tag'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);

InvalidateTags::dispatch(['test::tag']);

$this->assertSame([], Tracker::all());
}
}

0 comments on commit 4fc3d2a

Please sign in to comment.