diff --git a/src/Storage/Adapter/Redis.php b/src/Storage/Adapter/Redis.php index 89467aca1..94b7c8c38 100644 --- a/src/Storage/Adapter/Redis.php +++ b/src/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"); diff --git a/src/Storage/Adapter/RedisOptions.php b/src/Storage/Adapter/RedisOptions.php index c95211eb2..b167dd9ba 100644 --- a/src/Storage/Adapter/RedisOptions.php +++ b/src/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; } diff --git a/src/Storage/Adapter/RedisResourceManager.php b/src/Storage/Adapter/RedisResourceManager.php index d91deba54..691f827b7 100644 --- a/src/Storage/Adapter/RedisResourceManager.php +++ b/src/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; diff --git a/test/Storage/Adapter/RedisTest.php b/test/Storage/Adapter/RedisTest.php index 2100a8656..bd3fc84eb 100644 --- a/test/Storage/Adapter/RedisTest.php +++ b/test/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()