blob: 991cee0ccf24ea2abd6e9e30bccdcd15e8c6ec93 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Anton Lindqvist1e8be292012-01-21 12:25:08 +01002/**
3 * CodeIgniter
4 *
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +02005 * An open source application development framework for PHP 5.2.4 or newer
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @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 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300114 if ( ! $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
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100128 return ($ttl)
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200129 ? $this->_redis->setex($id, $ttl, $data)
130 : $this->_redis->set($id, $data);
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100131 }
132
Andrey Andreev9e674f72012-06-09 21:02:52 +0300133 // ------------------------------------------------------------------------
134
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100135 /**
136 * Delete from cache
137 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300138 * @param string Cache key
139 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100140 */
141 public function delete($key)
142 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300143 if ($this->_redis->delete($key) !== 1)
Ivan Tcholakovd514d5c2014-08-18 12:04:27 +0300144 {
Andrey Andreev2492b502014-08-18 16:20:05 +0300145 return FALSE;
Ivan Tcholakovd514d5c2014-08-18 12:04:27 +0300146 }
Ivan Tcholakovbc417612014-08-18 11:11:39 +0300147
Andrey Andreev2492b502014-08-18 16:20:05 +0300148 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 Lindqvist1e8be292012-01-21 12:25:08 +0100155 }
156
Andrey Andreev9e674f72012-06-09 21:02:52 +0300157 // ------------------------------------------------------------------------
158
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100159 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200160 * 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 Andreev5f0799a2014-07-31 13:32:41 +0300168 return $this->_redis->incr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200169 }
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 Andreev5f0799a2014-07-31 13:32:41 +0300182 return $this->_redis->decr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200183 }
184
185 // ------------------------------------------------------------------------
186
187 /**
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100188 * Clean cache
189 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300190 * @return bool
191 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100192 */
193 public function clean()
194 {
195 return $this->_redis->flushDB();
196 }
197
Andrey Andreev9e674f72012-06-09 21:02:52 +0300198 // ------------------------------------------------------------------------
199
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100200 /**
201 * Get cache driver info
202 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300203 * @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 Lindqvist1e8be292012-01-21 12:25:08 +0100208 */
209 public function cache_info($type = NULL)
210 {
211 return $this->_redis->info();
212 }
213
Andrey Andreev9e674f72012-06-09 21:02:52 +0300214 // ------------------------------------------------------------------------
215
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100216 /**
217 * Get cache metadata
218 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300219 * @param string Cache key
220 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100221 */
222 public function get_metadata($key)
223 {
224 $value = $this->get($key);
225
226 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300227 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100228 return array(
229 'expire' => time() + $this->_redis->ttl($key),
230 'data' => $value
231 );
232 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300233
234 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100235 }
236
Andrey Andreev9e674f72012-06-09 21:02:52 +0300237 // ------------------------------------------------------------------------
238
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100239 /**
240 * Check if Redis driver is supported
241 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300242 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100243 */
244 public function is_supported()
245 {
246 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300247 {
Kakysha2d146732013-10-28 20:20:24 +0400248 return $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100249 }
250 else
251 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400252 log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100253 return FALSE;
254 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100255 }
256
Andrey Andreev9e674f72012-06-09 21:02:52 +0300257 // ------------------------------------------------------------------------
258
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100259 /**
260 * Setup Redis config and connection
261 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300262 * Loads Redis config file if present. Will halt execution
263 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100264 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300265 * @return bool
266 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100267 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200268 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100269 {
270 $config = array();
271 $CI =& get_instance();
272
273 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200274 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100275 $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 {
Kakysha80d663a2013-10-28 01:39:17 +0400284 if ($config['socket_type'] === 'unix')
285 {
Kakyshaffe21302013-10-28 21:30:05 +0400286 $success = $this->_redis->connect($config['socket']);
Kakysha8f3f1f92013-10-28 23:14:08 +0400287 }
288 else // tcp socket
Kakysha80d663a2013-10-28 01:39:17 +0400289 {
Kakyshaffe21302013-10-28 21:30:05 +0400290 $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
Kakysha2d146732013-10-28 20:20:24 +0400291 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200292
Kakyshaffe21302013-10-28 21:30:05 +0400293 if ( ! $success)
Kakysha2d146732013-10-28 20:20:24 +0400294 {
Kakysha333b69b2013-10-29 03:49:56 +0400295 log_message('debug', 'Cache: Redis connection refused. Check the config.');
Kakysha2d146732013-10-28 20:20:24 +0400296 return FALSE;
Kakysha80d663a2013-10-28 01:39:17 +0400297 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100298 }
299 catch (RedisException $e)
300 {
Kakysha333b69b2013-10-29 03:49:56 +0400301 log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
Kakysha2d146732013-10-28 20:20:24 +0400302 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100303 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200304
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200305 if (isset($config['password']))
306 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200307 $this->_redis->auth($config['password']);
308 }
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}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100338
339/* End of file Cache_redis.php */
340/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */