dc / cache
缓存接口
0.2
2014-10-27 08:45 UTC
Suggests
- dc/cache-memcache: >=0.2
This package is not auto-updated.
Last update: 2024-09-14 15:58:01 UTC
README
安装
$ composer install dc/cache
或者将其添加到composer.json
"require": { "dc/cache": "0.*", }
$ composer install
此包建议使用dc/cache-memcache
,这是目前唯一的实现。
入门
这只是一个接口。你可能需要安装dc/cache-memcahe
或根据接口编写自己的实现。
当你准备好了,你可以像这样使用接口
$cache = new \SomeImplementation(); $cache->set('foo', 'bar'); echo $cache->get('foo'); // prints bar
一个常见的模式是你想要从缓存中获取特定的值,如果它是缓存未命中,你想要生成它
echo $cache->getWithFallback('foo', function() { return 'bar'; }); // prints bar $cache->get('foo'); // now also prints bar
此语法与任何可调用项都兼容,而不仅仅是闭包。
你也可以使用返回值来确定有效性。
$foo = $cache->getWithFallback("foo", function($x) { return ["foo" => $x, "expires" => \DateInterval($x)]; }, function($result) { return $result["expires"]; });