A thin cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing.

Installs: 2 141

Dependents: 1

Stars: 18

Watchers: 7

Forks: 14

Open Issues: 1

1.2.1 2014-10-04 18:13 UTC


Latest Stable Version Build Status Code Quality Code Coverage License

APIx Cache is a generic and thin cache wrapper with a simple interface to various different caching backends and emphasising cache tagging and indexing.

Cache-tagging allows to find/update all data items with one or more given tags. Providing, for instance, a batch delete of all obsolete entries matching a speficic tag such as a version string.

  • PSR-Cache (proposed) standard is provided thru a factory wrapper class.
  • Unit tested and compliant with PSR0, PSR1 and PSR2.
  • Continuously integrated
    • with PHP 5.3, 5.4, 5.5, 5.6 and 7.0,
    • and against APC, Redis, MongoDB, Sqlite, MySQL, PgSQL and Memcached...
  • Available as a Composer and as a PEAR package.

Cache backends

Currently, the following cache store are supplied:

Feel free to comment, send pull requests and patches...

Factory usage (PSR-Cache wrapper)

  use Apix\Cache;

  $backend = new \Redis();
  // $backend = 'apc';
  // $backend = new \PDO('...');

  $pool = Cache\Factory::getPool($backend); // without tagging support
  // or $pool = Cache\Factory::getTaggablePool($backend); // with tagging!

  $item = $pool->getItem('wibble_id');

  if (!$item->exists()) {
    $data = compute_expensive_stuff();

  return $item->get();

Basic usage (APIx native)

  use Apix\Cache;

  $cache = new Cache\Apc;

  // try to retrieve 'wibble_id' from the cache
  if (!$data = $cache->load('wibble_id')) {

    // otherwise, get some data from the origin
    // example of arbitrary mixed data
    $data = array('foo' => 'bar');

    // and save it to the cache
    $cache->save($data, 'wibble_id');

You can also use the folowing in your use cases:

  // save $data to the cache as 'wobble_id',
  // tagging it along the way as 'baz' and 'flob',
  // and set the ttl to 300 seconds (5 minutes)
  $cache->save($data, 'wobble_id', array('baz', 'flob'), 300);

  // retrieve all the cache ids under the tag 'baz'
  $ids = $cache->loadTag('baz');

  // clear out all items with a 'baz' tag

  // remove the named item

  // flush out the cache (of all -your- stored items)

Advanced usage (APIx native)

Options shared by all the backends

  use Apix\Cache;

  // default options
  $options = array(
      'prefix_key'  => 'apix-cache-key:', // prefix cache keys
      'prefix_tag'  => 'apix-cache-tag:', // prefix cache tags
      'tag_enable'  => true               // wether to enable tags support

  // start APC as a local cache
  $local_cache = new Cache\Apc($options);

Redis specific

  // additional (default) options
  $options['atomicity']  = false;    // false is faster, true is guaranteed
  $options['serializer'] = 'php';    // null, php, igbinary and json

  $redis_client = new \Redis;        // instantiate phpredis*
  $distributed_cache = new Cache\Redis($redis_client, $options);

* see phpredis for instantiation usage.

MongoDB specific

  // additional (default) options
  $options['object_serializer'] = 'php';  // null, json, php, igBinary
  $options['db_name'] = 'apix';           // name of the mongo db
  $options['collection_name'] = 'cache';  // name of the mongo collection

  $mongo  = new \MongoClient;             // MongoDB native driver** instance
  $cache = new Cache\Mongo($mongo, $options);

** see MongoDB for more instantiation details.

Memcached specific

  // additional (default) options, specific to Memcached
  $options['prefix_key'] = 'key_';  // prefix cache keys
  $options['prefix_tag'] = 'tag_';  // prefix cache tags
  $options['prefix_idx'] = 'idx_';  // prefix cache indexes
  $options['prefix_nsp'] = 'nsp_';  // prefix cache namespaces
  $options['serializer'] = 'php';   // null, php, json, igbinary.

  $memcached  = new \Memcached;     // a Memcached instance
  $shared_cache = new Cache\Memcached($memcached, $options);

Options for to the PDO backends

Note if preflight is set to true (default), the required DB table(s), if missing, will be created on-the-fly. Set preflight to false in order to avoid the expensive checks...

  // additional (default) options, specific to PDO
  $options['db_table']   = 'cache';       // table to hold the cache
  $options['serializer'] = 'php';         // null, php, igbinary, json
  $options['preflight']  = true;          // wether to preflight the DB
  $options['timestamp']  = 'Y-m-d H:i:s'; // the timestamp DB format

  // start SQLITE
  $db = new \PDO('sqlite:/tmp/apix_tests.sqlite3');
  $relational_cache = new Cache\Pdo\Sqlite($db, $options);

  // start PGSQL
  $pgsql = new \PDO('pgsql:dbname=apix_tests;host=', 'postgres');
  $postgres_cache = new Cache\Pdo\Pgsql($pgsql, $options);


Install the current major version using Composer with (recommended)

$ composer require apix/cache:1.2.*

Or install the latest stable version with

$ composer require apix/cache


APIx Cache is licensed under the New BSD license -- see the LICENSE.txt for the full license details.