lolltec / limoncello-php-component-redis-tagged-cache

基于Redis的带标签缓存实现。

dev-master 2020-10-30 11:55 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:48 UTC


README

Scrutinizer Code Quality Code Coverage Build Status License

摘要

这是为 Limoncello 框架 提供的组件,用于在 Redis 缓存中存储额外的信息(标签)。

每个方法都以事务方式工作。这些方法实现了以下功能:

  • 通过关联字符串标签和TTL(存活时间)向缓存中添加值。
  • 通过键和相关标签删除缓存值。
  • 通过标签无效化(删除)所有缓存值。

它设计得易于与处理Redis缓存的类结合使用。所有方法都可以使用 PHP 特性冲突解决 进行重命名,并使用 PHP 特性更改方法可见性 改变可见性。

集成示例

use Limoncello\RedisTaggedCache\RedisTaggedCacheTrait;
use Redis;

/** @var Redis $redis */
$redis = ...;

$cache = new class ($redis)
{
    use RedisTaggedCacheTrait;

    public function __construct(Redis $redis)
    {
        $this->setRedisInstance($redis);
    }
};

$cache->addTaggedValue('key1', 'value1', ['author:1', 'comment:2']);
$cache->addTaggedValue('key2', 'value2', ['author:1', 'comment:2']);
$cache->addTaggedValue('key3', 'value3', ['author:1', 'comment:2']);

// removes the first key-pair
$cache->removeTaggedValue('key1');

// removes 2 remaining values
$cache->invalidateTag('author:1');

更多信息.

测试

$ composer test