2017 © Pedro Peláez
 

library cachalot

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

image

ihor/cachalot

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

  • Tuesday, January 2, 2018
  • by ihor
  • Repository
  • 3 Watchers
  • 19 Stars
  • 9,841 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 7 % Grown

The README.md

Cachalot

Cachalot (cache a lot) is an easy to use caching library. It supposed to do the only one thing - return cached callback result., (*1)

Installation

Define the following requirement in your composer.json file:, (*2)

"require": {
    "ihor/cachalot": "2.3"
}

Usage

// 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 ']);

Reference

Cache API

getCached($callback, array $args = array(), $expireIn = 0, $suffix = null, $useSuffixAsKey = false)

Returns cached $callback result, (*3)

$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, (*4)

$length = $cache->getCached('strlen', ['hello world']);

To have possibility to use Cachalot as a regular caching library when needed it contains classic cache methods, (*5)

contains($key)

Returns true if cache contains entry with given key, (*6)

if ($cache->contains('lastVisit')) {
    echo 'This is not the first visit';
}
get($key)

Returns cached value by key or false if there is no cache entry for the given key, (*7)

if ($lastVisitDate = $cache->get('lastVisit')) {
    echo sprintf('Last visit was at %s', $lastVisitDate);
}
set($key, $value, $expireIn = 0)

Caches value by key. When $expireIn = 0 the value is cached forever, (*8)

$cache->set('lastVisit', time());
delete($key)

Deletes cache entry by key, (*9)

$cache->delete('lastVisit');
clear()

Deletes all cache entries, (*10)

$cache->clear();

Back-ends

Cachalot\ApcCache

Stores data in APC, (*11)

$cache = new Cachalot\ApcCache();
Cachalot\XcacheCache

Stores data in Xcache, (*12)

$cache = new Cachalot\XcacheCache();
Cachalot\MemcacheCache

Stores data in Memcached using Memcache PHP extension, (*13)

$memcache = new \Memcache();
$memcache->connect('unix:///usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcacheCache($memcache);
Cachalot\MemcachedCache

Stores data in Memcached using Memcached PHP extension, (*14)

$memcached = new \Memcached();
$memcached->addServer('/usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcachedCache($memcached);
Cachalot\RedisCache

Stores data in Redis, (*15)

$redis = new \Redis();
$redis->connect('127.0.0.1');
$redis->select(1);

$cache = new \Cachalot\RedisCache($redis);
Cachalot\CouchbaseCache

Stores data in Couchbase using Couchbase PHP SDK 1.x, (*16)

$couchbase = new \Couchbase('127.0.0.1', '', '', 'default');

$cache = new \Cachalot\CouchbaseCache($couchbase);
Cachalot\Couchbase2Cache

Stores data in Couchbase using Couchbase PHP SDK 2.x, (*17)

$cluster = new \CouchbaseCluster('couchbase://localhost');
$bucket = $cluster->openBucket('default');

$cache = new \Cachalot\Couchbase2Cache($bucket);
Cachalot\ArrayCache

Stores data in PHP array, (*18)

$cache = new \Cachalot\ArrayCache();
Cachalot\BlackholeCache

Never stores any data, (*19)

$cache = new \Cachalot\BlackholeCache();

The Versions

02/01 2018

2.4

2.4.0.0

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ihor Burlachenko

cache redis memcache apc memcached couchbase xcache array cache blackhole

29/01 2016

dev-master

9999999-dev

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ihor Burlachenko

cache redis memcache apc memcached couchbase xcache array cache blackhole

10/01 2016

2.3

2.3.0.0

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ihor Burlachenko

cache redis memcache apc memcached couchbase xcache array cache blackhole

22/05 2015

2.2

2.2.0.0

Cache a lot in a proper way (APC, XCache, Memcache, Redis, Couchbase)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Ihor Burlachenko

12/09 2014

2.1

2.1.0.0

Cache a lot in a proper way (APC, XCache, Memcache, Redis, Couchbase)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Ihor Burlachenko

28/08 2014

1.2

1.2.0.0

Cache a lot in a proper way (APC, XCache, Memcache, Redis, Couchbase)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Ihor Burlachenko