Statamic Multi Cacher is a caching strategy "redirector" of sorts, it can be used to provide different caching strategies based on your own logic.
An example of this could be to bypass/disable the cache for super admins and serve the half
strategy to everyone else.
Please be aware that it is not recommended to use this in production just yet.
- Statamic 3.1 or higher
You can install the package via composer:
composer require handmadeweb/statamic-multi-cacher
First add the strategy to your static_cache
config
'strategies' => [
'half' => [
'driver' => 'application',
'expiry' => null,
],
'full' => [
'driver' => 'file',
'path' => public_path('static'),
'lock_hold_length' => 0,
],
'multi' => [
'driver' => 'multi-cacher',
],
],
Then specify the name of the strategies that you want to be available to the multi-cacher
'multi' => [
'driver' => 'multi-cacher',
'strategies' => [
'half',
'full',
],
],
Then update the static_cache
strategy at the top of the configuration to:
'strategy' => 'multi',
It is important to note, that if strategies are omitted or are empty, then the multi-cacher
strategy will default to null
.
The null
strategy will always be available for selection, so you don't need to add it to your strategies section.
If you don't override the CacheSelector
which is \HandmadeWeb\StatamicMultiCacher\CacheSelector
then the first strategy will always be used, In the above example this would be half
.
Overriding can be done by extending the CacheSelector
class like so:
<?php
namespace App\Cachers;
use Illuminate\Support\Facades\Auth;
use HandmadeWeb\StatamicMultiCacher\CacheSelector;
class MyMultiCacher extends CacheSelector
{
public function selectCacher()
{
// Disable cache for super users.
if(Auth::check() && Auth::user()->isSuper()){
return $this->multiCacher()->cachers()->get('null');
}
// Cache everyone else with the half strategy.
return $this->multiCacher()->cachers()->get('half');
}
}
And then updating your static_cache
configuration to be as follows:
'multi' => [
'driver' => 'multi-cacher',
'selector' => \App\Cachers\MyMultiCacher::class,
'strategies' => [
'half',
],
],
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.