Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config option for specifying cacheKey namespace #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```


Expand Down
18 changes: 15 additions & 3 deletions src/Kdyby/Redis/DI/RedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RedisExtension extends Nette\DI\CompilerExtension
'journal' => FALSE,
'storage' => FALSE,
'session' => FALSE,
'cacheKey' => NULL,
'clients' => [],
];

Expand Down Expand Up @@ -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);
}
}


Expand All @@ -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');
Expand Down
18 changes: 12 additions & 6 deletions src/Kdyby/Redis/RedisJournal.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ class RedisJournal implements Nette\Caching\Storages\IJournal
TAGS = 'tags',
KEYS = 'keys';

/**
* @var string
*/
protected $cacheKey;

/**
* @var RedisClient
*/
protected $client;



/**
* @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;
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

}
20 changes: 13 additions & 7 deletions src/Kdyby/Redis/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class RedisStorage implements IMultiReadStorage
/** additional cache structure */
const KEY = 'key';

/**
* @var string
*/
protected $cacheKey;

/**
* @var RedisClient
*/
Expand All @@ -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;
}


Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}


Expand Down