Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 6 | * |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 24 | * @since Version 3.0 |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 28 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter Redis Caching Class |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Core |
| 35 | * @author Anton Lindqvist <anton@qvister.se> |
| 36 | * @link |
| 37 | */ |
| 38 | class CI_Cache_redis extends CI_Driver |
| 39 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 40 | /** |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame^] | 41 | * The name of the Redis set that is to store keys of serialized values. |
| 42 | */ |
| 43 | const KEY_SET_FOR_SERIALIZATION = '_ci_redis_serialization_set'; |
| 44 | |
| 45 | /** |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 46 | * Default config |
| 47 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 48 | * @static |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 49 | * @var array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 50 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 51 | protected static $_default_config = array( |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 52 | 'socket_type' => 'tcp', |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 53 | 'host' => '127.0.0.1', |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 54 | 'password' => NULL, |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 55 | 'port' => 6379, |
| 56 | 'timeout' => 0 |
| 57 | ); |
| 58 | |
| 59 | /** |
| 60 | * Redis connection |
| 61 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 62 | * @var Redis |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 63 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 64 | protected $_redis; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 65 | |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame^] | 66 | /** |
| 67 | * An internal cache for storing keys of serialized values. |
| 68 | * |
| 69 | * @var array |
| 70 | */ |
| 71 | protected $_serialized; |
| 72 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 73 | // ------------------------------------------------------------------------ |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Get cache |
| 77 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 78 | * @param string Cache ID |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 79 | * @return mixed |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 80 | */ |
| 81 | public function get($key) |
| 82 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame^] | 83 | $value = $this->_redis->get($key); |
| 84 | |
| 85 | if ($value !== FALSE AND in_array($key, $this->_serialized)) |
| 86 | { |
| 87 | return unserialize($value); |
| 88 | } |
| 89 | |
| 90 | return $value; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 93 | // ------------------------------------------------------------------------ |
| 94 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 95 | /** |
| 96 | * Save cache |
| 97 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 98 | * @param string $id Cache ID |
| 99 | * @param mixed $data Data to save |
| 100 | * @param int $ttl Time to live in seconds |
| 101 | * @param bool $raw Whether to store the raw value (unused) |
| 102 | * @return bool TRUE on success, FALSE on failure |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 103 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 104 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 105 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame^] | 106 | if (is_array($data) OR is_object($data)) |
| 107 | { |
| 108 | $data = serialize($data); |
| 109 | |
| 110 | if (($index_key = array_search($id, $this->_serialized)) === FALSE) |
| 111 | { |
| 112 | $this->_serialized[] = $id; |
| 113 | } |
| 114 | |
| 115 | $this->_redis->sAdd(self::KEY_SET_FOR_SERIALIZATION, $id); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | if (($index_key = array_search($id, $this->_serialized)) !== FALSE) |
| 120 | { |
| 121 | unset($this->_serialized[$index_key]); |
| 122 | } |
| 123 | |
| 124 | $this->_redis->sRemove(self::KEY_SET_FOR_SERIALIZATION, $id); |
| 125 | } |
| 126 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 127 | return ($ttl) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 128 | ? $this->_redis->setex($id, $ttl, $data) |
| 129 | : $this->_redis->set($id, $data); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 132 | // ------------------------------------------------------------------------ |
| 133 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 134 | /** |
| 135 | * Delete from cache |
| 136 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 137 | * @param string Cache key |
| 138 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 139 | */ |
| 140 | public function delete($key) |
| 141 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame^] | 142 | if (($index_key = array_search($key, $this->_serialized)) !== FALSE) |
| 143 | { |
| 144 | unset($this->_serialized[$index_key]); |
| 145 | } |
| 146 | |
| 147 | $this->_redis->sRemove(self::KEY_SET_FOR_SERIALIZATION, $key); |
| 148 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 149 | return ($this->_redis->delete($key) === 1); |
| 150 | } |
| 151 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 152 | // ------------------------------------------------------------------------ |
| 153 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 154 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 155 | * Increment a raw value |
| 156 | * |
| 157 | * @param string $id Cache ID |
| 158 | * @param int $offset Step/value to add |
| 159 | * @return mixed New value on success or FALSE on failure |
| 160 | */ |
| 161 | public function increment($id, $offset = 1) |
| 162 | { |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 163 | return $this->_redis->incr($id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // ------------------------------------------------------------------------ |
| 167 | |
| 168 | /** |
| 169 | * Decrement a raw value |
| 170 | * |
| 171 | * @param string $id Cache ID |
| 172 | * @param int $offset Step/value to reduce by |
| 173 | * @return mixed New value on success or FALSE on failure |
| 174 | */ |
| 175 | public function decrement($id, $offset = 1) |
| 176 | { |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 177 | return $this->_redis->decr($id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // ------------------------------------------------------------------------ |
| 181 | |
| 182 | /** |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 183 | * Clean cache |
| 184 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 185 | * @return bool |
| 186 | * @see Redis::flushDB() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 187 | */ |
| 188 | public function clean() |
| 189 | { |
| 190 | return $this->_redis->flushDB(); |
| 191 | } |
| 192 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 193 | // ------------------------------------------------------------------------ |
| 194 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 195 | /** |
| 196 | * Get cache driver info |
| 197 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 198 | * @param string Not supported in Redis. |
| 199 | * Only included in order to offer a |
| 200 | * consistent cache API. |
| 201 | * @return array |
| 202 | * @see Redis::info() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 203 | */ |
| 204 | public function cache_info($type = NULL) |
| 205 | { |
| 206 | return $this->_redis->info(); |
| 207 | } |
| 208 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 209 | // ------------------------------------------------------------------------ |
| 210 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 211 | /** |
| 212 | * Get cache metadata |
| 213 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 214 | * @param string Cache key |
| 215 | * @return array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 216 | */ |
| 217 | public function get_metadata($key) |
| 218 | { |
| 219 | $value = $this->get($key); |
| 220 | |
| 221 | if ($value) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 222 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 223 | return array( |
| 224 | 'expire' => time() + $this->_redis->ttl($key), |
| 225 | 'data' => $value |
| 226 | ); |
| 227 | } |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 228 | |
| 229 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 232 | // ------------------------------------------------------------------------ |
| 233 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 234 | /** |
| 235 | * Check if Redis driver is supported |
| 236 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 237 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 238 | */ |
| 239 | public function is_supported() |
| 240 | { |
| 241 | if (extension_loaded('redis')) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 242 | { |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 243 | return $this->_setup_redis(); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 244 | } |
| 245 | else |
| 246 | { |
Tyler Brownell | d967f72 | 2013-07-29 19:06:52 -0400 | [diff] [blame] | 247 | log_message('debug', 'The Redis extension must be loaded to use Redis cache.'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 248 | return FALSE; |
| 249 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 252 | // ------------------------------------------------------------------------ |
| 253 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 254 | /** |
| 255 | * Setup Redis config and connection |
| 256 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 257 | * Loads Redis config file if present. Will halt execution |
| 258 | * if a Redis connection can't be established. |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 259 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 260 | * @return bool |
| 261 | * @see Redis::connect() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 262 | */ |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 263 | protected function _setup_redis() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 264 | { |
| 265 | $config = array(); |
| 266 | $CI =& get_instance(); |
| 267 | |
| 268 | if ($CI->config->load('redis', TRUE, TRUE)) |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 269 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 270 | $config += $CI->config->item('redis'); |
| 271 | } |
| 272 | |
| 273 | $config = array_merge(self::$_default_config, $config); |
| 274 | |
| 275 | $this->_redis = new Redis(); |
| 276 | |
| 277 | try |
| 278 | { |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 279 | if ($config['socket_type'] === 'unix') |
| 280 | { |
Kakysha | ffe2130 | 2013-10-28 21:30:05 +0400 | [diff] [blame] | 281 | $success = $this->_redis->connect($config['socket']); |
Kakysha | 8f3f1f9 | 2013-10-28 23:14:08 +0400 | [diff] [blame] | 282 | } |
| 283 | else // tcp socket |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 284 | { |
Kakysha | ffe2130 | 2013-10-28 21:30:05 +0400 | [diff] [blame] | 285 | $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 286 | } |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 287 | |
Kakysha | ffe2130 | 2013-10-28 21:30:05 +0400 | [diff] [blame] | 288 | if ( ! $success) |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 289 | { |
Kakysha | 333b69b | 2013-10-29 03:49:56 +0400 | [diff] [blame] | 290 | log_message('debug', 'Cache: Redis connection refused. Check the config.'); |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 291 | return FALSE; |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 292 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 293 | } |
| 294 | catch (RedisException $e) |
| 295 | { |
Kakysha | 333b69b | 2013-10-29 03:49:56 +0400 | [diff] [blame] | 296 | log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')'); |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 297 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 298 | } |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 299 | |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 300 | if (isset($config['password'])) |
| 301 | { |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 302 | $this->_redis->auth($config['password']); |
| 303 | } |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 304 | |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame^] | 305 | // Initialize the index of selialized values. |
| 306 | $this->_serialized = $this->_redis->sMembers(self::KEY_SET_FOR_SERIALIZATION); |
| 307 | |
| 308 | if (empty($this->_serialized)) |
| 309 | { |
| 310 | // On error FALSE is returned, ensure array type for empty index. |
| 311 | $this->_serialized = array(); |
| 312 | } |
| 313 | |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 314 | return TRUE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 317 | // ------------------------------------------------------------------------ |
| 318 | |
| 319 | /** |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 320 | * Class destructor |
| 321 | * |
| 322 | * Closes the connection to Redis if present. |
| 323 | * |
| 324 | * @return void |
| 325 | */ |
| 326 | public function __destruct() |
| 327 | { |
| 328 | if ($this->_redis) |
| 329 | { |
| 330 | $this->_redis->close(); |
| 331 | } |
| 332 | } |
| 333 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 334 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 335 | |
| 336 | /* End of file Cache_redis.php */ |
| 337 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |