Adding timeout and port options to the redis cache engine

This commit is contained in:
Jelle Henkens 2012-05-29 23:21:18 +01:00
parent 06476a22da
commit ab1f336e21
3 changed files with 23 additions and 7 deletions

View file

@ -82,6 +82,19 @@
* 'probability'=> 100, //[optional]
* 'prefix' => Inflector::slug(APP_DIR) . '_', //[optional] prefix every cache file with this string
* ));
*
* Redis (http://http://redis.io/)
*
* Cache::config('default', array(
* 'engine' => 'Redis', //[required]
* 'duration'=> 3600, //[optional]
* 'probability'=> 100, //[optional]
* 'prefix' => Inflector::slug(APP_DIR) . '_', //[optional] prefix every cache file with this string
* 'server' => '127.0.0.1' // localhost
* 'port' => 6379 // default port 6379
* 'timeout' => 0 // timeout in seconds, 0 = unlimited
* 'persistent' => true, // [optional] set this to false for non-persistent connections
* ));
*/
Cache::config('default', array('engine' => 'File'));

View file

@ -36,6 +36,9 @@ class RedisEngine extends CacheEngine {
* Settings
*
* - server = string url or ip to the Redis server host
* - port = integer port number to the Redis server (default: 6379)
* - timeout = float timeout in seconds (default: 0)
* - persistent = bool Connects to the Redis server with a persistent connection (default: true)
*
* @var array
*/
@ -58,9 +61,9 @@ class RedisEngine extends CacheEngine {
'engine' => 'Redis',
'prefix' => null,
'server' => '127.0.0.1',
'port' => null,
'persistent' => true,
'serialize' => true
'port' => 6379,
'timeout' => 0,
'persistent' => true
), $settings)
);
@ -77,9 +80,9 @@ class RedisEngine extends CacheEngine {
try {
$this->_Redis = new Redis();
if (empty($this->settings['persistent'])) {
$return = $this->_Redis->connect($this->settings['server']);
$return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
} else {
$return = $this->_Redis->pconnect($this->settings['server']);
$return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
}
} catch (RedisException $e) {
return false;

View file

@ -64,15 +64,15 @@ class RegisEngineTest extends CakeTestCase {
*/
public function testSettings() {
$settings = Cache::settings('redis');
unset($settings['serialize'], $settings['path']);
$expecting = array(
'port' => null,
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
'groups' => array(),
'engine' => 'Redis',
'server' => '127.0.0.1',
'port' => 6379,
'timeout' => 0,
'persistent' => true
);
$this->assertEquals($expecting, $settings);