koded / cache-extended
A PSR-6 caching library with support for several caching technologies.
Requires
- php: ^8
- koded/cache-simple: 3.*
- psr/cache: ^1
Requires (Dev)
- cache/integration-tests: dev-master
- mikey179/vfsstream: ^1
- phpunit/phpunit: ^8
- predis/predis: ^1
- symfony/phpunit-bridge: ^4.4@dev
Suggests
- ext-igbinary: For Redis igbinary support
- ext-memcached: For caching in Memcached
- ext-msgpack: For de/serializing the cache data with msgpack
- ext-redis: For caching in Redis
- predis/predis: For using Redis without ext-redis extension
README
A PSR-6 caching library for PHP 7 using several caching technologies.
Requirements
- Linux machine
- PHP 8
Recommended cache technologies are
- Redis server
- Memcached
Recommended PHP modules
For developing purposes you can use
- Memory client (default)
- File client
Usage
- create an instance of
CacheItemPoolInterface
with desired caching technology - manipulate the cache items with the pool instance
$cache = CachePool::use('redis'); $item = $cache->getItem('fubar'); $item->set('some value'); $item->expiresAfter(new DateTime('3 days')); $cache->save();
The pool instance is created only once.
CachePool::use()
accepts specific parameters for the underlying caching technology.
This method uses the Koded Simple Cache package. Please see the README in that repository for the specific arguments.
You can grab the cache client if you want to use it directly
/** $var Koded\Caching\Cache $client */ $client = $cache->client();
Deferring the items
To postpone the saving of the cache items (store all items "at once"),
you can use the saveDeferred()
method. These cache items are saved when you
- execute
commit()
- when
CacheItemPoolInterface
instance is destroyed
Keep in mind that commit()
is not an atomic operation.
There is no guarantee that all items will be saved, because anything can
happen while save()
runs (network, client crash, etc).
$cache->saveDeferred($event); $cache->saveDeferred($counter); // ... do some stuff // store this now $cache->save($dependency); // ... do more stuff $cache->saveDeferred($updates); $cache->saveDeferred($extras); // Store all deferred items $cache->commit();
License
The code is distributed under the terms of The 3-Clause BSD license.