blob: 21ef9919d39df72660ec797ecb190ebfa8877d26 [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]);
117 }
118
Ivan Tcholakov7c835572014-08-18 10:37:29 +0300119 $this->_redis->sRemove('_ci_redis_serialized', $id);
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300120 }
121
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100122 return ($ttl)
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200123 ? $this->_redis->setex($id, $ttl, $data)
124 : $this->_redis->set($id, $data);
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100125 }
126
Andrey Andreev9e674f72012-06-09 21:02:52 +0300127 // ------------------------------------------------------------------------
128
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100129 /**
130 * Delete from cache
131 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300132 * @param string Cache key
133 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100134 */
135 public function delete($key)
136 {
137 return ($this->_redis->delete($key) === 1);
138 }
139
Andrey Andreev9e674f72012-06-09 21:02:52 +0300140 // ------------------------------------------------------------------------
141
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100142 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200143 * Increment a raw value
144 *
145 * @param string $id Cache ID
146 * @param int $offset Step/value to add
147 * @return mixed New value on success or FALSE on failure
148 */
149 public function increment($id, $offset = 1)
150 {
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300151 return $this->_redis->incr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200152 }
153
154 // ------------------------------------------------------------------------
155
156 /**
157 * Decrement a raw value
158 *
159 * @param string $id Cache ID
160 * @param int $offset Step/value to reduce by
161 * @return mixed New value on success or FALSE on failure
162 */
163 public function decrement($id, $offset = 1)
164 {
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300165 return $this->_redis->decr($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200166 }
167
168 // ------------------------------------------------------------------------
169
170 /**
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100171 * Clean cache
172 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300173 * @return bool
174 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100175 */
176 public function clean()
177 {
178 return $this->_redis->flushDB();
179 }
180
Andrey Andreev9e674f72012-06-09 21:02:52 +0300181 // ------------------------------------------------------------------------
182
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100183 /**
184 * Get cache driver info
185 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300186 * @param string Not supported in Redis.
187 * Only included in order to offer a
188 * consistent cache API.
189 * @return array
190 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100191 */
192 public function cache_info($type = NULL)
193 {
194 return $this->_redis->info();
195 }
196
Andrey Andreev9e674f72012-06-09 21:02:52 +0300197 // ------------------------------------------------------------------------
198
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100199 /**
200 * Get cache metadata
201 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300202 * @param string Cache key
203 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100204 */
205 public function get_metadata($key)
206 {
207 $value = $this->get($key);
208
209 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300210 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100211 return array(
212 'expire' => time() + $this->_redis->ttl($key),
213 'data' => $value
214 );
215 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300216
217 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100218 }
219
Andrey Andreev9e674f72012-06-09 21:02:52 +0300220 // ------------------------------------------------------------------------
221
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100222 /**
223 * Check if Redis driver is supported
224 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300225 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100226 */
227 public function is_supported()
228 {
229 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300230 {
Kakysha2d146732013-10-28 20:20:24 +0400231 return $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100232 }
233 else
234 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400235 log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100236 return FALSE;
237 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100238 }
239
Andrey Andreev9e674f72012-06-09 21:02:52 +0300240 // ------------------------------------------------------------------------
241
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100242 /**
243 * Setup Redis config and connection
244 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300245 * Loads Redis config file if present. Will halt execution
246 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100247 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300248 * @return bool
249 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100250 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200251 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100252 {
253 $config = array();
254 $CI =& get_instance();
255
256 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200257 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100258 $config += $CI->config->item('redis');
259 }
260
261 $config = array_merge(self::$_default_config, $config);
262
263 $this->_redis = new Redis();
264
265 try
266 {
Kakysha80d663a2013-10-28 01:39:17 +0400267 if ($config['socket_type'] === 'unix')
268 {
Kakyshaffe21302013-10-28 21:30:05 +0400269 $success = $this->_redis->connect($config['socket']);
Kakysha8f3f1f92013-10-28 23:14:08 +0400270 }
271 else // tcp socket
Kakysha80d663a2013-10-28 01:39:17 +0400272 {
Kakyshaffe21302013-10-28 21:30:05 +0400273 $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
Kakysha2d146732013-10-28 20:20:24 +0400274 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200275
Kakyshaffe21302013-10-28 21:30:05 +0400276 if ( ! $success)
Kakysha2d146732013-10-28 20:20:24 +0400277 {
Kakysha333b69b2013-10-29 03:49:56 +0400278 log_message('debug', 'Cache: Redis connection refused. Check the config.');
Kakysha2d146732013-10-28 20:20:24 +0400279 return FALSE;
Kakysha80d663a2013-10-28 01:39:17 +0400280 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100281 }
282 catch (RedisException $e)
283 {
Kakysha333b69b2013-10-29 03:49:56 +0400284 log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
Kakysha2d146732013-10-28 20:20:24 +0400285 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100286 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200287
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200288 if (isset($config['password']))
289 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200290 $this->_redis->auth($config['password']);
291 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200292
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300293 // Initialize the index of selialized values.
Ivan Tcholakov7c835572014-08-18 10:37:29 +0300294 $this->_serialized = $this->_redis->sMembers('_ci_redis_serialized');
Ivan Tcholakov927e5082014-08-14 04:07:39 +0300295
296 if (empty($this->_serialized))
297 {
298 // On error FALSE is returned, ensure array type for empty index.
299 $this->_serialized = array();
300 }
301
Kakysha2d146732013-10-28 20:20:24 +0400302 return TRUE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100303 }
304
Andrey Andreev9e674f72012-06-09 21:02:52 +0300305 // ------------------------------------------------------------------------
306
307 /**
Andrey Andreev9e674f72012-06-09 21:02:52 +0300308 * Class destructor
309 *
310 * Closes the connection to Redis if present.
311 *
312 * @return void
313 */
314 public function __destruct()
315 {
316 if ($this->_redis)
317 {
318 $this->_redis->close();
319 }
320 }
321
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100322}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100323
324/* End of file Cache_redis.php */
325/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */