Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RedisEngine: Support for selecting database number

Closes GH-2254
  • Loading branch information...
commit fb436644c300702848ff7e899c08df1c170e1702 1 parent aaac360
@rchavik rchavik authored
View
8 lib/Cake/Cache/Engine/RedisEngine.php
@@ -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 @@ public function init($settings = array()) {
'engine' => 'Redis',
'prefix' => null,
'server' => '127.0.0.1',
+ 'database' => 0,
'port' => 6379,
'password' => false,
'timeout' => 0,
@@ -84,7 +86,8 @@ 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;
@@ -92,6 +95,9 @@ protected function _connect() {
if ($return && $this->settings['password']) {
$return = $this->_Redis->auth($this->settings['password']);
}
+ if ($return) {
+ $return = $this->_Redis->select($this->settings['database']);
+ }
return $return;
}
View
46 lib/Cake/Test/Case/Cache/Engine/RedisEngineTest.php
@@ -78,6 +78,7 @@ public function testSettings() {
'timeout' => 0,
'persistent' => true,
'password' => false,
+ 'database' => 0,
);
$this->assertEquals($expecting, $settings);
}
@@ -93,6 +94,51 @@ public function testConnect() {
}
/**
+ * 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
*
* @return void

0 comments on commit fb43664

Please sign in to comment.
Something went wrong with that request. Please try again.