kuick / cache
Kuick Cache is a slim PSR-16 Simple Cache Interface implementation, supporting popular backends including Redis, ApcU and FileSystem
v1.4.0
2025-01-13 15:45 UTC
Requires
- php: >=8.2.0
- doctrine/dbal: ^3.1
- kuick/redis: ^1.0
- nyholm/dsn: ^2.0
- psr/simple-cache: ^3.0
Requires (Dev)
- kuick/qa-toolkit: ^1.0
Suggests
Provides
- psr/simple-cache-implementation: 1.0|2.0|3.0
README
PSR-16 Simple Cache implementation
Supporting popular backends such as:
- File system
- Redis
- Database (Doctrine Dbal)
- APCu
- InMemory (aka ArrayCache)
- Layered
Usage
- Direct object creation:
<?php
use Kuick\Cache\FilesystemCache;
$fileCache = new FilesystemCache('/tmp/cache');
$fileCache->set('foo', 'bar');
echo $fileCache->get('foo'); // bar
- Cache factory: Factory provides automatic cache object creation by a valid DSN
<?php
use Kuick\Cache\CacheFactory;
$cacheFactory = new CacheFactory();
$dbCache = $cacheFactory('pdo-mysql://127.0.0.1:3306/mydb'); // DbalCache instance
$apcuCache = $cacheFactory('apcu://'); // ApcuCache instance
$fileCache = $cacheFactory('file:///tmp/cache'); // FilesystemCache instance
$redisCache = $cacheFactory('redis://redis-server.com:6379/2'); // RedisCache instance
- Customizing the serializer:
Saved data can be serialized with one of available serializers: default, json, gzip and gzip-json.
With larger datasets it can be beneficial to use Gzip serializer, on the other hand Json based serializers are safer to use, as stored objects are casted to simple, JSON objects.
<?php
use Kuick\Cache\CacheFactory;
use Kuick\Cache\FilesystemCache;
use Kuick\Cache\Serializers\GzipJsonSerializer;
$fileCache = (new CacheFactory())('file:///tmp/cache?serializer=gzip-json');
// equivalent to:
$fileCache = new FilesystemCache('/tmp/cache', new GzipJsonSerializer());
- Method overview
Kuick Cache implements PSR-16 interface with no exceptions
<?php
use Kuick\Cache\InMemoryCache;
$cache = new InMemoryCache();
$cache->set('foo', 'bar', 300); // set "foo" to "bar", with 5 minutes ttl
$cache->get('foo'); // "bar"
$cache->get('inexistent, 'default') // "default" (using the default value as the key does not exist)
$cache->has('foo'); // true
$cache->delete('foo'); // remove "foo"
// set "foo" to "bar", and "bar" to "baz"
$cache->setMultiple([
'foo' => 'bar',
'bar' => 'baz',
]);
// ['foo' => 'bar', 'bar' => 'baz']
$cache->getMultiple([
'foo',
'bar',
]);
// removes "foo" and "bar"
$cache->deleteMultiple([
'foo',
'bar',
]);
// removes all the keys
$cache->clear();
- Layered cache conception
If you are using cost intensive backend like Dbal, it can be beneficial to store cache data on multiple layers. Get methods will try the fastest backend possible.
PLEASE CONSIDER!
If not all backends are distributed, data inconsistency may occur, in example: We have 2 PHP containers serving content. We have 2 layers - one APCu, another Dbal. If one container changes Dbal data the other one may serve stale cache.
<?php
use Kuick\Cache\CacheFactory;
use Kuick\Cache\InMemoryCache;
use Kuick\Cache\LayeredCache;
$layeredCache = new LayeredCache([
new InMemoryCache(), // the fastest backend
new FilesystemCache('/tmp/cache'), // medium backend
(new CacheFactory())('pdo-mysql://remote:3306/db') // slowest backend
]);
$layeredCache->get('foo');