juhara / zzzcache
A minimalist cache system
Requires
- php: >=5.4
- psr/cache: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- php: >=5.6
- phpunit/phpunit: ^5
This package is not auto-updated.
Last update: 2024-12-13 09:42:50 UTC
README
A minimalist and simple PHP cache library implementation. Visit Documentation for more information.
Requirement
Installation
Run through composer
$ composer require juhara/zzzcache
How to use
Implement Cacheable interface
Any class that can be stored in cache manager needs to implements Juhara\ZzzCache\Contracts\Cacheable
interface, which is data()
and ttl()
method.
data()
method should return class data.ttl()
should return integer value of time to live in millisecond. This value determine how long data will be kept in cache until considered expired.
When reading data from cache, cache manager relies on cache storage interface implementation to provide proper serialization/unserialization when read or write data.
There is one Juhara\ZzzCache\Contracts\Cacheable
implementation provided, Juhara\ZzzCache\Helpers\ClosureCacheable
class, which implements data as closure.
$ttl = 60 * 60 * 1; //cache item for 1 hour
$cacheableItem = new Juhara\ZzzCache\Helpers\ClosureCacheable(function () {
return [
'dummyData' => 'dummy data'
];
}, $ttl);
When $cacheableItem->data()
is called, it calls closure function pass in constructor and return data that defined in closure.
Juhara\ZzzCache\Helpers\ClosureCacheableFactory
class implements
Juhara\ZzzCache\Contracts\CacheableFactoryInterface
and acts as factory for
Juhara\ZzzCache\Helpers\ClosureCacheable
class.
$cacheableFactory = new \Juhara\ZzzCache\Helpers\ClosureCacheableFactory();
$cacheableItem = $cacheableFactory->build($data, $ttl);
Of course, you are free to implement your own.
Initialize Cache instance
Juhara\ZzzCache\Cache
class is default Juhara\ZzzCache\Contracts\CacheInterface
implementation provided with this library.
To use it, you need to provide Juhara\ZzzCache\Contracts\CacheStorageInterface
and Juhara\ZzzCache\Contracts\ExpiryCalculatorInterface
implementation.
See Storage Implementation for available CacheStorageInterface
implementation.
There is Juhara\ZzzCache\Helpers\ExpiryCalculator
class which is default ExpiryCalculatorInterface
implementation for this library. See Example
<?php
use Juhara\ZzzCache\Cache;
use Juhara\ZzzCache\Storages\File;
use Juhara\ZzzCache\Helpers\ExpiryCalculator;
use Juhara\ZzzCache\Helpers\Md5Hash;
// create a file-based cache where all cache
// files is stored in directory name
// app/storages/cache with
// filename prefixed with string 'cache'
$cache = new Cache(
new File(
new Md5Hash(),
'app/storages/cache/',
'cache'
),
new ExpiryCalculator()
);
Storage Implementation
Caches need to be stored somewhere. ZzzCache does not implement storage interface. It delegates this to separate library to provide storage implementation, so developer can use storage implementation that suits their needs only. Currently supported implementation is file-based and Redis-based storage.
File-based Storage
To install, run composer
$ composer require juhara/zzzfile
See zzzfile.
Redis-based Storage
To install, run composer
$ composer require juhara/zzzredis
See zzzredis.
Example
Using file as cache storage with zzzfile.
<?php
use Juhara\ZzzCache\Cache;
use Juhara\ZzzCache\Storages\File;
use Juhara\ZzzCache\Helpers\ExpiryCalculator;
use Juhara\ZzzCache\Helpers\Md5Hash;
// create a file-based cache where all cache
// files is stored in directory name
// app/storages/cache with
// filename prefixed with string 'cache'
$cache = new Cache(
new File(
new Md5Hash(),
'app/storages/cache/',
'cache'
),
new ExpiryCalculator()
);
Using Redis as cache storage with zzzredis.
<?php
use Juhara\ZzzCache\Cache;
use Juhara\ZzzCache\Storages\Redis;
use Juhara\ZzzCache\Helpers\ExpiryCalculator;
// create a redis-based cache
$cache = new Cache(
new Redis(new \Predis\Client()),
new ExpiryCalculator()
);
To get data from cache if available or from slower storage.
<?php
...
try {
//try to get data from cache if available
$cachedData = $cache->get('itemNeedToBeCache');
} catch (\Juhara\ZzzCache\Exceptions\CacheNameNotFound $e) {
$acacheableItem = new \Juhara\ZzzCache\Helpers\ClosureCacheable(
function () {
//get data from slower storage
return ['dummyData'=>'dummyData'];
},
60 * 60 * 1 //cache item for 1 hour
);
$cachedData = $cache->add('itemNeedToBeCache', $acacheableItem)
->get('itemNeedToBeCache');
}
PSR-16 CacheInterface support
ZzzCache adds support to PSR-16 CacheInterface
thorough Juhara\ZzzCache\Psr\AdapterCache
class which acts as an adapter. It
implements Psr\SimpleCache\CacheInterface
. For example,
$psr16Cache = new \Juhara\ZzzCache\Psr\AdapterCache(
new \Juhara\ZzzCache\Cache(
new \Juhara\ZzzCache\Storages\Redis(new \Predis\Client()),
new \Juhara\ZzzCache\Helpers\ExpiryCalculator()
),
new \Juhara\ZzzCache\Helpers\ClosureCacheableFactory(),
60 * 60 * 1 //default time to live for 1 hour
);
Contributing
Just create PR if you want to improve it.
Thank you.