Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4603 from piteer1/hotfix-redis-liboption

[BUG] Redis Storage won't behave correctly after libOptions were set
  • Loading branch information...
commit 076570810f77587104d20dbab066e1f494aabe69 2 parents 365846a + 73f256d
@weierophinney weierophinney authored
View
6 library/Zend/Cache/Storage/Adapter/Redis.php
@@ -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");
View
1  library/Zend/Cache/Storage/Adapter/RedisOptions.php
@@ -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;
}
View
22 library/Zend/Cache/Storage/Adapter/RedisResourceManager.php
@@ -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;
View
39 tests/ZendTest/Cache/Storage/Adapter/RedisTest.php
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.