Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4603'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Storage/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function __construct($options = null)
*/
protected function getRedisResource()
{

if (!$this->initialized) {
$options = $this->getOptions();

Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions src/Storage/Adapter/RedisOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 12 additions & 10 deletions src/Storage/Adapter/RedisResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions test/Storage/Adapter/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f2b29a0

Please sign in to comment.