blob: 66966ba166ea94412b61c30470efceebc18eb17b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Anton Lindqvist1e8be292012-01-21 12:25:08 +01002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Anton Lindqvist1e8be292012-01-21 12:25:08 +01006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Anton Lindqvist5a1d9532012-01-23 23:20:26 +01008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Anton Lindqvist5a1d9532012-01-23 23:20:26 +010010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Lindqvist5a1d9532012-01-23 23:20:26 +010017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 3.0.0
Anton Lindqvist1e8be292012-01-21 12:25:08 +010036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Anton Lindqvist1e8be292012-01-21 12:25:08 +010039
Anton Lindqvist1e8be292012-01-21 12:25:08 +010040/**
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 */
49class CI_Cache_redis extends CI_Driver
50{
Anton Lindqvist1e8be292012-01-21 12:25:08 +010051 /**
52 * Default config
53 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010054 * @static
Andrey Andreev9e674f72012-06-09 21:02:52 +030055 * @var array
Anton Lindqvist1e8be292012-01-21 12:25:08 +010056 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010057 protected static $_default_config = array(
Kakysha80d663a2013-10-28 01:39:17 +040058 'socket_type' => 'tcp',
Anton Lindqvist1e8be292012-01-21 12:25:08 +010059 'host' => '127.0.0.1',
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +020060 'password' => NULL,
Anton Lindqvist1e8be292012-01-21 12:25:08 +010061 'port' => 6379,
62 'timeout' => 0
63 );
64
65 /**
66 * Redis connection
67 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030068 * @var Redis
Anton Lindqvist1e8be292012-01-21 12:25:08 +010069 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010070 protected $_redis;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010071
Ivan Tcholakov927e5082014-08-14 04:07:39 +030072 /**
73 * An internal cache for storing keys of serialized values.
74 *
75 * @var array
76 */
Andrey Andreev2492b502014-08-18 16:20:05 +030077 protected $_serialized = array();
Ivan Tcholakov927e5082014-08-14 04:07:39 +030078
Andrey Andreev9e674f72012-06-09 21:02:52 +030079 // ------------------------------------------------------------------------
Anton Lindqvist1e8be292012-01-21 12:25:08 +010080
81 /**
82 * Get cache
83 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020084 * @param string Cache ID
Andrey Andreev9e674f72012-06-09 21:02:52 +030085 * @return mixed
Anton Lindqvist1e8be292012-01-21 12:25:08 +010086 */
87 public function get($key)
88 {
Ivan Tcholakov927e5082014-08-14 04:07:39 +030089 $value = $this->_redis->get($key);
90
Andrey Andreev2492b502014-08-18 16:20:05 +030091 if ($value !== FALSE && isset($this->_serialized[$key]))
Ivan Tcholakov927e5082014-08-14 04:07:39 +030092 {
93 return unserialize($value);
94 }
95
96 return $value;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010097 }
98
Andrey Andreev9e674f72012-06-09 21:02:52 +030099 // ------------------------------------------------------------------------
100
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100101 /**
102 * Save cache
103 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200104 * @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 Lindqvist1e8be292012-01-21 12:25:08 +0100109 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200110 public function save($id, $data, $ttl = 60, $raw = FALSE)
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100111 {
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300112 if (is_array($data) OR is_object($data))
113 {
Mathew White01015d92015-03-22 12:46:49 +0000114 if ( ! $this->_redis->sIsMember('_ci_redis_serialized', $id) && ! $this->_redis->sAdd('_ci_redis_serialized', $id))
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300115 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300116 return FALSE;
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300117 }
118
Andrey Andreev2492b502014-08-18 16:20:05 +0300119 isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE;
120 $data = serialize($data);
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300121 }
Andrey Andreev2492b502014-08-18 16:20:05 +0300122 elseif (isset($this->_serialized[$id]))
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300123 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300124 $this->_serialized[$id] = NULL;
Ivan Tcholakovd245f062014-08-18 13:52:44 +0300125 $this->_redis->sRemove('_ci_redis_serialized', $id);
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300126 }
127
ftwbzhaob4f36302015-05-13 17:47:41 +0800128 return $this->_redis->set($id, $data, $ttl);
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100129 }
130
Andrey Andreev9e674f72012-06-09 21:02:52 +0300131 // ------------------------------------------------------------------------
132
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100133 /**
134 * Delete from cache
135 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300136 * @param string Cache key
137 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100138 */
139 public function delete($key)
140 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300141 if ($this->_redis->delete($key) !== 1)
Ivan Tcholakovd514d5c2014-08-18 12:04:27 +0300142 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300143 return FALSE;
Ivan Tcholakovd514d5c2014-08-18 12:04:27 +0300144 }
Ivan Tcholakovbc417612014-08-18 11:11:39 +0300145
Andrey Andreev2492b502014-08-18 16:20:05 +0300146 if (isset($this->_serialized[$key]))
147 {
148 $this->_serialized[$key] = NULL;
149 $this->_redis->sRemove('_ci_redis_serialized', $key);
150 }
151
152 return TRUE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100153 }
154
Andrey Andreev9e674f72012-06-09 21:02:52 +0300155 // ------------------------------------------------------------------------
156
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100157 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200158 * Increment a raw value
159 *
160 * @param string $id Cache ID
161 * @param int $offset Step/value to add
162 * @return mixed New value on success or FALSE on failure
163 */
164 public function increment($id, $offset = 1)
165 {
Andrey Andreev70f738a2015-05-07 12:16:33 +0300166 return $this->_redis->incr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200167 }
168
169 // ------------------------------------------------------------------------
170
171 /**
172 * Decrement a raw value
173 *
174 * @param string $id Cache ID
175 * @param int $offset Step/value to reduce by
176 * @return mixed New value on success or FALSE on failure
177 */
178 public function decrement($id, $offset = 1)
179 {
Andrey Andreev70f738a2015-05-07 12:16:33 +0300180 return $this->_redis->decr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200181 }
182
183 // ------------------------------------------------------------------------
184
185 /**
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100186 * Clean cache
187 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300188 * @return bool
189 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100190 */
191 public function clean()
192 {
193 return $this->_redis->flushDB();
194 }
195
Andrey Andreev9e674f72012-06-09 21:02:52 +0300196 // ------------------------------------------------------------------------
197
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100198 /**
199 * Get cache driver info
200 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300201 * @param string Not supported in Redis.
202 * Only included in order to offer a
203 * consistent cache API.
204 * @return array
205 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100206 */
207 public function cache_info($type = NULL)
208 {
209 return $this->_redis->info();
210 }
211
Andrey Andreev9e674f72012-06-09 21:02:52 +0300212 // ------------------------------------------------------------------------
213
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100214 /**
215 * Get cache metadata
216 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300217 * @param string Cache key
218 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100219 */
220 public function get_metadata($key)
221 {
222 $value = $this->get($key);
223
ftwbzhaob2119a72015-04-09 13:27:01 +0800224 if ($value !== FALSE)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300225 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100226 return array(
227 'expire' => time() + $this->_redis->ttl($key),
228 'data' => $value
229 );
230 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300231
232 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100233 }
234
Andrey Andreev9e674f72012-06-09 21:02:52 +0300235 // ------------------------------------------------------------------------
236
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100237 /**
238 * Check if Redis driver is supported
239 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300240 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100241 */
242 public function is_supported()
243 {
Fieahbc834c32015-02-22 17:08:35 +0800244 if ( ! extension_loaded('redis'))
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100245 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400246 log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100247 return FALSE;
248 }
Fieahbc834c32015-02-22 17:08:35 +0800249
250 return $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100251 }
252
Andrey Andreev9e674f72012-06-09 21:02:52 +0300253 // ------------------------------------------------------------------------
254
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100255 /**
256 * Setup Redis config and connection
257 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300258 * Loads Redis config file if present. Will halt execution
259 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100260 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300261 * @return bool
262 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100263 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200264 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100265 {
266 $config = array();
267 $CI =& get_instance();
268
269 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200270 {
ftwbzhaoabd713f2015-04-09 13:11:51 +0800271 $config = $CI->config->item('redis');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100272 }
273
274 $config = array_merge(self::$_default_config, $config);
275
276 $this->_redis = new Redis();
277
278 try
279 {
Kakysha80d663a2013-10-28 01:39:17 +0400280 if ($config['socket_type'] === 'unix')
281 {
Kakyshaffe21302013-10-28 21:30:05 +0400282 $success = $this->_redis->connect($config['socket']);
Kakysha8f3f1f92013-10-28 23:14:08 +0400283 }
284 else // tcp socket
Kakysha80d663a2013-10-28 01:39:17 +0400285 {
Kakyshaffe21302013-10-28 21:30:05 +0400286 $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
Kakysha2d146732013-10-28 20:20:24 +0400287 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200288
Kakyshaffe21302013-10-28 21:30:05 +0400289 if ( ! $success)
Kakysha2d146732013-10-28 20:20:24 +0400290 {
Kakysha333b69b2013-10-29 03:49:56 +0400291 log_message('debug', 'Cache: Redis connection refused. Check the config.');
Kakysha2d146732013-10-28 20:20:24 +0400292 return FALSE;
Kakysha80d663a2013-10-28 01:39:17 +0400293 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100294 }
295 catch (RedisException $e)
296 {
Kakysha333b69b2013-10-29 03:49:56 +0400297 log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
Kakysha2d146732013-10-28 20:20:24 +0400298 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100299 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200300
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200301 if (isset($config['password']))
302 {
ftwbzhao9b9a06c2015-04-07 18:36:51 +0800303 if ( ! $this->_redis->auth($config['password']))
304 {
305 log_message('debug', 'Cache: Redis authentication failed.');
306 return FALSE;
307 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200308 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200309
Ivan Tcholakovd245f062014-08-18 13:52:44 +0300310 // Initialize the index of serialized values.
Andrey Andreev2492b502014-08-18 16:20:05 +0300311 $serialized = $this->_redis->sMembers('_ci_redis_serialized');
312 if ( ! empty($serialized))
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300313 {
Andrey Andreev1b634f82014-08-18 16:21:11 +0300314 $this->_serialized = array_flip($serialized);
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300315 }
316
Kakysha2d146732013-10-28 20:20:24 +0400317 return TRUE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100318 }
319
Andrey Andreev9e674f72012-06-09 21:02:52 +0300320 // ------------------------------------------------------------------------
321
322 /**
Andrey Andreev9e674f72012-06-09 21:02:52 +0300323 * 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 Lindqvist1e8be292012-01-21 12:25:08 +0100337}