Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
| 9 | * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc. |
| 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 2.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Redis Caching Class |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Core |
| 24 | * @author Anton Lindqvist <anton@qvister.se> |
| 25 | * @link |
| 26 | */ |
| 27 | class CI_Cache_redis extends CI_Driver |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Default config |
| 32 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 33 | * @static |
| 34 | * @var array |
| 35 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame^] | 36 | protected static $_default_config = array( |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 37 | 'host' => '127.0.0.1', |
| 38 | 'port' => 6379, |
| 39 | 'timeout' => 0 |
| 40 | ); |
| 41 | |
| 42 | /** |
| 43 | * Redis connection |
| 44 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 45 | * @var Redis |
| 46 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame^] | 47 | protected $_redis; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Class destructor |
| 51 | * |
| 52 | * Closes the connection to Redis if present. |
| 53 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 54 | * @return void |
| 55 | */ |
| 56 | public function __destruct() |
| 57 | { |
| 58 | if ($this->_redis) |
| 59 | { |
| 60 | $this->_redis->close(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get cache |
| 66 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 67 | * @param string $key Cache key identifier |
| 68 | * @return mixed |
| 69 | */ |
| 70 | public function get($key) |
| 71 | { |
| 72 | return $this->_redis->get($key); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Save cache |
| 77 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 78 | * @param string $key Cache key identifier |
| 79 | * @param mixed $value Data to save |
| 80 | * @param integer $ttl Time to live |
| 81 | * @return boolean |
| 82 | */ |
| 83 | public function save($key, $value, $ttl = NULL) |
| 84 | { |
| 85 | return ($ttl) |
| 86 | ? $this->_redis->setex($key, $ttl, $value) |
| 87 | : $this->_redis->set($key, $value); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Delete from cache |
| 92 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 93 | * @param string $key Cache key |
| 94 | * @return boolean |
| 95 | */ |
| 96 | public function delete($key) |
| 97 | { |
| 98 | return ($this->_redis->delete($key) === 1); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Clean cache |
| 103 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 104 | * @return boolean |
| 105 | * @see Redis::flushDB() |
| 106 | */ |
| 107 | public function clean() |
| 108 | { |
| 109 | return $this->_redis->flushDB(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get cache driver info |
| 114 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 115 | * @param string $type Not supported in Redis. Only included in order to offer a |
| 116 | * consistent cache API. |
| 117 | * @return array |
| 118 | * @see Redis::info() |
| 119 | */ |
| 120 | public function cache_info($type = NULL) |
| 121 | { |
| 122 | return $this->_redis->info(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get cache metadata |
| 127 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 128 | * @param string $key Cache key |
| 129 | * @return array |
| 130 | */ |
| 131 | public function get_metadata($key) |
| 132 | { |
| 133 | $value = $this->get($key); |
| 134 | |
| 135 | if ($value) |
| 136 | { |
| 137 | return array( |
| 138 | 'expire' => time() + $this->_redis->ttl($key), |
| 139 | 'data' => $value |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Check if Redis driver is supported |
| 146 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 147 | * @return boolean |
| 148 | */ |
| 149 | public function is_supported() |
| 150 | { |
| 151 | if (extension_loaded('redis')) |
| 152 | { |
| 153 | $this->_setup_redis(); |
| 154 | |
| 155 | return TRUE; |
| 156 | } |
| 157 | else |
| 158 | { |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame^] | 159 | log_message('error', 'The Redis extension must be loaded to use Redis cache.'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 160 | |
| 161 | return FALSE; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Setup Redis config and connection |
| 168 | * |
| 169 | * Loads Redis config file if present. Will halt execution if a Redis connection |
| 170 | * can't be established. |
| 171 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 172 | * @return void |
| 173 | * @see Redis::connect() |
| 174 | */ |
| 175 | private function _setup_redis() |
| 176 | { |
| 177 | $config = array(); |
| 178 | $CI =& get_instance(); |
| 179 | |
| 180 | if ($CI->config->load('redis', TRUE, TRUE)) |
| 181 | { |
| 182 | $config += $CI->config->item('redis'); |
| 183 | } |
| 184 | |
| 185 | $config = array_merge(self::$_default_config, $config); |
| 186 | |
| 187 | $this->_redis = new Redis(); |
| 188 | |
| 189 | try |
| 190 | { |
| 191 | $this->_redis->connect($config['host'], $config['port'], $config['timeout']); |
| 192 | } |
| 193 | catch (RedisException $e) |
| 194 | { |
| 195 | show_error('Redis connection refused. ' . $e->getMessage()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | // End Class |
| 201 | |
| 202 | /* End of file Cache_redis.php */ |
| 203 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |