Skip to content

Commit

Permalink
RedisEngine: Support for selecting database number
Browse files Browse the repository at this point in the history
Closes GH-2254
  • Loading branch information
rchavik committed Oct 31, 2013
1 parent aaac360 commit fb43664
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/Cake/Cache/Engine/RedisEngine.php
Expand Up @@ -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)
Expand All @@ -62,6 +63,7 @@ public function init($settings = array()) {
'engine' => 'Redis',
'prefix' => null,
'server' => '127.0.0.1',
'database' => 0,
'port' => 6379,
'password' => false,
'timeout' => 0,
Expand All @@ -84,14 +86,18 @@ protected function _connect() {
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;
}
if ($return && $this->settings['password']) {
$return = $this->_Redis->auth($this->settings['password']);
}
if ($return) {
$return = $this->_Redis->select($this->settings['database']);
}
return $return;
}

Expand Down
46 changes: 46 additions & 0 deletions lib/Cake/Test/Case/Cache/Engine/RedisEngineTest.php
Expand Up @@ -78,6 +78,7 @@ public function testSettings() {
'timeout' => 0,
'persistent' => true,
'password' => false,
'database' => 0,
);
$this->assertEquals($expecting, $settings);
}
Expand All @@ -92,6 +93,51 @@ public function testConnect() {
$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
*
Expand Down

0 comments on commit fb43664

Please sign in to comment.