blob: 26de61db8336c1571708431bf52bde739f73c01c [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 *
Anton Lindqvist5a1d9532012-01-23 23:20:26 +01007 * 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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Anton Lindqvist5a1d9532012-01-23 23:20:26 +010022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
Andrey Andreev9e674f72012-06-09 21:02:52 +030024 * @since Version 3.0
Anton Lindqvist1e8be292012-01-21 12:25:08 +010025 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Anton Lindqvist1e8be292012-01-21 12:25:08 +010028
Anton Lindqvist1e8be292012-01-21 12:25:08 +010029/**
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 */
38class CI_Cache_redis extends CI_Driver
39{
Anton Lindqvist1e8be292012-01-21 12:25:08 +010040 /**
41 * Default config
42 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010043 * @static
Andrey Andreev9e674f72012-06-09 21:02:52 +030044 * @var array
Anton Lindqvist1e8be292012-01-21 12:25:08 +010045 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010046 protected static $_default_config = array(
Kakysha80d663a2013-10-28 01:39:17 +040047 'socket_type' => 'tcp',
Anton Lindqvist1e8be292012-01-21 12:25:08 +010048 'host' => '127.0.0.1',
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +020049 'password' => NULL,
Anton Lindqvist1e8be292012-01-21 12:25:08 +010050 'port' => 6379,
51 'timeout' => 0
52 );
53
54 /**
55 * Redis connection
56 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030057 * @var Redis
Anton Lindqvist1e8be292012-01-21 12:25:08 +010058 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010059 protected $_redis;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010060
Ivan Tcholakov927e5082014-08-14 04:07:39 +030061 /**
62 * An internal cache for storing keys of serialized values.
63 *
64 * @var array
65 */
66 protected $_serialized;
67
Andrey Andreev9e674f72012-06-09 21:02:52 +030068 // ------------------------------------------------------------------------
Anton Lindqvist1e8be292012-01-21 12:25:08 +010069
70 /**
71 * Get cache
72 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020073 * @param string Cache ID
Andrey Andreev9e674f72012-06-09 21:02:52 +030074 * @return mixed
Anton Lindqvist1e8be292012-01-21 12:25:08 +010075 */
76 public function get($key)
77 {
Ivan Tcholakov927e5082014-08-14 04:07:39 +030078 $value = $this->_redis->get($key);
79
Ivan Tcholakove838c832014-08-18 09:56:05 +030080 if ($value !== FALSE && in_array($key, $this->_serialized, TRUE))
Ivan Tcholakov927e5082014-08-14 04:07:39 +030081 {
82 return unserialize($value);
83 }
84
85 return $value;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010086 }
87
Andrey Andreev9e674f72012-06-09 21:02:52 +030088 // ------------------------------------------------------------------------
89
Anton Lindqvist1e8be292012-01-21 12:25:08 +010090 /**
91 * Save cache
92 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020093 * @param string $id Cache ID
94 * @param mixed $data Data to save
95 * @param int $ttl Time to live in seconds
96 * @param bool $raw Whether to store the raw value (unused)
97 * @return bool TRUE on success, FALSE on failure
Anton Lindqvist1e8be292012-01-21 12:25:08 +010098 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020099 public function save($id, $data, $ttl = 60, $raw = FALSE)
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100100 {
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300101 if (is_array($data) OR is_object($data))
102 {
103 $data = serialize($data);
104
Ivan Tcholakovc773a482014-08-18 10:02:49 +0300105 if (($index_key = array_search($id, $this->_serialized, TRUE)) === FALSE)
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300106 {
107 $this->_serialized[] = $id;
108 }
109
Ivan Tcholakov7c835572014-08-18 10:37:29 +0300110 $this->_redis->sAdd('_ci_redis_serialized', $id);
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300111 }
112 else
113 {
Ivan Tcholakovc773a482014-08-18 10:02:49 +0300114 if (($index_key = array_search($id, $this->_serialized, TRUE)) !== FALSE)
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300115 {
116 unset($this->_serialized[$index_key]);
Ivan Tcholakov52b32532014-08-18 12:30:04 +0300117 $this->_redis->sRemove('_ci_redis_serialized', $id);
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300118 }
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300119 }
120
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100121 return ($ttl)
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200122 ? $this->_redis->setex($id, $ttl, $data)
123 : $this->_redis->set($id, $data);
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100124 }
125
Andrey Andreev9e674f72012-06-09 21:02:52 +0300126 // ------------------------------------------------------------------------
127
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100128 /**
129 * Delete from cache
130 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300131 * @param string Cache key
132 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100133 */
134 public function delete($key)
135 {
Ivan Tcholakovd514d5c2014-08-18 12:04:27 +0300136 if ($result = ($this->_redis->delete($key) === 1))
137 {
138 // This is for not leaving garbage keys within the Redis auxilary set.
139 $this->_redis->sRemove('_ci_redis_serialized', $key);
140 }
Ivan Tcholakovbc417612014-08-18 11:11:39 +0300141
Ivan Tcholakovd514d5c2014-08-18 12:04:27 +0300142 return $result;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100143 }
144
Andrey Andreev9e674f72012-06-09 21:02:52 +0300145 // ------------------------------------------------------------------------
146
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100147 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200148 * Increment a raw value
149 *
150 * @param string $id Cache ID
151 * @param int $offset Step/value to add
152 * @return mixed New value on success or FALSE on failure
153 */
154 public function increment($id, $offset = 1)
155 {
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300156 return $this->_redis->incr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200157 }
158
159 // ------------------------------------------------------------------------
160
161 /**
162 * Decrement a raw value
163 *
164 * @param string $id Cache ID
165 * @param int $offset Step/value to reduce by
166 * @return mixed New value on success or FALSE on failure
167 */
168 public function decrement($id, $offset = 1)
169 {
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300170 return $this->_redis->decr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200171 }
172
173 // ------------------------------------------------------------------------
174
175 /**
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100176 * Clean cache
177 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300178 * @return bool
179 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100180 */
181 public function clean()
182 {
183 return $this->_redis->flushDB();
184 }
185
Andrey Andreev9e674f72012-06-09 21:02:52 +0300186 // ------------------------------------------------------------------------
187
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100188 /**
189 * Get cache driver info
190 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300191 * @param string Not supported in Redis.
192 * Only included in order to offer a
193 * consistent cache API.
194 * @return array
195 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100196 */
197 public function cache_info($type = NULL)
198 {
199 return $this->_redis->info();
200 }
201
Andrey Andreev9e674f72012-06-09 21:02:52 +0300202 // ------------------------------------------------------------------------
203
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100204 /**
205 * Get cache metadata
206 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300207 * @param string Cache key
208 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100209 */
210 public function get_metadata($key)
211 {
212 $value = $this->get($key);
213
214 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300215 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100216 return array(
217 'expire' => time() + $this->_redis->ttl($key),
218 'data' => $value
219 );
220 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300221
222 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100223 }
224
Andrey Andreev9e674f72012-06-09 21:02:52 +0300225 // ------------------------------------------------------------------------
226
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100227 /**
228 * Check if Redis driver is supported
229 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300230 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100231 */
232 public function is_supported()
233 {
234 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300235 {
Kakysha2d146732013-10-28 20:20:24 +0400236 return $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100237 }
238 else
239 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400240 log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100241 return FALSE;
242 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100243 }
244
Andrey Andreev9e674f72012-06-09 21:02:52 +0300245 // ------------------------------------------------------------------------
246
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100247 /**
248 * Setup Redis config and connection
249 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300250 * Loads Redis config file if present. Will halt execution
251 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100252 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300253 * @return bool
254 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100255 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200256 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100257 {
258 $config = array();
259 $CI =& get_instance();
260
261 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200262 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100263 $config += $CI->config->item('redis');
264 }
265
266 $config = array_merge(self::$_default_config, $config);
267
268 $this->_redis = new Redis();
269
270 try
271 {
Kakysha80d663a2013-10-28 01:39:17 +0400272 if ($config['socket_type'] === 'unix')
273 {
Kakyshaffe21302013-10-28 21:30:05 +0400274 $success = $this->_redis->connect($config['socket']);
Kakysha8f3f1f92013-10-28 23:14:08 +0400275 }
276 else // tcp socket
Kakysha80d663a2013-10-28 01:39:17 +0400277 {
Kakyshaffe21302013-10-28 21:30:05 +0400278 $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
Kakysha2d146732013-10-28 20:20:24 +0400279 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200280
Kakyshaffe21302013-10-28 21:30:05 +0400281 if ( ! $success)
Kakysha2d146732013-10-28 20:20:24 +0400282 {
Kakysha333b69b2013-10-29 03:49:56 +0400283 log_message('debug', 'Cache: Redis connection refused. Check the config.');
Kakysha2d146732013-10-28 20:20:24 +0400284 return FALSE;
Kakysha80d663a2013-10-28 01:39:17 +0400285 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100286 }
287 catch (RedisException $e)
288 {
Kakysha333b69b2013-10-29 03:49:56 +0400289 log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
Kakysha2d146732013-10-28 20:20:24 +0400290 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100291 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200292
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200293 if (isset($config['password']))
294 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200295 $this->_redis->auth($config['password']);
296 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200297
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300298 // Initialize the index of selialized values.
Ivan Tcholakov7c835572014-08-18 10:37:29 +0300299 $this->_serialized = $this->_redis->sMembers('_ci_redis_serialized');
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300300
301 if (empty($this->_serialized))
302 {
303 // On error FALSE is returned, ensure array type for empty index.
304 $this->_serialized = array();
305 }
306
Kakysha2d146732013-10-28 20:20:24 +0400307 return TRUE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100308 }
309
Andrey Andreev9e674f72012-06-09 21:02:52 +0300310 // ------------------------------------------------------------------------
311
312 /**
Andrey Andreev9e674f72012-06-09 21:02:52 +0300313 * 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 }
326
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100327}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100328
329/* End of file Cache_redis.php */
330/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */