dc/cache

Cache interface

0.2 2014-10-27 08:45 UTC

Requires

None

Requires (Dev)

None

Suggests

Provides

None

Conflicts

None

Replaces

None

MIT 9199d17f86a923988ab1d73908a486e26a649699

cachememcached

This package is not auto-updated.

Last update: 2026-06-21 01:44:36 UTC


README

👁 DC\Cache - Caching interface

Installation

$ composer install dc/cache

Or add it to composer.json:

"require": {
	"dc/cache": "0.*",
}
$ composer install

This package suggests dc/cache-memcache, which is the only implementation at this point.

Getting started

This is just an interface. You probably should install dc/cache-memcahe or write your own implementation to the interface.

When you have, you can use the interface like this:

$cache = new \SomeImplementation();
$cache->set('foo', 'bar');
echo $cache->get('foo'); // prints bar

A common pattern is that you want a specific value from the cache, and if it is a cache miss, you want to produce it:

echo $cache->getWithFallback('foo', function() {
 return 'bar';
}); // prints bar
$cache->get('foo'); // now also prints bar

This syntax works with any callable, not just closures.

You can also use the return value to determine the validity.

$foo = $cache->getWithFallback("foo", 
 function($x) { return ["foo" => $x, "expires" => \DateInterval($x)]; }, 
 function($result) { return $result["expires"]; });