pluggit / cache
一个库,提供对不同后端缓存的接口访问
Requires
- php: >=5.5
- psr/log: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- behat/behat: ^3.1
- phpspec/phpspec: ^5.1
- phpunit/php-code-coverage: ^2.2
- silex/silex: ~2.0
- ukko/phpredis-phpdoc: dev-master
This package is not auto-updated.
Last update: 2024-09-18 12:17:28 UTC
README
提供对大多数常用用户缓存的抽象
TLDR;
/** @var \Cmp\Cache\Cache $cache */ $cache = (new CacheBuilder) ->withLogging($psrLogger) ->withoutExceptions() ->withArrayCache() ->withRedisCacheFromParams($host, $port, $dbNumber) ->build(); // Set an item $cache->set('foo', 'bar'); // Demand an item, throws an exception if not present $bar = $cache->demand('foo'); // Get an item, if not present in any cache it will return the given default $default = $cache->get('not found', 'default'); // Delete an item $cache->delete('foo'); // Empty the cache $cache->flush(); //Create a tagged cache $taggedCache = $cache->tag('myTag'); //Set a tagged item $taggedCache->set('key','value'); //Remove all tagged items $taggedCache->flush();
缓存接口
缓存接口允许访问 10 个方法
set
setAll
has
get
getAll
demand
delete
deleteAll
flush
tag
appendList
increase
Set
使用它来存储缓存中的值。您可以通过传递大于零的 $timeToLive
参数来使项目在一段时间后过期
注意:存活时间必须是表示秒的整数
SetAll
使用它一次性存储多个值
Has
检查项目是否在缓存中且未过期
Get
尝试从缓存中获取一个项目,如果它不在那里,它将返回一个给定的默认值
GetAll
尝试从缓存中获取多个项目,如果找不到项目,则返回 null
Demand
尝试从缓存中检索一个项目,如果它不存在,则抛出 NotFoundException
Delete
从缓存中删除一个项目
Delete All
一次性删除多个项目
Flush
清空缓存
Tag
创建一个带标签的缓存实例。它像一个普通缓存一样工作,但所有条目都放入一个特殊的桶中,您可以清空。
提供的缓存后端
以下是当前的实现后端
- Redis
- 数组(内存中)
- ChainCache
注意:您可以使用 CacheFactory
通过提供的后端之一轻松构建一个缓存对象
ChainCache
链式缓存将接受一个或多个缓存,并在失败之前尝试所有缓存
提供的缓存后端装饰器
- LoggerCache
日志缓存
它允许记录装饰的缓存抛出的任何异常。您可以选择静默模式以避免从缓存中抛出异常
注意:在通过构建器构建缓存时始终使用此装饰器,因为它确保所有抛出的异常都扩展了基类 CacheException
标签
可以使用标签来对相关对象进行分组。所有三个提供后端都有此功能。要使用它,只需使用 tag() 方法请求一个新的带标签的缓存实例,然后您就可以像使用任何其他缓存后端一样使用它
// Store one photo in the cache $cache->set('photo.1', $photoOne); // Store a set of photos and tag them $cache->tag('photos')->set('photo.1', $taggedPhotoOne); $cache->tag('photos')->set('photo.2', $taggedPhotoTwo); // The photos are into different buckets $cache->has('photo.1'); // true $cache->has('photo.2') // false $cache->tag('photos')->has('photo.1'); // true $cache->tag('photos')->has('photo.2') // true // You can flush all items with the same tag at once $cache->tag('photos')->flush(); $cache->has('photo.1'); // true $cache->tag('photos')->has('photo.1'); // false
Pimple 服务提供者
该库包括一个用于 Pimple ^3.0(包含在 Silex ^2.0 中)的服务提供者,可以轻松注册缓存
默认情况下,它将在键 cache
上注册一个 ArrayCache
$pimple->register(new CacheServiceProvider()); /** @var LoggerCache $cache */ $cache = $pimple['cache']; /** @var ArrayCache $cache */ $arrayCache = $pimple['cache']->getDecoratedCache();
选项
-
cache.backends: 要使用的后端缓存数组,如果提供了多个,则将使用 ChainCache 来包装它们。每个后端选项 必须 有键 backend,可用的选项有
array
: 它将创建一个 ArrayCache(与默认相同)redis
: 如果您已经有了 redis 连接,它可以通过键 connection 传递(如果使用字符串,提供者将尝试从容器中获取服务。如果没有提供 connection,提供者将尝试从选项数组中读取构建连接)- host: 默认为 127.0.0.1
- port: 默认为 6379
- db: 默认为 0
- timeout: 默认为 0.0
string
:如果提供了字符串,提供者将尝试从容器中获取服务,该服务必须实现Cmp\Cache\Cache
接口Cmp\Cache\Cache
:如果提供了实现 Cache 接口的对象,它将被直接使用
-
cache.exceptions:一个布尔值,默认为 true。如果设置为 true,缓存将抛出异常;如果为 false,则不抛出异常。
-
cache.logging:允许记录缓存系统抛出的异常。它接受一个数组,包含以下选项
logger
:实现 Psr\LoggerInterface 的 logger 实例log_level
:用于记录异常的日志级别,默认为 'alert'。必须是 Psr\LogLevel 定义的有效级别之一
示例
// Redis from parameters $pimple->register(new PimpleCacheProvider(), ['cache.backends' => [ ['backend' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'db' => 1, 'timeout' => 2.5] ]]); // ArrayCache + Redis from existing connection $pimple->register(new PimpleCacheProvider(), ['cache.backends' => [ ['backend' => 'array'] ['backend' => 'redis', 'connection' => $redisConnection,] ]]); // ArrayCache + Redis from a service registered in the container $pimple->register(new PimpleCacheProvider(), ['cache.backends' => [ ['backend' => 'array'] ['backend' => 'redis', 'connection' => 'redis.connection'] ]]); // Chain caches with logging and muted exceptions $pimple->register(new PimpleCacheProvider(), [ 'cache.backends' => [ ['backend' => 'array'], ['backend' => 'custom_cache_service_key'], ], 'cache.exceptions' => false, 'cache.logging' => ['logger' => $psrLogger, 'log_level' => PsrLevel::CRITICAL], ]);