Statamic Cache Tracker keeps a record of what items (entries, assets, terms etc) are used in the output of each page, and clears the cache (full or half) for those pages when an item is saved.
Run the following command from your project root:
composer require thoughtco/statamic-cache-tracker
You can also optionally publish the config:
php artisan vendor:publish --tag=statamic-cache-tracker-config
The addon should work auto-magically in most cases. It listens for hooks in form, nav and partial tags, as well as augmentation of entries, terms, assets and globals to determine what content is being output.
This data is then added to a cache store that is used to determine what cached data should be invalidated when tracked items are saved or deleted. If you want to clear data on other events (eg clear any page builder elements on entry create), just add a listener and call invalidate on the Tracker facade (eg. Tracker::invalidate(['partial:_partials/my_pagebuilder_block']
);
The default cache is used, or if you have specified a static_cache
store this will be used instead. This data will then be cleared when your static cache is cleared.
The autocache middleware will automatically be added to your web
middleware stack. If you want to include it to other stacks simply add:
\Thoughtco\StatamicCacheTracker\Http\Middleware\CacheTracker::class
The addon comes with a Facade for interacting with the Tracker:
\Thoughtco\StatamicCacheTracker\Facades\Tracker
If you have your own custom tracking data, for example for one of your own tags, you can register them on the Facade. Please bear in mind tracking only happens while the response is generated, so where possible use augmentation hooks.
Tracker::addAdditionalTracker(function ($tracker, $next) {
// run your logic, for example in an augmentation hook
// then call:
$tracker->addContentTag('your-tag-here');
return $next($tracker);
});
class AdditionalTrackerClass {
public function __invoke($tracker, $next) {
$tracker->addContentTag('additional::tag');
}
}
Tracker::addAdditionalTracker(AdditionalTrackerClass::class);
Thoughtco\StatamicCacheTracker\Events\TrackContentTags::dispatch(['additional::tag']);
To invalidate pages containing your tracked data, use a listener or observer, and call:
$tags = ['one', 'two', 'three'];
Tracker::invalidate($tags);