phoole / cache
针对 PHP 的精简且完全兼容 PSR-16 缓存库
1.1.0
2019-11-18 01:29 UTC
Requires
- php: >=7.2.0
- phoole/base: ^1.0.17
- psr/simple-cache: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8
Provides
This package is auto-updated.
Last update: 2024-09-18 17:28:44 UTC
README
针对 PHP 的精简且完全兼容 PSR-16 缓存库
安装
通过 composer
工具安装。
composer require "phoole/cache"
或者将以下行添加到您的 composer.json
文件中
{ "require": { "phoole/cache": "1.1.*" } }
功能
-
完全符合 PSR-16。
-
支持所有可序列化的 PHP 数据类型。
-
额外功能
-
暴走保护:当 任何一个 缓存对象的生存时间小于可配置的
stampedeGap
秒数(默认 60 秒)时,通过可配置的stampedePercent
(默认 5%)百分比,将被视为过期。这可能会根据您的决定触发生成新的缓存。此功能对于减少单个热点项目暴走情况非常有用。// overwrite stampede defaults $cache = new Cache($fileAdatpor, [ 'stampedeGap' => 120, // 120second 'stampedePercent' => 2 // 2% ]);
-
分布式过期:通过将
distributedPercent
(默认 5%)设置为合理的百分比,系统将存储每个缓存项,其 TTL(生存时间)略有随机波动。这将有助于避免大量项目同时过期。$cache = new Cache($fileAdaptor, [ 'distributedPercent' => 3, // 3%, default is 5% ]);
-
-
CacheAwareInterface
和CacheAwareTrait
用法
-
简单用法
use Phoole\Cache\Cache; // using default adaptor and default settings $cache = new Cache(); // get with default value 'phoole' $name = $cache->get('name', 'phoole'); // set cache $cache->set('name', 'wow');
-
指定适配器
use Phoole\Cache\Cache; use Phoole\Cache\Adaptor\FileAdaptor; // use file adaptor and specific cache directory $cache = new Cache(new FileAdaptor('/tmp/cache');
-
与 依赖注入 一起使用
use Phoole\Cache\Cache; use Phoole\Di\Container; use Phoole\Config\Config; // config cache in the container $container = new Container(new Config( 'di.service' => [ 'cache' => Cache::class ], )); // get from container $cache = $container->get('cache'); // or static FACADE way $cache = Container::cache();
测试
$ composer test
依赖
-
PHP >= 7.2.0
-
phoole/base 1.*