Cachalot (cache a lot) is an easy to use caching library. It supposed to do the only one thing - return cached callback result.
Define the following requirement in your composer.json file:
"require": {
"ihor/cachalot": "2.3"
}
// With Cachalot cache you can easily cache results of different types of functions
$cache = new \Cachalot\ArrayCache();
// built-in functions
$length = $cache->getCached('strlen', ['hello world']);
// user defined functions
$unique = $cache->getCached('uniqueValues', [[1, 2, 3, 1, 2, 3]]);
// static methods
$result = $cache->getCached(['Calculator', 'subtract'], [1, 2]);
// instance methods
$square = $cache->getCached([new Calculator(), 'square'], [5]);
// anonymous functions
$reason = $cache->getCached($getErrorReason, [], \Cachalot\Cache::ONE_DAY, 'error-reason');
// callable objects
$trimed = $cache->getCached(new Trimmer(), [' hello world ']);
Returns cached $callback result
$callback
is the function (callable) which results we want to be cached
$args
are the arguments passed to the $callback
$expireIn
sets cache TTL in seconds
$suffix
is needed to avoid collisions when callback is an anonymous function
$useSuffixAsKey
when true cache suffix will be used as a cache key
$length = $cache->getCached('strlen', ['hello world']);
To have possibility to use Cachalot as a regular caching library when needed it contains classic cache methods
Returns true if cache contains entry with given key
if ($cache->contains('lastVisit')) {
echo 'This is not the first visit';
}
Returns cached value by key or false if there is no cache entry for the given key
if ($lastVisitDate = $cache->get('lastVisit')) {
echo sprintf('Last visit was at %s', $lastVisitDate);
}
Caches value by key. When $expireIn = 0
the value is cached forever
$cache->set('lastVisit', time());
Deletes cache entry by key
$cache->delete('lastVisit');
Deletes all cache entries
$cache->clear();
Stores data in APC
$cache = new Cachalot\ApcCache();
Stores data in Xcache
$cache = new Cachalot\XcacheCache();
Stores data in Memcached using Memcache PHP extension
$memcache = new \Memcache();
$memcache->connect('unix:///usr/local/var/run/memcached.sock', 0);
$cache = new \Cachalot\MemcacheCache($memcache);
Stores data in Memcached using Memcached PHP extension
$memcached = new \Memcached();
$memcached->addServer('/usr/local/var/run/memcached.sock', 0);
$cache = new \Cachalot\MemcachedCache($memcached);
Stores data in Redis
$redis = new \Redis();
$redis->connect('127.0.0.1');
$redis->select(1);
$cache = new \Cachalot\RedisCache($redis);
Stores data in Couchbase using Couchbase PHP SDK 1.x
$couchbase = new \Couchbase('127.0.0.1', '', '', 'default');
$cache = new \Cachalot\CouchbaseCache($couchbase);
Stores data in Couchbase using Couchbase PHP SDK 2.x
$cluster = new \CouchbaseCluster('couchbase://localhost');
$bucket = $cluster->openBucket('default');
$cache = new \Cachalot\Couchbase2Cache($bucket);
Stores data in PHP array
$cache = new \Cachalot\ArrayCache();
Never stores any data
$cache = new \Cachalot\BlackholeCache();