blob: b6fddf035bf032708727a971318e51e0a73df8e0 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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
Andrey Andreev9e674f72012-06-09 21:02:52 +030061 // ------------------------------------------------------------------------
Anton Lindqvist1e8be292012-01-21 12:25:08 +010062
63 /**
64 * Get cache
65 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020066 * @param string Cache ID
Andrey Andreev9e674f72012-06-09 21:02:52 +030067 * @return mixed
Anton Lindqvist1e8be292012-01-21 12:25:08 +010068 */
69 public function get($key)
70 {
71 return $this->_redis->get($key);
72 }
73
Andrey Andreev9e674f72012-06-09 21:02:52 +030074 // ------------------------------------------------------------------------
75
Anton Lindqvist1e8be292012-01-21 12:25:08 +010076 /**
77 * Save cache
78 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020079 * @param string $id Cache ID
80 * @param mixed $data Data to save
81 * @param int $ttl Time to live in seconds
82 * @param bool $raw Whether to store the raw value (unused)
83 * @return bool TRUE on success, FALSE on failure
Anton Lindqvist1e8be292012-01-21 12:25:08 +010084 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020085 public function save($id, $data, $ttl = 60, $raw = FALSE)
Anton Lindqvist1e8be292012-01-21 12:25:08 +010086 {
87 return ($ttl)
Andrey Andreev43d7fa72014-01-09 17:29:45 +020088 ? $this->_redis->setex($id, $ttl, $data)
89 : $this->_redis->set($id, $data);
Anton Lindqvist1e8be292012-01-21 12:25:08 +010090 }
91
Andrey Andreev9e674f72012-06-09 21:02:52 +030092 // ------------------------------------------------------------------------
93
Anton Lindqvist1e8be292012-01-21 12:25:08 +010094 /**
95 * Delete from cache
96 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030097 * @param string Cache key
98 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010099 */
100 public function delete($key)
101 {
102 return ($this->_redis->delete($key) === 1);
103 }
104
Andrey Andreev9e674f72012-06-09 21:02:52 +0300105 // ------------------------------------------------------------------------
106
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100107 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200108 * Increment a raw value
109 *
110 * @param string $id Cache ID
111 * @param int $offset Step/value to add
112 * @return mixed New value on success or FALSE on failure
113 */
114 public function increment($id, $offset = 1)
115 {
116 return $this->_redis->exists($id)
117 ? $this->_redis->incr($id, $offset)
118 : FALSE;
119 }
120
121 // ------------------------------------------------------------------------
122
123 /**
124 * Decrement a raw value
125 *
126 * @param string $id Cache ID
127 * @param int $offset Step/value to reduce by
128 * @return mixed New value on success or FALSE on failure
129 */
130 public function decrement($id, $offset = 1)
131 {
132 return $this->_redis->exists($id)
133 ? $this->_redis->decr($id, $offset)
134 : FALSE;
135 }
136
137 // ------------------------------------------------------------------------
138
139 /**
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100140 * Clean cache
141 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300142 * @return bool
143 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100144 */
145 public function clean()
146 {
147 return $this->_redis->flushDB();
148 }
149
Andrey Andreev9e674f72012-06-09 21:02:52 +0300150 // ------------------------------------------------------------------------
151
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100152 /**
153 * Get cache driver info
154 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300155 * @param string Not supported in Redis.
156 * Only included in order to offer a
157 * consistent cache API.
158 * @return array
159 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100160 */
161 public function cache_info($type = NULL)
162 {
163 return $this->_redis->info();
164 }
165
Andrey Andreev9e674f72012-06-09 21:02:52 +0300166 // ------------------------------------------------------------------------
167
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100168 /**
169 * Get cache metadata
170 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300171 * @param string Cache key
172 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100173 */
174 public function get_metadata($key)
175 {
176 $value = $this->get($key);
177
178 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300179 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100180 return array(
181 'expire' => time() + $this->_redis->ttl($key),
182 'data' => $value
183 );
184 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300185
186 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100187 }
188
Andrey Andreev9e674f72012-06-09 21:02:52 +0300189 // ------------------------------------------------------------------------
190
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100191 /**
192 * Check if Redis driver is supported
193 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300194 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100195 */
196 public function is_supported()
197 {
198 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300199 {
Kakysha2d146732013-10-28 20:20:24 +0400200 return $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100201 }
202 else
203 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400204 log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100205 return FALSE;
206 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100207 }
208
Andrey Andreev9e674f72012-06-09 21:02:52 +0300209 // ------------------------------------------------------------------------
210
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100211 /**
212 * Setup Redis config and connection
213 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300214 * Loads Redis config file if present. Will halt execution
215 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100216 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300217 * @return bool
218 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100219 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200220 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100221 {
222 $config = array();
223 $CI =& get_instance();
224
225 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200226 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100227 $config += $CI->config->item('redis');
228 }
229
230 $config = array_merge(self::$_default_config, $config);
231
232 $this->_redis = new Redis();
233
234 try
235 {
Kakysha80d663a2013-10-28 01:39:17 +0400236 if ($config['socket_type'] === 'unix')
237 {
Kakyshaffe21302013-10-28 21:30:05 +0400238 $success = $this->_redis->connect($config['socket']);
Kakysha8f3f1f92013-10-28 23:14:08 +0400239 }
240 else // tcp socket
Kakysha80d663a2013-10-28 01:39:17 +0400241 {
Kakyshaffe21302013-10-28 21:30:05 +0400242 $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
Kakysha2d146732013-10-28 20:20:24 +0400243 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200244
Kakyshaffe21302013-10-28 21:30:05 +0400245 if ( ! $success)
Kakysha2d146732013-10-28 20:20:24 +0400246 {
Kakysha333b69b2013-10-29 03:49:56 +0400247 log_message('debug', 'Cache: Redis connection refused. Check the config.');
Kakysha2d146732013-10-28 20:20:24 +0400248 return FALSE;
Kakysha80d663a2013-10-28 01:39:17 +0400249 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100250 }
251 catch (RedisException $e)
252 {
Kakysha333b69b2013-10-29 03:49:56 +0400253 log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
Kakysha2d146732013-10-28 20:20:24 +0400254 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100255 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200256
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200257 if (isset($config['password']))
258 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200259 $this->_redis->auth($config['password']);
260 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200261
Kakysha2d146732013-10-28 20:20:24 +0400262 return TRUE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100263 }
264
Andrey Andreev9e674f72012-06-09 21:02:52 +0300265 // ------------------------------------------------------------------------
266
267 /**
268
269 * Class destructor
270 *
271 * Closes the connection to Redis if present.
272 *
273 * @return void
274 */
275 public function __destruct()
276 {
277 if ($this->_redis)
278 {
279 $this->_redis->close();
280 }
281 }
282
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100283}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100284
285/* End of file Cache_redis.php */
286/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */