[BUG] Redis Storage won't behave correctly after libOptions were set #4603

Closed
wants to merge 4 commits into
from
@@ -80,7 +80,6 @@ public function __construct($options = null)
*/
protected function getRedisResource()
{
-
if (!$this->initialized) {
$options = $this->getOptions();
@@ -222,9 +221,10 @@ protected function internalHasItem(& $normalizedKey)
*/
protected function internalSetItem(& $normalizedKey, & $value)
{
+ $redis = $this->getRedisResource();
+ $ttl = $this->getOptions()->getTtl();
+
try {
- $redis = $this->getRedisResource();
- $ttl = $this->getOptions()->getTtl();
if ($ttl) {
if ($this->resourceManager->getMayorVersion($this->resourceId) < 2) {
throw new Exception\UnsupportedMethodCallException("To use ttl you need version >= 2.0.0");
@@ -172,6 +172,7 @@ public function setPersistentId($persistentId)
*/
public function setLibOptions(array $libOptions)
{
+ $this->triggerOptionEvent('lib_option', $libOptions);
$this->getResourceManager()->setLibOptions($this->getResourceId(), $libOptions);
return $this;
}
@@ -237,4 +238,27 @@ public function getDatabase()
{
return $this->getResourceManager()->getDatabase($this->getResourceId());
}
+
+ /**
+ * Set resource password
+ *
+ * @param string $password Password
+ *
+ * @return RedisOptions
+ */
+ public function setPassword($password)
+ {
+ $this->getResourceManager()->setPassword($this->getResourceId(), $password);
+ return $this;
+ }
+
+ /**
+ * Get resource password
+ *
+ * @return string
+ */
+ public function getPassword()
+ {
+ return $this->getResourceManager()->getPassword($this->getResourceId());
+ }
}
@@ -66,13 +66,13 @@ public function getResource($id)
$redis = new RedisResource();
+ $resource['resource'] = $redis;
+ $this->connect($resource);
+
foreach ($resource['lib_options'] as $k => $v) {
$redis->setOption($k, $v);
}
- $resource['resource'] = $redis;
- $this->connect($resource);
-
$info = $redis->info();
$resource['version'] = $info['redis_version'];
$this->resources[$id]['resource'] = $redis;
@@ -107,6 +107,7 @@ protected function connect(array & $resource)
if (!$success) {
throw new Exception\RuntimeException('Could not estabilish connection with Redis instance');
}
+
$resource['initialized'] = true;
if ($resource['password']) {
$redis->auth($resource['password']);
@@ -256,18 +257,19 @@ public function setLibOptions($id, array $libOptions)
}
$this->normalizeLibOptions($libOptions);
-
$resource = & $this->resources[$id];
- if ($resource instanceof RedisResource) {
- if (method_exists($resource, 'setOptions')) {
- $resource->setOptions($libOptions);
+
+ $resource['lib_options'] = $libOptions;
+
+ if ($resource['resource'] instanceof RedisResource) {
+ $redis = & $resource['resource'];
+ if (method_exists($redis, 'setOptions')) {
+ $redis->setOptions($libOptions);
} else {
foreach ($libOptions as $key => $value) {
- $resource->setOption($key, $value);
+ $redis->setOption($key, $value);
}
}
- } else {
- $resource['lib_options'] = $libOptions;
}
return $this;
@@ -32,6 +32,7 @@ class AdapterPluginManager extends AbstractPluginManager
'filesystem' => 'Zend\Cache\Storage\Adapter\Filesystem',
'memcached' => 'Zend\Cache\Storage\Adapter\Memcached',
'memory' => 'Zend\Cache\Storage\Adapter\Memory',
+ 'redis' => 'Zend\Cache\Storage\Adapter\Redis',
'session' => 'Zend\Cache\Storage\Adapter\Session',
'xcache' => 'Zend\Cache\Storage\Adapter\XCache',
'wincache' => 'Zend\Cache\Storage\Adapter\WinCache',
@@ -191,6 +191,45 @@ public function testGetSetPassword()
);
}
+ public function testGetSetLibOptionsOnExistingRedisResourceInstance()
+ {
+ $options = array('serializer', RedisResource::SERIALIZER_PHP);
+ $this->_options->setLibOptions($options);
+
+ $value = array('value');
+ $key = 'key';
+ //test if it's still possible to set/get item and if lib serializer works
+ $this->_storage->setItem($key, $value);
+ $this->assertEquals($value, $this->_storage->getItem($key), 'Redis should return an array, lib options were not set correctly');
+
+
+ $options = array('serializer', RedisResource::SERIALIZER_NONE);
+ $this->_options->setLibOptions($options);
+ $this->_storage->setItem($key, $value);
+ //should not serialize array correctly
+ $this->assertFalse(is_array($this->_storage->getItem($key)), 'Redis should not serialize automatically anymore, lib options were not set correctly');
+ }
+
+ public function testGetSetLibOptionsWithCleanRedisResourceInstance()
+ {
+ $options = array('serializer', RedisResource::SERIALIZER_PHP);
+ $this->_options->setLibOptions($options);
+
+ $redis = new Cache\Storage\Adapter\Redis($this->_options);
+ $value = array('value');
+ $key = 'key';
+ //test if it's still possible to set/get item and if lib serializer works
+ $redis->setItem($key, $value);
+ $this->assertEquals($value, $redis->getItem($key), 'Redis should return an array, lib options were not set correctly');
+
+
+ $options = array('serializer', RedisResource::SERIALIZER_NONE);
+ $this->_options->setLibOptions($options);
+ $redis->setItem($key, $value);
+ //should not serialize array correctly
+ $this->assertFalse(is_array($redis->getItem($key)), 'Redis should not serialize automatically anymore, lib options were not set correctly');
+ }
+
/* RedisOptions */
public function testGetSetNamespace()
@@ -261,4 +300,11 @@ public function testOptionsGetSetDatabase()
$this->assertEquals($database, $this->_options->getDatabase(), 'Database not set correctly using RedisOptions');
}
+ public function testOptionsGetSetPassword()
+ {
+ $password = 'my-secret';
+ $this->_options->setPassword($password);
+ $this->assertEquals($password, $this->_options->getPassword(), 'Password was set incorrectly using RedisOptions');
+ }
+
}