php - 具有多个标签的 Laravel 刷新缓存

标签 php caching redis laravel-5.2

我在 Laravel 5.2 上使用 Redis 缓存,我的 key 有 2 个标签(基本上),年份和来源。

例子:

$this->cache->tags(['online', 2016])->put("key1", $value1, 10));
$this->cache->tags(['online', 2016])->put("key2", $value2, 10));
$this->cache->tags(['online', 2017])->put("key3", $value3, 10));
$this->cache->tags(['online', 2017])->put("key4", $value4, 10));

$this->cache->tags(['database', 2016])->put("key5", $value5, 10));
$this->cache->tags(['database', 2016])->put("key6", $value6, 10));
$this->cache->tags(['database', 2017])->put("key7", $value7, 10));
$this->cache->tags(['database', 2017])->put("key8", $value8, 10));

我想刷新 2016 年和在线标签的缓存。

使用这个 $this->cache->tags(['online', 2016])->flush(); 它会用 any标签,即 online 2016(在本例中为 key1、key2、key3、key4、key5、key6)。

我想删除所有内容,包括所有标签,即两者 online2016(在此仅大小写 key1 和 key2)

最佳答案

所以这需要一些挖掘,但这是结论。

是的,这在技术上是可能的(最好的可能?)

首先,RedisTaggedCache(负责在redis中实现标记)将所有标记成员键存储在一个redis集合中。以下是如何发现它的位置以及如何获取所有 key :

function getAllTagKeys($cache,$tags) {
    $tagStore = new TagSet($cache->getStore(),$tags);
    $key = "<prefix>:{$tagStore->getNamespace()}:". RedisTaggedCache::REFERENCE_KEY_STANDARD;//use REFERENCE_KEY_FOREVER if the keys are cached forever or both one after the other to get all of them
    return collect($cache->getRedis()->smembers($key));
}

然后你可以这样做:

getAllTagKeys($this->cache, ["online"])
    ->insersect(getAllTagKeys($this->cache, ["2016"]))
    ->each(function ($v) {
         $this->cache->getRedis()->del();
     });

这看起来很糟糕。也许向 Laravel 提出功能请求更明智,因为这看起来像是他们应该公开的东西?

关于php - 具有多个标签的 Laravel 刷新缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692755/

相关文章:

php - ob_start() 未能在 laravel 3 中创建缓冲区

java - tomcat 应用程序服务器中的内存缓存?

php - WordPress 有时很慢,有时又很快

mongodb - 哪个数据库最好保存分层数据(树)

php - Jquery,在每个循环中向数组添加值

caching - 自定义 <jdoc :include type ="head"/> in joomla 3. 2 的输出

redis - ServiceStack 的 JsonSerializer 可以序列化私有(private)成员吗?

php - 提高 wp-admin 面板性能的建议

php - Laravel - 从两个数据透视表中获取计数作为一个数字

php - Laravel 4 - 验证器 - 文件大小