h4kuna / critical-cache
Only one process can to write or to delete to cache.
Fund package maintenance!
h4kuna
revolut.me/milan2m/czk1000/critical-cache
Installs: 1 084
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- h4kuna/memoize: ^0.1.3
- psr/simple-cache: ^3.0
Requires (Dev)
- h4kuna/dir: ^0.1.2
- malkusch/lock: ^2.2
- nette/caching: ^3.2
- nette/tester: ^2.4
- phpstan/phpstan: ^1.8
- phpstan/phpstan-strict-rules: ^1.4
- tracy/tracy: ^2.9
Suggests
- h4kuna/dir: malkusch/lock and nette/cache need to create directories.
- malkusch/lock: As default implementation for Lock system.
- nette/caching: As default implementation for Cache for PSR-16.
README
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.