From 7374953f1a161597ebcbbfabe45c6bee19e94013 Mon Sep 17 00:00:00 2001 From: VBoss Date: Mon, 1 Oct 2018 14:44:41 +0200 Subject: [PATCH] Added config option for specifying cacheKey namespace --- docs/en/index.md | 2 ++ src/Kdyby/Redis/DI/RedisExtension.php | 18 +++++++++++++++--- src/Kdyby/Redis/RedisJournal.php | 18 ++++++++++++------ src/Kdyby/Redis/RedisStorage.php | 20 +++++++++++++------- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/docs/en/index.md b/docs/en/index.md index 406e278..2655548 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -27,12 +27,14 @@ extensions: By default, the extension doesn't do much, but trying to reach the database server. There are three main configuration options, that when enabled, each one replaces or configures the original service provided by Nette. +CacheKey option for specifying cache namespace. If namespace is specified RedisJournal is used instead of RedisLuaJournal. ```yml redis: journal: on storage: on session: on + cacheKey: 'staging' ``` diff --git a/src/Kdyby/Redis/DI/RedisExtension.php b/src/Kdyby/Redis/DI/RedisExtension.php index 4423bdb..5842a71 100644 --- a/src/Kdyby/Redis/DI/RedisExtension.php +++ b/src/Kdyby/Redis/DI/RedisExtension.php @@ -34,6 +34,7 @@ class RedisExtension extends Nette\DI\CompilerExtension 'journal' => FALSE, 'storage' => FALSE, 'session' => FALSE, + 'cacheKey' => NULL, 'clients' => [], ]; @@ -162,9 +163,17 @@ protected function loadJournal(array $config) $journalService = $builder->getByType(Nette\Caching\Storages\IJournal::class) ?: 'nette.cacheJournal'; $builder->removeDefinition($journalService); $builder->addDefinition($journalService)->setFactory($this->prefix('@cacheJournal')); + if ($config['cacheKey']) { + $builder->addDefinition($this->prefix('cacheJournal')) + ->setClass(Kdyby\Redis\RedisJournal::class, [ + 'cacheKey' => $config['cacheKey'], + ] + ); - $builder->addDefinition($this->prefix('cacheJournal')) - ->setClass(Kdyby\Redis\RedisLuaJournal::class); + } else { + $builder->addDefinition($this->prefix('cacheJournal')) + ->setClass(Kdyby\Redis\RedisLuaJournal::class); + } } @@ -186,7 +195,10 @@ protected function loadStorage(array $config) $builder->addDefinition($storageService)->setFactory($this->prefix('@cacheStorage')); $cacheStorage = $builder->addDefinition($this->prefix('cacheStorage')) - ->setClass(Kdyby\Redis\RedisStorage::class); + ->setClass(Kdyby\Redis\RedisStorage::class, [ + 'cacheKey' => $config['cacheKey'], + ] + ); if (!$storageConfig['locks']) { $cacheStorage->addSetup('disableLocking'); diff --git a/src/Kdyby/Redis/RedisJournal.php b/src/Kdyby/Redis/RedisJournal.php index 09ca4af..2af7d7a 100644 --- a/src/Kdyby/Redis/RedisJournal.php +++ b/src/Kdyby/Redis/RedisJournal.php @@ -32,6 +32,11 @@ class RedisJournal implements Nette\Caching\Storages\IJournal TAGS = 'tags', KEYS = 'keys'; + /** + * @var string + */ + protected $cacheKey; + /** * @var RedisClient */ @@ -39,12 +44,13 @@ class RedisJournal implements Nette\Caching\Storages\IJournal - /** - * @param RedisClient $client - */ - public function __construct(RedisClient $client) + public function __construct( + RedisClient $client, + $cacheKey = NULL + ) { $this->client = $client; + $this->cacheKey = $cacheKey ? self::NS_NETTE . '.' . $cacheKey : self::NS_NETTE; } @@ -115,7 +121,7 @@ private function cleanEntry($keys) public function clean(array $conds) { if (!empty($conds[Cache::ALL])) { - $all = $this->client->keys(self::NS_NETTE . ':*'); + $all = $this->client->keys($this->cacheKey . ':*'); $this->client->multi(); call_user_func_array([$this->client, 'del'], $all); @@ -184,7 +190,7 @@ private function tagEntries($tag) */ protected function formatKey($key, $suffix = NULL) { - return self::NS_NETTE . ':' . $key . ($suffix ? ':' . $suffix : NULL); + return $this->cacheKey . ':' . $key . ($suffix ? ':' . $suffix : NULL); } } diff --git a/src/Kdyby/Redis/RedisStorage.php b/src/Kdyby/Redis/RedisStorage.php index 9726e6a..a648411 100644 --- a/src/Kdyby/Redis/RedisStorage.php +++ b/src/Kdyby/Redis/RedisStorage.php @@ -40,6 +40,11 @@ class RedisStorage implements IMultiReadStorage /** additional cache structure */ const KEY = 'key'; + /** + * @var string + */ + protected $cacheKey; + /** * @var RedisClient */ @@ -57,14 +62,15 @@ class RedisStorage implements IMultiReadStorage - /** - * @param RedisClient $client - * @param \Nette\Caching\Storages\IJournal $journal - */ - public function __construct(RedisClient $client, IJournal $journal = NULL) + public function __construct( + RedisClient $client, + IJournal $journal = NULL, + $cacheKey = NULL + ) { $this->client = $client; $this->journal = $journal; + $this->cacheKey = $cacheKey ? self::NS_NETTE . '.' . $cacheKey : self::NS_NETTE; } @@ -276,7 +282,7 @@ public function clean(array $conds) { // cleaning using file iterator if (!empty($conds[Cache::ALL])) { - if ($keys = $this->client->send('keys', [self::NS_NETTE . ':*'])) { + if ($keys = $this->client->send('keys', [$this->cacheKey . ':*'])) { $this->client->send('del', $keys); } @@ -302,7 +308,7 @@ public function clean(array $conds) */ protected function formatEntryKey($key) { - return self::NS_NETTE . ':' . str_replace(Cache::NAMESPACE_SEPARATOR, ':', $key); + return $this->cacheKey . ':' . str_replace(\Nette\Caching\Cache::NAMESPACE_SEPARATOR, ':', $key); }