From 1e8be299b1c0f6385fb0536a6983147bd8ac029e Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Sat, 21 Jan 2012 12:25:08 +0100 Subject: Added redis cache driver. --- system/libraries/Cache/drivers/Cache_redis.php | 217 +++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 system/libraries/Cache/drivers/Cache_redis.php (limited to 'system/libraries/Cache/drivers') diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php new file mode 100644 index 000000000..9eb7a8d4e --- /dev/null +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -0,0 +1,217 @@ + + * @link + */ +class CI_Cache_redis extends CI_Driver +{ + + /** + * Default config + * + * @access private + * @static + * @var array + */ + private static $_default_config = array( + 'host' => '127.0.0.1', + 'port' => 6379, + 'timeout' => 0 + ); + + /** + * Redis connection + * + * @access private + * @var Redis + */ + private $_redis; + + /** + * Class destructor + * + * Closes the connection to Redis if present. + * + * @access public + * @return void + */ + public function __destruct() + { + if ($this->_redis) + { + $this->_redis->close(); + } + } + + /** + * Get cache + * + * @access public + * @param string $key Cache key identifier + * @return mixed + */ + public function get($key) + { + return $this->_redis->get($key); + } + + /** + * Save cache + * + * @access public + * @param string $key Cache key identifier + * @param mixed $value Data to save + * @param integer $ttl Time to live + * @return boolean + */ + public function save($key, $value, $ttl = NULL) + { + return ($ttl) + ? $this->_redis->setex($key, $ttl, $value) + : $this->_redis->set($key, $value); + } + + /** + * Delete from cache + * + * @access public + * @param string $key Cache key + * @return boolean + */ + public function delete($key) + { + return ($this->_redis->delete($key) === 1); + } + + /** + * Clean cache + * + * @access public + * @return boolean + * @see Redis::flushDB() + */ + public function clean() + { + return $this->_redis->flushDB(); + } + + /** + * Get cache driver info + * + * @access public + * @param string $type Not supported in Redis. Only included in order to offer a + * consistent cache API. + * @return array + * @see Redis::info() + */ + public function cache_info($type = NULL) + { + return $this->_redis->info(); + } + + /** + * Get cache metadata + * + * @access public + * @param string $key Cache key + * @return array + */ + public function get_metadata($key) + { + $value = $this->get($key); + + if ($value) + { + return array( + 'expire' => time() + $this->_redis->ttl($key), + 'data' => $value + ); + } + } + + /** + * Check if Redis driver is supported + * + * @access public + * @return boolean + */ + public function is_supported() + { + if (extension_loaded('redis')) + { + $this->_setup_redis(); + + return TRUE; + } + else + { + log_message( + 'error', + 'The Redis extension must be loaded to use Redis cache.' + ); + + return FALSE; + } + + } + + /** + * Setup Redis config and connection + * + * Loads Redis config file if present. Will halt execution if a Redis connection + * can't be established. + * + * @access private + * @return void + * @see Redis::connect() + */ + private function _setup_redis() + { + $config = array(); + $CI =& get_instance(); + + if ($CI->config->load('redis', TRUE, TRUE)) + { + $config += $CI->config->item('redis'); + } + + $config = array_merge(self::$_default_config, $config); + + $this->_redis = new Redis(); + + try + { + $this->_redis->connect($config['host'], $config['port'], $config['timeout']); + } + catch (RedisException $e) + { + show_error('Redis connection refused. ' . $e->getMessage()); + } + } + +} +// End Class + +/* End of file Cache_redis.php */ +/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 3573af8b98ceeb2308a1eaf02ee3b327dfca82b0 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Sat, 21 Jan 2012 20:33:12 +0100 Subject: Fixed syntax according to feedback. --- system/libraries/Cache/drivers/Cache_redis.php | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'system/libraries/Cache/drivers') diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index 9eb7a8d4e..f3acc6e46 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -30,11 +30,10 @@ class CI_Cache_redis extends CI_Driver /** * Default config * - * @access private * @static * @var array */ - private static $_default_config = array( + protected static $_default_config = array( 'host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0 @@ -43,17 +42,15 @@ class CI_Cache_redis extends CI_Driver /** * Redis connection * - * @access private * @var Redis */ - private $_redis; + protected $_redis; /** * Class destructor * * Closes the connection to Redis if present. * - * @access public * @return void */ public function __destruct() @@ -67,7 +64,6 @@ class CI_Cache_redis extends CI_Driver /** * Get cache * - * @access public * @param string $key Cache key identifier * @return mixed */ @@ -79,7 +75,6 @@ class CI_Cache_redis extends CI_Driver /** * Save cache * - * @access public * @param string $key Cache key identifier * @param mixed $value Data to save * @param integer $ttl Time to live @@ -95,7 +90,6 @@ class CI_Cache_redis extends CI_Driver /** * Delete from cache * - * @access public * @param string $key Cache key * @return boolean */ @@ -107,7 +101,6 @@ class CI_Cache_redis extends CI_Driver /** * Clean cache * - * @access public * @return boolean * @see Redis::flushDB() */ @@ -119,7 +112,6 @@ class CI_Cache_redis extends CI_Driver /** * Get cache driver info * - * @access public * @param string $type Not supported in Redis. Only included in order to offer a * consistent cache API. * @return array @@ -133,7 +125,6 @@ class CI_Cache_redis extends CI_Driver /** * Get cache metadata * - * @access public * @param string $key Cache key * @return array */ @@ -153,7 +144,6 @@ class CI_Cache_redis extends CI_Driver /** * Check if Redis driver is supported * - * @access public * @return boolean */ public function is_supported() @@ -166,10 +156,7 @@ class CI_Cache_redis extends CI_Driver } else { - log_message( - 'error', - 'The Redis extension must be loaded to use Redis cache.' - ); + log_message('error', 'The Redis extension must be loaded to use Redis cache.'); return FALSE; } @@ -182,7 +169,6 @@ class CI_Cache_redis extends CI_Driver * Loads Redis config file if present. Will halt execution if a Redis connection * can't be established. * - * @access private * @return void * @see Redis::connect() */ -- cgit v1.2.3-24-g4f1b From 5a1d953e8a492326b8e8cbd0473b1593fe42cfa6 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Mon, 23 Jan 2012 23:20:26 +0100 Subject: Updated redis driver doc block. --- system/libraries/Cache/drivers/Cache_redis.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'system/libraries/Cache/drivers') diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index f3acc6e46..5d42905cb 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -4,12 +4,24 @@ * * An open source application development framework for PHP 5.1.6 or newer * - * @package CodeIgniter - * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc. - * @license http://codeigniter.com/user_guide/license.html - * @link http://codeigniter.com - * @since Version 2.0 + * NOTICE OF LICENSE + * + * Licensed under the Open Software License version 3.0 + * + * This source file is subject to the Open Software License (OSL 3.0) that is + * bundled with this package in the files license.txt / license.rst. It is + * also available through the world wide web at this URL: + * http://opensource.org/licenses/OSL-3.0 + * If you did not receive a copy of the license and are unable to obtain it + * through the world wide web, please send an email to + * licensing@ellislab.com so we can send you a copy immediately. + * + * @package CodeIgniter + * @author EllisLab Dev Team + * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc. + * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @link http://codeigniter.com + * @since Version 2.0 * @filesource */ -- cgit v1.2.3-24-g4f1b From 210e664abe857ddd267a7ba8713e2318d3e59a9c Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Mon, 23 Apr 2012 10:13:46 +0200 Subject: Added redis auth support. --- system/libraries/Cache/drivers/Cache_redis.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'system/libraries/Cache/drivers') diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index 5d42905cb..8c650ff43 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -47,6 +47,7 @@ class CI_Cache_redis extends CI_Driver */ protected static $_default_config = array( 'host' => '127.0.0.1', + 'password' => null, 'port' => 6379, 'timeout' => 0 ); @@ -206,6 +207,10 @@ class CI_Cache_redis extends CI_Driver { show_error('Redis connection refused. ' . $e->getMessage()); } + + if (isset($config['password'])) { + $this->_redis->auth($config['password']); + } } } -- cgit v1.2.3-24-g4f1b From 5ccf5ce0189669f003a578de88936209c7b331fd Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Fri, 8 Jun 2012 11:47:17 +0200 Subject: Fixed according to feedback. --- system/libraries/Cache/drivers/Cache_redis.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'system/libraries/Cache/drivers') diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index 8c650ff43..205f17cd1 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -2,7 +2,7 @@ /** * CodeIgniter * - * An open source application development framework for PHP 5.1.6 or newer + * An open source application development framework for PHP 5.2.4 or newer * * NOTICE OF LICENSE * @@ -47,7 +47,7 @@ class CI_Cache_redis extends CI_Driver */ protected static $_default_config = array( 'host' => '127.0.0.1', - 'password' => null, + 'password' => NULL, 'port' => 6379, 'timeout' => 0 ); @@ -185,13 +185,13 @@ class CI_Cache_redis extends CI_Driver * @return void * @see Redis::connect() */ - private function _setup_redis() + protected function _setup_redis() { $config = array(); $CI =& get_instance(); if ($CI->config->load('redis', TRUE, TRUE)) - { + { $config += $CI->config->item('redis'); } @@ -208,7 +208,8 @@ class CI_Cache_redis extends CI_Driver show_error('Redis connection refused. ' . $e->getMessage()); } - if (isset($config['password'])) { + if (isset($config['password'])) + { $this->_redis->auth($config['password']); } } -- cgit v1.2.3-24-g4f1b