| sidebar_key | cache-engine | |||
|---|---|---|---|---|
| sidebar_category | infrastructure | |||
| tags |
|
PSR-6 and PSR-16 caching over memory, files, Redis or Memcached, extended with atomic increment, decrement and compare-and-swap.
PSR-16 and PSR-6 cover get, set and delete, and stop there. A counter, a rate limit or a
lock needs more: with only get() and set(), two processes read the same value and one
update is lost, and checking has() before set() lets both believe they own a key.
Cache Engine implements both standards and extends them with the operations the storage can do in one indivisible step:
$cache = new RedisCacheEngine('redis-host');
$hits = $cache->increment('hits:home', 1, 60); // atomic counter, with a TTL
$cache->decrement('stock:sku-42'); // never loses an update
if ($cache->setIfAbsent('lock:report', $token, 60)) { // exactly one caller gets true
// ... only this process runs
$cache->deleteIfEquals('lock:report', $token); // release only our own lock
}- Atomic operations beyond PSR.
increment(),decrement()andadd()(append to a list) read and modify the value in one step, on Redis, Memcached and the file system (AtomicOperationInterface). - Correct locks.
setIfAbsent(),deleteIfEquals()andexpireIfEquals()compare and write atomically on Redis and Memcached (CompareAndSwapInterface); engines that cannot guarantee it do not offer them. byjg/resilience builds its distributed mutex on them. - Change the storage, keep the code. Memory, file system, Redis, Memcached or the no-op engine behind the same interfaces.
- PSR-16 Simple Cache interface - Simple, straightforward caching API
- PSR-6 Cache Pool interface - More verbose caching with fine-grained control
- Multiple storage backends - Choose from memory, file system, Redis, Memcached and more
- Atomic operations - Support for increment, decrement and add operations in compatible engines
- Compare and swap - Conditional writes for correct distributed locking on Redis and Memcached
- Garbage collection - Automatic cleanup of expired items
- PSR-11 container support - Retrieve cache keys via dependency container
- Logging capabilities - PSR-3 compatible logging of cache operations
composer require "byjg/cache-engine"// PSR-16 Simple Cache
$cache = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
$cache->set('key', 'value', 3600); // Cache for 1 hour
$value = $cache->get('key');
// PSR-6 Cache Pool
$pool = \ByJG\Cache\Factory::createFilePool();
$item = $pool->getItem('key');
if (!$item->isHit()) {
$item->set('value');
$item->expiresAfter(3600);
$pool->save($item);
}
$value = $item->get();| Engine | Description |
|---|---|
| NoCacheEngine | No-op engine for disabling cache without code changes |
| ArrayCacheEngine | In-memory array cache (non-persistent between requests) |
| FileSystemCacheEngine | File system based caching |
| MemcachedEngine | Memcached distributed caching |
| RedisCacheEngine | Redis-based caching |
| SessionCacheEngine | PHP session-based caching |
| TmpfsCacheEngine | Tmpfs-based caching |
docker compose up -d
vendor/bin/phpunit