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: 2 146
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- h4kuna/memoize: ^0.1.7
- psr/clock: ^1.0
- psr/simple-cache: ^3.0
Requires (Dev)
- beste/clock: ^3.0
- h4kuna/dir: ^0.1.7
- malkusch/lock: ^2.2
- nette/caching: ^3.2
- nette/tester: ^2.4
- orisai/clock: ^1.2
- 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
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