RedisEngine: Support for selecting database number

Closes GH-2254
This commit is contained in:
Rachman Chavik 2013-10-31 20:40:45 +07:00
parent aaac360833
commit fb436644c3
2 changed files with 53 additions and 1 deletions

View file

@ -37,6 +37,7 @@ class RedisEngine extends CacheEngine {
* Settings
*
* - server = string URL or ip to the Redis server host
* - database = integer database number to use for connection
* - port = integer port number to the Redis server (default: 6379)
* - timeout = float timeout in seconds (default: 0)
* - persistent = boolean Connects to the Redis server with a persistent connection (default: true)
@ -62,6 +63,7 @@ class RedisEngine extends CacheEngine {
'engine' => 'Redis',
'prefix' => null,
'server' => '127.0.0.1',
'database' => 0,
'port' => 6379,
'password' => false,
'timeout' => 0,
@ -84,7 +86,8 @@ class RedisEngine extends CacheEngine {
if (empty($this->settings['persistent'])) {
$return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
} else {
$return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
$persistentId = $this->settings['port'] . $this->settings['timeout'] . $this->settings['database'];
$return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout'], $persistentId);
}
} catch (RedisException $e) {
return false;
@ -92,6 +95,9 @@ class RedisEngine extends CacheEngine {
if ($return && $this->settings['password']) {
$return = $this->_Redis->auth($this->settings['password']);
}
if ($return) {
$return = $this->_Redis->select($this->settings['database']);
}
return $return;
}

View file

@ -78,6 +78,7 @@ class RedisEngineTest extends CakeTestCase {
'timeout' => 0,
'persistent' => true,
'password' => false,
'database' => 0,
);
$this->assertEquals($expecting, $settings);
}
@ -92,6 +93,51 @@ class RedisEngineTest extends CakeTestCase {
$this->assertTrue($Redis->init(Cache::settings('redis')));
}
/**
* testMultiDatabaseOperations method
*
* @return void
*/
public function testMultiDatabaseOperations() {
Cache::config('redisdb0', array(
'engine' => 'Redis',
'prefix' => 'cake2_',
'duration' => 3600,
'persistent' => false,
));
Cache::config('redisdb1', array(
'engine' => 'Redis',
'database' => 1,
'prefix' => 'cake2_',
'duration' => 3600,
'persistent' => false,
));
$result = Cache::write('save_in_0', true, 'redisdb0');
$exist = Cache::read('save_in_0', 'redisdb0');
$this->assertTrue($result);
$this->assertTrue($exist);
$result = Cache::write('save_in_1', true, 'redisdb1');
$this->assertTrue($result);
$exist = Cache::read('save_in_0', 'redisdb1');
$this->assertFalse($exist);
$exist = Cache::read('save_in_1', 'redisdb1');
$this->assertTrue($exist);
Cache::delete('save_in_0', 'redisdb0');
$exist = Cache::read('save_in_0', 'redisdb0');
$this->assertFalse($exist);
Cache::delete('save_in_1', 'redisdb1');
$exist = Cache::read('save_in_1', 'redisdb1');
$this->assertFalse($exist);
Cache::drop('redisdb0');
Cache::drop('redisdb1');
}
/**
* testReadAndWriteCache method
*