mbretter / stk-cache
实现PSR-6和PSR-16的缓存
3.1.0
2022-08-09 09:04 UTC
Requires
- php: ^8.0
- mbretter/stk-di: >=2.0.1
- psr/cache: ^3.0.0
- psr/simple-cache: ^3.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- php-mock/php-mock-phpunit: ^2.6.0
- phpstan/phpstan: ^1.2.0
- phpunit/phpunit: ^9
Suggests
- ext-apcu: *
- ext-memcached: *
README
支持memcached和APCu。
Memcached
$memcached = new Memcached(); $memcached->addServer("127.0.0.1", 11211, 50]); $pool = new Cache\Pool\Memcached($memcached); $cache = new Cache\Cache($pool);
APCu
$pool = new Cache\Pool\APCu(); $cache = new Cache\Cache($pool);
黑洞
黑洞池是一个虚拟池,不会进行任何缓存。在开发环境中,当需要禁用缓存时,可以使用它。
$pool = new Cache\Pool\Blackhole(); $cache = new Cache\Cache($pool);
内存
使用池对象的实例变量作为缓存的缓存池。
$pool = new Cache\Pool\Memory(); $cache = new Cache\Cache($pool);
其他方法
除了PSR标准外,缓存还实现了一些有用的额外方法。
getSet
如果缓存中没有找到键,getSet会调用一个闭包。这有助于构建一个无需额外检查键是否找到的线性代码库。
如果找到了键,将直接返回值,而不调用闭包。
$pool = new Cache\Pool\APCu(); $cache = new Cache\Cache($pool); $val = $cache->getSet('mykey', function() { // ... do some expensive calculations return $val; });
分组
如果您只想通过删除一个键来使一组缓存项失效,可以使用分组功能来实现。
$groupkey = 'mygroup'; $cache->setGrouped($groupkey, 'key1', $val1); $cache->setGrouped($groupkey, 'key2', $val2); $cache->setGrouped($groupkey, 'key-' . uniqid(), $val2); $cache->delete($groupkey); // invalidates key1, key2, key-xxxx $val = $cache->getGrouped($groupkey, 'key1'); // $val is null
getSetGrouped与getSet的工作方式相同,但增加了分组键。