-
Notifications
You must be signed in to change notification settings - Fork 70
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
Feature key prefixes #56
base: master
Are you sure you want to change the base?
Changes from 4 commits
28ff2c2
b7dde89
1eed9f0
27657e1
26f38c4
fabf7fa
8deb7c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,36 @@ You can disable the native driver by this option (and the emulated will take con | |
redis: | ||
session: {native: off} | ||
``` | ||
|
||
## Key prefixes | ||
|
||
When you use two instances of one application with one Redis server, it is possible, that your data can be overwritten by second application instance. | ||
|
||
To avoid this problem you can define key prefixes in configuration, for example: | ||
|
||
Instance 1 | ||
```yml | ||
redis: | ||
keyPrefix: "instance1_" | ||
host: 127.0.0.1 | ||
port: 6379 | ||
journal: on | ||
session: on | ||
storage: on | ||
debugger: off | ||
``` | ||
|
||
Instance 2 | ||
```yml | ||
redis: | ||
keyPrefix: "instance2_" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I think about it, someone might misinterpret this as adding the prefix to the client object - it would be a little more writing, but it would also make a lot more sense to me having them under the specific sections. parameters:
redis:
namespace: instance1
redis:
host: 127.0.0.1
port: 6379
journal:
namespace: %redis.namespace%
session:
namespace: %redis.namespace%
storage:
namespace: %redis.namespace%
debugger: off |
||
host: 127.0.0.1 | ||
port: 6379 | ||
journal: on | ||
session: on | ||
storage: on | ||
debugger: off | ||
``` | ||
|
||
After configration all keys will be prefixed "keyPrefix_key" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
use Nette\DI\Config; | ||
|
||
|
||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not neccesary |
||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
|
@@ -155,8 +154,13 @@ protected function loadJournal(array $config) | |
|
||
$builder = $this->getContainerBuilder(); | ||
|
||
$constructParams = array( | ||
$this->prefix('@client'), | ||
(!isset($config['keyPrefix']) ? null : $config['keyPrefix']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
isset($config['keyPrefix']) ? $config['keyPrefix']) : NULL, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the default |
||
); | ||
|
||
$builder->addDefinition($this->prefix('cacheJournal')) | ||
->setClass('Kdyby\Redis\RedisLuaJournal'); | ||
->setClass('Kdyby\Redis\RedisLuaJournal', $constructParams); | ||
|
||
// overwrite | ||
$journalService = $builder->getByType('Nette\Caching\Storages\IJournal') ?: 'nette.cacheJournal'; | ||
|
@@ -178,8 +182,14 @@ protected function loadStorage(array $config) | |
'locks' => TRUE, | ||
)); | ||
|
||
$constructParams = array( | ||
$this->prefix('@client'), | ||
$this->prefix('@cacheJournal'), | ||
(!isset($config['keyPrefix']) ? null : $config['keyPrefix']), | ||
); | ||
|
||
$cacheStorage = $builder->addDefinition($this->prefix('cacheStorage')) | ||
->setClass('Kdyby\Redis\RedisStorage'); | ||
->setClass('Kdyby\Redis\RedisStorage', $constructParams); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should upgrade your PhpStorm, the new one doesn't break the indentation in here :) |
||
|
||
if (!$storageConfig['locks']) { | ||
$cacheStorage->addSetup('disableLocking'); | ||
|
@@ -206,7 +216,7 @@ protected function loadSession(array $config) | |
'weight' => 1, | ||
'timeout' => $config['timeout'], | ||
'database' => $config['database'], | ||
'prefix' => self::DEFAULT_SESSION_PREFIX, | ||
'prefix' => (!isset($config['keyPrefix']) ? self::DEFAULT_SESSION_PREFIX : $config['keyPrefix']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here also #56 (diff) + other occurences |
||
'auth' => $config['auth'], | ||
'native' => TRUE, | ||
'lockDuration' => $config['lockDuration'], | ||
|
@@ -220,15 +230,20 @@ protected function loadSession(array $config) | |
|
||
if ($sessionConfig['native']) { | ||
$this->loadNativeSessionHandler($sessionConfig); | ||
return; | ||
} | ||
|
||
} else { | ||
$builder->addDefinition($this->prefix('sessionHandler')) | ||
->setClass('Kdyby\Redis\RedisSessionHandler', array($this->prefix('@sessionHandler_client'))); | ||
$constructParams = array( | ||
$this->prefix('@sessionHandler_client'), | ||
(!isset($config['keyPrefix']) ? null : $config['keyPrefix']), | ||
); | ||
|
||
$sessionService = $builder->getByType('Nette\Http\Session') ?: 'session'; | ||
$builder->getDefinition($sessionService) | ||
->addSetup('?->bind(?)', array($this->prefix('@sessionHandler'), '@self')); | ||
} | ||
$builder->addDefinition($this->prefix('sessionHandler')) | ||
->setClass('Kdyby\Redis\RedisSessionHandler', $constructParams); | ||
|
||
$sessionService = $builder->getByType('Nette\Http\Session') ?: 'session'; | ||
$builder->getDefinition($sessionService) | ||
->addSetup('?->bind(?)', array($this->prefix('@sessionHandler'), '@self')); | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,11 @@ class ExclusiveLock extends Nette\Object | |
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $keyPrefix = ''; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -48,13 +53,16 @@ class ExclusiveLock extends Nette\Object | |
public $acquireTimeout = FALSE; | ||
|
||
|
||
|
||
/** | ||
* @param RedisClient $redisClient | ||
* @param string|null $keyPrefix | ||
*/ | ||
public function __construct(RedisClient $redisClient) | ||
public function __construct(RedisClient $redisClient, $keyPrefix = null) | ||
{ | ||
$this->client = $redisClient; | ||
if (!empty($keyPrefix)) { | ||
$this->keyPrefix = $keyPrefix . '_'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd preffer |
||
} | ||
} | ||
|
||
|
||
|
@@ -216,7 +224,7 @@ public function getLockTimeout($key) | |
*/ | ||
protected function formatLock($key) | ||
{ | ||
return $key . ':lock'; | ||
return $this->keyPrefix . $key . ':lock'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really neccesary to prefix the lock keys? This would probably result in something like |
||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,11 @@ class RedisSessionHandler extends Nette\Object implements \SessionHandlerInterfa | |
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $keyPrefix = ''; | ||
|
||
/** | ||
* @var Nette\Http\Session | ||
*/ | ||
|
@@ -51,13 +56,16 @@ class RedisSessionHandler extends Nette\Object implements \SessionHandlerInterfa | |
private $ttl; | ||
|
||
|
||
|
||
/** | ||
* @param RedisClient $redisClient | ||
* @param string|null $keyPrefix | ||
*/ | ||
public function __construct(RedisClient $redisClient) | ||
public function __construct(RedisClient $redisClient, $keyPrefix = null) | ||
{ | ||
$this->client = $redisClient; | ||
if (!empty($keyPrefix)) { | ||
$this->keyPrefix = $keyPrefix . '_'; | ||
} | ||
} | ||
|
||
|
||
|
@@ -228,7 +236,8 @@ protected function lock($id) | |
*/ | ||
private function formatKey($id) | ||
{ | ||
return self::NS_NETTE . $id; | ||
$key = $this->keyPrefix . $id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it makes more sense to have the instance prefix at the beggining of the key to me |
||
return self::NS_NETTE . $key; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
namespace sounds better to me, doctrine cache also uses it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the
_
is added automatically in the classes. The resulting key would beinstance1__Nette:something