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 | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2015, 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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2015, 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 |
| 34 | * @link http://codeigniter.com |
| 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 | /** |
| 82 | * Get cache |
| 83 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 84 | * @param string Cache ID |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 85 | * @return mixed |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 86 | */ |
| 87 | public function get($key) |
| 88 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 89 | $value = $this->_redis->get($key); |
| 90 | |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 91 | if ($value !== FALSE && isset($this->_serialized[$key])) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 92 | { |
| 93 | return unserialize($value); |
| 94 | } |
| 95 | |
| 96 | return $value; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 99 | // ------------------------------------------------------------------------ |
| 100 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 101 | /** |
| 102 | * Save cache |
| 103 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 104 | * @param string $id Cache ID |
| 105 | * @param mixed $data Data to save |
| 106 | * @param int $ttl Time to live in seconds |
| 107 | * @param bool $raw Whether to store the raw value (unused) |
| 108 | * @return bool TRUE on success, FALSE on failure |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 109 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 110 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 111 | { |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 112 | if (is_array($data) OR is_object($data)) |
| 113 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 114 | if ( ! $this->_redis->sAdd('_ci_redis_serialized', $id)) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 115 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 116 | return FALSE; |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 117 | } |
| 118 | |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 119 | isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE; |
| 120 | $data = serialize($data); |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 121 | } |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 122 | elseif (isset($this->_serialized[$id])) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 123 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 124 | $this->_serialized[$id] = NULL; |
Ivan Tcholakov | d245f06 | 2014-08-18 13:52:44 +0300 | [diff] [blame] | 125 | $this->_redis->sRemove('_ci_redis_serialized', $id); |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 126 | } |
| 127 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 128 | return ($ttl) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 129 | ? $this->_redis->setex($id, $ttl, $data) |
| 130 | : $this->_redis->set($id, $data); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 133 | // ------------------------------------------------------------------------ |
| 134 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 135 | /** |
| 136 | * Delete from cache |
| 137 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 138 | * @param string Cache key |
| 139 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 140 | */ |
| 141 | public function delete($key) |
| 142 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 143 | if ($this->_redis->delete($key) !== 1) |
Ivan Tcholakov | d514d5c | 2014-08-18 12:04:27 +0300 | [diff] [blame] | 144 | { |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 145 | return FALSE; |
Ivan Tcholakov | d514d5c | 2014-08-18 12:04:27 +0300 | [diff] [blame] | 146 | } |
Ivan Tcholakov | bc41761 | 2014-08-18 11:11:39 +0300 | [diff] [blame] | 147 | |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 148 | if (isset($this->_serialized[$key])) |
| 149 | { |
| 150 | $this->_serialized[$key] = NULL; |
| 151 | $this->_redis->sRemove('_ci_redis_serialized', $key); |
| 152 | } |
| 153 | |
| 154 | return TRUE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 157 | // ------------------------------------------------------------------------ |
| 158 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 159 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 160 | * Increment a raw value |
| 161 | * |
| 162 | * @param string $id Cache ID |
| 163 | * @param int $offset Step/value to add |
| 164 | * @return mixed New value on success or FALSE on failure |
| 165 | */ |
| 166 | public function increment($id, $offset = 1) |
| 167 | { |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 168 | return $this->_redis->incr($id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // ------------------------------------------------------------------------ |
| 172 | |
| 173 | /** |
| 174 | * Decrement a raw value |
| 175 | * |
| 176 | * @param string $id Cache ID |
| 177 | * @param int $offset Step/value to reduce by |
| 178 | * @return mixed New value on success or FALSE on failure |
| 179 | */ |
| 180 | public function decrement($id, $offset = 1) |
| 181 | { |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 182 | return $this->_redis->decr($id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // ------------------------------------------------------------------------ |
| 186 | |
| 187 | /** |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 188 | * Clean cache |
| 189 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 190 | * @return bool |
| 191 | * @see Redis::flushDB() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 192 | */ |
| 193 | public function clean() |
| 194 | { |
| 195 | return $this->_redis->flushDB(); |
| 196 | } |
| 197 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 198 | // ------------------------------------------------------------------------ |
| 199 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 200 | /** |
| 201 | * Get cache driver info |
| 202 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 203 | * @param string Not supported in Redis. |
| 204 | * Only included in order to offer a |
| 205 | * consistent cache API. |
| 206 | * @return array |
| 207 | * @see Redis::info() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 208 | */ |
| 209 | public function cache_info($type = NULL) |
| 210 | { |
| 211 | return $this->_redis->info(); |
| 212 | } |
| 213 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 214 | // ------------------------------------------------------------------------ |
| 215 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 216 | /** |
| 217 | * Get cache metadata |
| 218 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 219 | * @param string Cache key |
| 220 | * @return array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 221 | */ |
| 222 | public function get_metadata($key) |
| 223 | { |
| 224 | $value = $this->get($key); |
| 225 | |
| 226 | if ($value) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 227 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 228 | return array( |
| 229 | 'expire' => time() + $this->_redis->ttl($key), |
| 230 | 'data' => $value |
| 231 | ); |
| 232 | } |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 233 | |
| 234 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 237 | // ------------------------------------------------------------------------ |
| 238 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 239 | /** |
| 240 | * Check if Redis driver is supported |
| 241 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 242 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 243 | */ |
| 244 | public function is_supported() |
| 245 | { |
| 246 | if (extension_loaded('redis')) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 247 | { |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 248 | return $this->_setup_redis(); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 249 | } |
| 250 | else |
| 251 | { |
Tyler Brownell | d967f72 | 2013-07-29 19:06:52 -0400 | [diff] [blame] | 252 | 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] | 253 | return FALSE; |
| 254 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 257 | // ------------------------------------------------------------------------ |
| 258 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 259 | /** |
| 260 | * Setup Redis config and connection |
| 261 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 262 | * Loads Redis config file if present. Will halt execution |
| 263 | * if a Redis connection can't be established. |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 264 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 265 | * @return bool |
| 266 | * @see Redis::connect() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 267 | */ |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 268 | protected function _setup_redis() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 269 | { |
| 270 | $config = array(); |
| 271 | $CI =& get_instance(); |
| 272 | |
| 273 | if ($CI->config->load('redis', TRUE, TRUE)) |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 274 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 275 | $config += $CI->config->item('redis'); |
| 276 | } |
| 277 | |
| 278 | $config = array_merge(self::$_default_config, $config); |
| 279 | |
| 280 | $this->_redis = new Redis(); |
| 281 | |
| 282 | try |
| 283 | { |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 284 | if ($config['socket_type'] === 'unix') |
| 285 | { |
Kakysha | ffe2130 | 2013-10-28 21:30:05 +0400 | [diff] [blame] | 286 | $success = $this->_redis->connect($config['socket']); |
Kakysha | 8f3f1f9 | 2013-10-28 23:14:08 +0400 | [diff] [blame] | 287 | } |
| 288 | else // tcp socket |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 289 | { |
Kakysha | ffe2130 | 2013-10-28 21:30:05 +0400 | [diff] [blame] | 290 | $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 291 | } |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 292 | |
Kakysha | ffe2130 | 2013-10-28 21:30:05 +0400 | [diff] [blame] | 293 | if ( ! $success) |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 294 | { |
Kakysha | 333b69b | 2013-10-29 03:49:56 +0400 | [diff] [blame] | 295 | log_message('debug', 'Cache: Redis connection refused. Check the config.'); |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 296 | return FALSE; |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame] | 297 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 298 | } |
| 299 | catch (RedisException $e) |
| 300 | { |
Kakysha | 333b69b | 2013-10-29 03:49:56 +0400 | [diff] [blame] | 301 | log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')'); |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 302 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 303 | } |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 304 | |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 305 | if (isset($config['password'])) |
| 306 | { |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 307 | $this->_redis->auth($config['password']); |
| 308 | } |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 309 | |
Ivan Tcholakov | d245f06 | 2014-08-18 13:52:44 +0300 | [diff] [blame] | 310 | // Initialize the index of serialized values. |
Andrey Andreev | 2492b50 | 2014-08-18 16:20:05 +0300 | [diff] [blame] | 311 | $serialized = $this->_redis->sMembers('_ci_redis_serialized'); |
| 312 | if ( ! empty($serialized)) |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 313 | { |
Andrey Andreev | 1b634f8 | 2014-08-18 16:21:11 +0300 | [diff] [blame] | 314 | $this->_serialized = array_flip($serialized); |
Ivan Tcholakov | 927e508 | 2014-08-14 04:07:39 +0300 | [diff] [blame] | 315 | } |
| 316 | |
Kakysha | 2d14673 | 2013-10-28 20:20:24 +0400 | [diff] [blame] | 317 | return TRUE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 320 | // ------------------------------------------------------------------------ |
| 321 | |
| 322 | /** |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 323 | * Class destructor |
| 324 | * |
| 325 | * Closes the connection to Redis if present. |
| 326 | * |
| 327 | * @return void |
| 328 | */ |
| 329 | public function __destruct() |
| 330 | { |
| 331 | if ($this->_redis) |
| 332 | { |
| 333 | $this->_redis->close(); |
| 334 | } |
| 335 | } |
| 336 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 337 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 338 | |
| 339 | /* End of file Cache_redis.php */ |
| 340 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |