handmadeweb/statamic-multi-cacher

dev-main 2021-07-27 22:00 UTC

This package is auto-updated.

Last update: 2024-10-28 05:31:52 UTC


README

Latest Version on Packagist Total Downloads MIT Licensed Run Tests Statamic v3.1

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.

THIS IS A BETA

Please be aware that it is not recommended to use this in production just yet.

Requirements

  • Statamic 3.1 or higher

Installation

You can install the package via composer:

composer require handmadeweb/statamic-multi-cacher

Usage

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',
    ],
],

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.