Last active
November 14, 2016 12:23
-
-
Save JanPetr/4c6f7490f50016f7ee5f to your computer and use it in GitHub Desktop.
Nette + Redis - delete cache based on tag intersection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Kdyby\Redis\RedisClient; | |
use Kdyby\Redis\RedisStorage; | |
use Nette\Caching\Cache; | |
use Nette\Caching\IStorage; | |
use Nette\DI\Container; | |
class RedisIntersectTagInvalidator | |
{ | |
/** @var Cache */ | |
private $cache; | |
/** @var RedisStorage */ | |
private $redisStorage; | |
/** @var RedisClient */ | |
private $redisClient; | |
public function __construct(Cache $cache, IStorage $cacheStorage, Container $container) | |
{ | |
$this->cache = $cache; | |
if($cacheStorage instanceof RedisStorage) | |
{ | |
$this->redisStorage = $cacheStorage; | |
$this->redisClient = $container->getByType('Kdyby\Redis\RedisClient'); | |
} | |
} | |
public function cleanTags($tags = array()) | |
{ | |
if(!isset($this->redisStorage)) | |
{ | |
$this->cache->clean(array( | |
Cache::TAGS => $tags, | |
)); | |
return; | |
} | |
array_walk($tags, array($this, 'formatTag')); | |
$keys = call_user_func_array(array($this->redisClient, 'sInter'), $tags); | |
foreach($keys as $key) | |
{ | |
$this->redisStorage->remove($this->formatKey($key)); | |
} | |
} | |
private function formatTag(&$tag) | |
{ | |
$tag = 'Nette.Journal:'.$tag.':keys'; | |
} | |
private function formatKey($key) | |
{ | |
return str_replace('Nette.Storage:', '', $key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment