h4kuna/critical-cache

Only one process can to write or to delete to cache.

v1.0.0 2024-08-28 08:41 UTC

This package is auto-updated.

Last update: 2024-10-28 09:03:57 UTC


README

Downloads this Month Latest stable

The library extends PSR-16 about locking when write or delete to cache.

Installation to project

composer require h4kuna/critical-cache

Optional

composer require h4kuna/dir malkusch/lock nette/caching beste/clock

How to use

First time you can use prepared factory CacheFactory. The factory tell you what dependency missing. The dependency are not mandatory, because everything can be replaced by your implementation.

use h4kuna\CriticalCache\PSR16\Locking\CacheLockingFactory;
$cacheFactory = new CacheLockingFactory('/my/temp');
$cache = $cacheFactory->create();
assert($cache instanceof Psr\SimpleCache\CacheInterface);

$data = $cache->load('foo', fn() => 'done');
echo $data; // done

Method load try read from cache, if data is not null that success else create critical section by lock system (Mutex), try read from cache, because any parallel process could be faster, if is success unlock critical section and return data, else call callback for create cache, save data to cache and unlock and return data.

Pool

Support to use multi-level cache implements CacheInterface. By order Memory -> Filesystem.

use h4kuna\CriticalCache\PSR16\Locking\CacheLockingFactory;
use h4kuna\CriticalCache\PSR16\Pool\CachePoolFactory;

$cacheFactory = new CacheLockingFactory('/my/temp');
$cachePoolFactory = new CachePoolFactory($cacheFactory);

$cache = $cachePoolFactory->create(); // by default create MemoryCache and FileSystem. You can choose redis, memcache.
$cache->set('foo', 1); // write to memory and filesystem

$cache1 = $cachePoolFactory->create();

// try to load from Memory (not found), second is Filesystem (found), and save to Memory, return result. 
echo $cache1->get('foo'); // 1 

Lock

By default, is used malkusch/lock, but if you implement Lock interface you can use different library.

And by default is used FlockMutex this is reason why is need h4kuna/dir. If you use different Lock you don't need previous library.

Cache

By default, is used nette/caching with PSR16 adapter.

Clock PSR-20

internal cache system beste/clock