Laravel cache with more than one tag

I am using Redis cache on Laravel 5.2 and I have my keys with 2 tags (mostly), year and source.

Example:

$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));

      

I want to clear cache for tags 2016 and online.

Using this one $this->cache->tags(['online', 2016])->flush();

, it will contain all any tags, i.e. online

or 2016

(in this case key1, key2, key3, key4, key5, key6).

I want to remove everything including all tags, i.e. and online

and 2016

(in this case only key1 and key2)

+3


source to share


2 answers


So it took a bit of digging, but here's the verdict.

Yes, it is technically possible (best possible?)

First of all, RedisTaggedCache

(responsible for the tagging implementation in redis) stores all the member keys of the tag in a redis set. Here's how to find out where it is and how you can get all the keys:

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));
}

      



Then you can do:

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

      

It looks like a terrible way to do it. Perhaps it makes more sense to query Laravel for properties, since that sounds like what they should be exposing?

+1


source


//Get all cached data ... 
$redis = Cache::getRedis();
$a_keys = $redis->keys("*yourKeyGoesHere*");
foreach ($a_keys as $key){
  //Your Action on key ... (Example delete / forget)
  $redis->del($key);
 }

      



-1


source







All Articles