h4kuna/critical-cache

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

v0.1.5 2024-02-13 04:30 UTC

This package is auto-updated.

Last update: 2024-04-13 04:50:46 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

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;
$cacheFactory = new CriticalCache\CacheFactory('/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.

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 and it is implemented by NetteCache, this class implement PSR-16.