blob: e4a26b5f05b8ba4ab0a86fc2e320bfd89f28a80d [file] [log] [blame]
Anton Lindqvist1e8be292012-01-21 12:25:08 +01001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
21 * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
22 * @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 */
27
Anton Lindqvist1e8be292012-01-21 12:25:08 +010028/**
29 * CodeIgniter Redis Caching Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Core
34 * @author Anton Lindqvist <anton@qvister.se>
35 * @link
36 */
37class CI_Cache_redis extends CI_Driver
38{
Anton Lindqvist1e8be292012-01-21 12:25:08 +010039 /**
40 * Default config
41 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010042 * @static
Andrey Andreev9e674f72012-06-09 21:02:52 +030043 * @var array
Anton Lindqvist1e8be292012-01-21 12:25:08 +010044 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010045 protected static $_default_config = array(
Anton Lindqvist1e8be292012-01-21 12:25:08 +010046 'host' => '127.0.0.1',
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +020047 'password' => NULL,
Anton Lindqvist1e8be292012-01-21 12:25:08 +010048 'port' => 6379,
49 'timeout' => 0
50 );
51
52 /**
53 * Redis connection
54 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030055 * @var Redis
Anton Lindqvist1e8be292012-01-21 12:25:08 +010056 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010057 protected $_redis;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010058
Andrey Andreev9e674f72012-06-09 21:02:52 +030059 // ------------------------------------------------------------------------
Anton Lindqvist1e8be292012-01-21 12:25:08 +010060
61 /**
62 * Get cache
63 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030064 * @param string Cache key identifier
65 * @return mixed
Anton Lindqvist1e8be292012-01-21 12:25:08 +010066 */
67 public function get($key)
68 {
69 return $this->_redis->get($key);
70 }
71
Andrey Andreev9e674f72012-06-09 21:02:52 +030072 // ------------------------------------------------------------------------
73
Anton Lindqvist1e8be292012-01-21 12:25:08 +010074 /**
75 * Save cache
76 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030077 * @param string Cache key identifier
78 * @param mixed Data to save
79 * @param int Time to live
80 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010081 */
82 public function save($key, $value, $ttl = NULL)
83 {
84 return ($ttl)
85 ? $this->_redis->setex($key, $ttl, $value)
86 : $this->_redis->set($key, $value);
87 }
88
Andrey Andreev9e674f72012-06-09 21:02:52 +030089 // ------------------------------------------------------------------------
90
Anton Lindqvist1e8be292012-01-21 12:25:08 +010091 /**
92 * Delete from cache
93 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030094 * @param string Cache key
95 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010096 */
97 public function delete($key)
98 {
99 return ($this->_redis->delete($key) === 1);
100 }
101
Andrey Andreev9e674f72012-06-09 21:02:52 +0300102 // ------------------------------------------------------------------------
103
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100104 /**
105 * Clean cache
106 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300107 * @return bool
108 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100109 */
110 public function clean()
111 {
112 return $this->_redis->flushDB();
113 }
114
Andrey Andreev9e674f72012-06-09 21:02:52 +0300115 // ------------------------------------------------------------------------
116
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100117 /**
118 * Get cache driver info
119 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300120 * @param string Not supported in Redis.
121 * Only included in order to offer a
122 * consistent cache API.
123 * @return array
124 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100125 */
126 public function cache_info($type = NULL)
127 {
128 return $this->_redis->info();
129 }
130
Andrey Andreev9e674f72012-06-09 21:02:52 +0300131 // ------------------------------------------------------------------------
132
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100133 /**
134 * Get cache metadata
135 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300136 * @param string Cache key
137 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100138 */
139 public function get_metadata($key)
140 {
141 $value = $this->get($key);
142
143 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300144 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100145 return array(
146 'expire' => time() + $this->_redis->ttl($key),
147 'data' => $value
148 );
149 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300150
151 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100152 }
153
Andrey Andreev9e674f72012-06-09 21:02:52 +0300154 // ------------------------------------------------------------------------
155
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100156 /**
157 * Check if Redis driver is supported
158 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300159 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100160 */
161 public function is_supported()
162 {
163 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300164 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100165 $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100166 return TRUE;
167 }
168 else
169 {
Anton Lindqvist3573af82012-01-21 20:33:12 +0100170 log_message('error', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100171 return FALSE;
172 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100173 }
174
Andrey Andreev9e674f72012-06-09 21:02:52 +0300175 // ------------------------------------------------------------------------
176
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100177 /**
178 * Setup Redis config and connection
179 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300180 * Loads Redis config file if present. Will halt execution
181 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100182 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300183 * @return bool
184 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100185 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200186 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100187 {
188 $config = array();
189 $CI =& get_instance();
190
191 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200192 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100193 $config += $CI->config->item('redis');
194 }
195
196 $config = array_merge(self::$_default_config, $config);
197
198 $this->_redis = new Redis();
199
200 try
201 {
202 $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
203 }
204 catch (RedisException $e)
205 {
206 show_error('Redis connection refused. ' . $e->getMessage());
207 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200208
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200209 if (isset($config['password']))
210 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200211 $this->_redis->auth($config['password']);
212 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100213 }
214
Andrey Andreev9e674f72012-06-09 21:02:52 +0300215 // ------------------------------------------------------------------------
216
217 /**
218
219 * Class destructor
220 *
221 * Closes the connection to Redis if present.
222 *
223 * @return void
224 */
225 public function __destruct()
226 {
227 if ($this->_redis)
228 {
229 $this->_redis->close();
230 }
231 }
232
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100233}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100234
235/* End of file Cache_redis.php */
236/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */