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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 8 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, British Columbia Institute of Technology |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 3.0.0 |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 39 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Redis Caching Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Core |
| 46 | * @author Anton Lindqvist <anton@qvister.se> |
| 47 | * @link |
| 48 | */ |
| 49 | class CI_Cache_redis extends CI_Driver |
| 50 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 51 | /** |
| 52 | * Default config |
| 53 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 54 | * @static |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 55 | * @var array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 56 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 57 | protected static $_default_config = array( |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 58 | 'socket_type' => 'tcp', |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 59 | 'host' => '127.0.0.1', |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 60 | 'password' => NULL, |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 61 | 'port' => 6379, |
| 62 | 'timeout' => 0 |
| 63 | ); |
| 64 | |
| 65 | /** |
| 66 | * Redis connection |
| 67 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 68 | * @var Redis |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 69 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 70 | protected $_redis; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 71 | |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 72 | /** |
| 73 | * An internal cache for storing keys of serialized values. |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 77 | protected $_serialized = array(); |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 78 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 79 | // ------------------------------------------------------------------------ |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 80 | |
| 81 | /** |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 82 | * Class constructor |
| 83 | * |
| 84 | * Setup Redis |
| 85 | * |
| 86 | * Loads Redis config file if present. Will halt execution |
| 87 | * if a Redis connection can't be established. |
| 88 | * |
| 89 | * @return void |
| 90 | * @see Redis::connect() |
| 91 | */ |
| 92 | public function __construct() |
| 93 | { |
Andrey Andreev | 1be8987 | 2016-03-11 18:12:57 +0200 | [diff] [blame^] | 94 | if ( ! $this->is_supported()) |
| 95 | { |
| 96 | log_message('error', 'Cache: Failed to create Redis object; extension not loaded?'); |
| 97 | return; |
| 98 | } |
| 99 | |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 100 | $config = array(); |
| 101 | $CI =& get_instance(); |
| 102 | |
| 103 | if ($CI->config->load('redis', TRUE, TRUE)) |
| 104 | { |
| 105 | $config = $CI->config->item('redis'); |
| 106 | } |
| 107 | |
| 108 | $config = array_merge(self::$_default_config, $config); |
| 109 | $this->_redis = new Redis(); |
| 110 | |
| 111 | try |
| 112 | { |
| 113 | if ($config['socket_type'] === 'unix') |
| 114 | { |
| 115 | $success = $this->_redis->connect($config['socket']); |
| 116 | } |
| 117 | else // tcp socket |
| 118 | { |
| 119 | $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); |
| 120 | } |
| 121 | |
| 122 | if ( ! $success) |
| 123 | { |
Andrey Andreev | 24a4a6a | 2015-08-31 15:11:47 +0300 | [diff] [blame] | 124 | log_message('error', 'Cache: Redis connection failed. Check your configuration.'); |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 125 | } |
Andrey Andreev | 24ff6db | 2015-09-14 13:56:41 +0300 | [diff] [blame] | 126 | |
| 127 | if (isset($config['password']) && ! $this->_redis->auth($config['password'])) |
| 128 | { |
| 129 | log_message('error', 'Cache: Redis authentication failed.'); |
| 130 | } |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 131 | } |
| 132 | catch (RedisException $e) |
| 133 | { |
Andrey Andreev | 24a4a6a | 2015-08-31 15:11:47 +0300 | [diff] [blame] | 134 | log_message('error', 'Cache: Redis connection refused ('.$e->getMessage().')'); |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 135 | } |
| 136 | |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 137 | // Initialize the index of serialized values. |
| 138 | $serialized = $this->_redis->sMembers('_ci_redis_serialized'); |
| 139 | empty($serialized) OR $this->_serialized = array_flip($serialized); |
| 140 | } |
| 141 | |
| 142 | // ------------------------------------------------------------------------ |
| 143 | |
| 144 | /** |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 145 | * Get cache |
| 146 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 147 | * @param string Cache ID |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 148 | * @return mixed |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 149 | */ |
| 150 | public function get($key) |
| 151 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 152 | $value = $this->_redis->get($key); |
| 153 | |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 154 | if ($value !== FALSE && isset($this->_serialized[$key])) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 155 | { |
| 156 | return unserialize($value); |
| 157 | } |
| 158 | |
| 159 | return $value; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 162 | // ------------------------------------------------------------------------ |
| 163 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 164 | /** |
| 165 | * Save cache |
| 166 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 167 | * @param string $id Cache ID |
| 168 | * @param mixed $data Data to save |
| 169 | * @param int $ttl Time to live in seconds |
| 170 | * @param bool $raw Whether to store the raw value (unused) |
| 171 | * @return bool TRUE on success, FALSE on failure |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 172 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 173 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 174 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 175 | if (is_array($data) OR is_object($data)) |
| 176 | { |
Mathew White | 01015d9 | 2015-03-22 12:46:49 +0000 | [diff] [blame] | 177 | if ( ! $this->_redis->sIsMember('_ci_redis_serialized', $id) && ! $this->_redis->sAdd('_ci_redis_serialized', $id)) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 178 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 179 | return FALSE; |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 180 | } |
| 181 | |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 182 | isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE; |
| 183 | $data = serialize($data); |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 184 | } |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 185 | elseif (isset($this->_serialized[$id])) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 186 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 187 | $this->_serialized[$id] = NULL; |
Ivan Tcholakov | d245f06 | 2014-08-18 13:52:44 +0300 | [diff] [blame] | 188 | $this->_redis->sRemove('_ci_redis_serialized', $id); |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 189 | } |
| 190 | |
ftwbzhao | b4f3630 | 2015-05-13 17:47:41 +0800 | [diff] [blame] | 191 | return $this->_redis->set($id, $data, $ttl); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 194 | // ------------------------------------------------------------------------ |
| 195 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 196 | /** |
| 197 | * Delete from cache |
| 198 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 199 | * @param string Cache key |
| 200 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 201 | */ |
| 202 | public function delete($key) |
| 203 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 204 | if ($this->_redis->delete($key) !== 1) |
Ivan Tcholakov | d514d5c | 2014-08-18 12:04:27 +0300 | [diff] [blame] | 205 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 206 | return FALSE; |
Ivan Tcholakov | d514d5c | 2014-08-18 12:04:27 +0300 | [diff] [blame] | 207 | } |
Ivan Tcholakov | bc41761 | 2014-08-18 11:11:39 +0300 | [diff] [blame] | 208 | |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 209 | if (isset($this->_serialized[$key])) |
| 210 | { |
| 211 | $this->_serialized[$key] = NULL; |
| 212 | $this->_redis->sRemove('_ci_redis_serialized', $key); |
| 213 | } |
| 214 | |
| 215 | return TRUE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 218 | // ------------------------------------------------------------------------ |
| 219 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 220 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 221 | * Increment a raw value |
| 222 | * |
| 223 | * @param string $id Cache ID |
| 224 | * @param int $offset Step/value to add |
| 225 | * @return mixed New value on success or FALSE on failure |
| 226 | */ |
| 227 | public function increment($id, $offset = 1) |
| 228 | { |
Andrey Andreev | 70f738a | 2015-05-07 12:16:33 +0300 | [diff] [blame] | 229 | return $this->_redis->incr($id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // ------------------------------------------------------------------------ |
| 233 | |
| 234 | /** |
| 235 | * Decrement a raw value |
| 236 | * |
| 237 | * @param string $id Cache ID |
| 238 | * @param int $offset Step/value to reduce by |
| 239 | * @return mixed New value on success or FALSE on failure |
| 240 | */ |
| 241 | public function decrement($id, $offset = 1) |
| 242 | { |
Andrey Andreev | 70f738a | 2015-05-07 12:16:33 +0300 | [diff] [blame] | 243 | return $this->_redis->decr($id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // ------------------------------------------------------------------------ |
| 247 | |
| 248 | /** |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 249 | * Clean cache |
| 250 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 251 | * @return bool |
| 252 | * @see Redis::flushDB() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 253 | */ |
| 254 | public function clean() |
| 255 | { |
| 256 | return $this->_redis->flushDB(); |
| 257 | } |
| 258 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 259 | // ------------------------------------------------------------------------ |
| 260 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 261 | /** |
| 262 | * Get cache driver info |
| 263 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 264 | * @param string Not supported in Redis. |
| 265 | * Only included in order to offer a |
| 266 | * consistent cache API. |
| 267 | * @return array |
| 268 | * @see Redis::info() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 269 | */ |
| 270 | public function cache_info($type = NULL) |
| 271 | { |
| 272 | return $this->_redis->info(); |
| 273 | } |
| 274 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 275 | // ------------------------------------------------------------------------ |
| 276 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 277 | /** |
| 278 | * Get cache metadata |
| 279 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 280 | * @param string Cache key |
| 281 | * @return array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 282 | */ |
| 283 | public function get_metadata($key) |
| 284 | { |
| 285 | $value = $this->get($key); |
| 286 | |
ftwbzhao | b2119a7 | 2015-04-09 13:27:01 +0800 | [diff] [blame] | 287 | if ($value !== FALSE) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 288 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 289 | return array( |
| 290 | 'expire' => time() + $this->_redis->ttl($key), |
| 291 | 'data' => $value |
| 292 | ); |
| 293 | } |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 294 | |
| 295 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 298 | // ------------------------------------------------------------------------ |
| 299 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 300 | /** |
| 301 | * Check if Redis driver is supported |
| 302 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 303 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 304 | */ |
| 305 | public function is_supported() |
| 306 | { |
Andrey Andreev | 24a4a6a | 2015-08-31 15:11:47 +0300 | [diff] [blame] | 307 | return extension_loaded('redis'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 310 | // ------------------------------------------------------------------------ |
| 311 | |
| 312 | /** |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 313 | * Class destructor |
| 314 | * |
| 315 | * Closes the connection to Redis if present. |
| 316 | * |
| 317 | * @return void |
| 318 | */ |
| 319 | public function __destruct() |
| 320 | { |
| 321 | if ($this->_redis) |
| 322 | { |
| 323 | $this->_redis->close(); |
| 324 | } |
| 325 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 326 | } |