bentools/cache

PSR-6 and PSR-16 Cache Adapters.

1.1 2020-04-20 08:33 UTC

This package is auto-updated.

Last update: 2024-04-20 17:37:30 UTC


README

Latest Stable Version License Build Status Coverage Status Quality Score Total Downloads

bentools/cache

Usage

Cache Fallback

If calling a cache method throws an exception, it will fall back to the other cache pool.

use BenTools\Cache\Fallback\CacheFallback;
use Cache\Adapter\Memcache\MemcacheCachePool;
use Cache\Adapter\Redis\RedisCachePool;

$main = new RedisCachePool(new Redis());
$default = new MemcacheCachePool(new Memcache());
$cache = new CacheFallback($main, $default);
$cache->get('foo'); // if $main->get('foo') throws an exception, will call $default->get('foo')

You can use as many cache pools as you want, so that a failing cache falls back to the next healthy one:

use BenTools\Cache\Fallback\CacheFallback;
use Cache\Adapter\Memcache\MemcacheCachePool;
use Cache\Adapter\Redis\RedisCachePool;
use Cache\Adapter\PHPArray\ArrayCachePool;

$redis = new RedisCachePool(new Redis());
$memcache = new MemcacheCachePool(new Memcache());
$arrayCache = new ArrayCachePool();
$cache = new CacheFallback($redis, $memcache, $arrayCache);
$cache->get('foo');

Installation

composer require bentools/cache

Tests

./vendor/bin/phpunit

License

MIT.