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] * 'probability'=> 100, //[optional]
* 'prefix' => Inflector::slug(APP_DIR) . '_', //[optional] prefix every cache file with this string * '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')); Cache::config('default', array('engine' => 'File'));

View file

@ -36,6 +36,9 @@ class RedisEngine extends CacheEngine {
* Settings * Settings
* *
* - server = string url or ip to the Redis server host * - 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 * @var array
*/ */
@ -58,9 +61,9 @@ class RedisEngine extends CacheEngine {
'engine' => 'Redis', 'engine' => 'Redis',
'prefix' => null, 'prefix' => null,
'server' => '127.0.0.1', 'server' => '127.0.0.1',
'port' => null, 'port' => 6379,
'persistent' => true, 'timeout' => 0,
'serialize' => true 'persistent' => true
), $settings) ), $settings)
); );
@ -77,9 +80,9 @@ class RedisEngine extends CacheEngine {
try { try {
$this->_Redis = new Redis(); $this->_Redis = new Redis();
if (empty($this->settings['persistent'])) { 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 { } else {
$return = $this->_Redis->pconnect($this->settings['server']); $return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
} }
} catch (RedisException $e) { } catch (RedisException $e) {
return false; return false;

View file

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