blob: 9eb7a8d4ea956bf6f17e624cccaba3a08d1df8ab [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 2.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Redis Caching Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Core
24 * @author Anton Lindqvist <anton@qvister.se>
25 * @link
26 */
27class CI_Cache_redis extends CI_Driver
28{
29
30 /**
31 * Default config
32 *
33 * @access private
34 * @static
35 * @var array
36 */
37 private static $_default_config = array(
38 'host' => '127.0.0.1',
39 'port' => 6379,
40 'timeout' => 0
41 );
42
43 /**
44 * Redis connection
45 *
46 * @access private
47 * @var Redis
48 */
49 private $_redis;
50
51 /**
52 * Class destructor
53 *
54 * Closes the connection to Redis if present.
55 *
56 * @access public
57 * @return void
58 */
59 public function __destruct()
60 {
61 if ($this->_redis)
62 {
63 $this->_redis->close();
64 }
65 }
66
67 /**
68 * Get cache
69 *
70 * @access public
71 * @param string $key Cache key identifier
72 * @return mixed
73 */
74 public function get($key)
75 {
76 return $this->_redis->get($key);
77 }
78
79 /**
80 * Save cache
81 *
82 * @access public
83 * @param string $key Cache key identifier
84 * @param mixed $value Data to save
85 * @param integer $ttl Time to live
86 * @return boolean
87 */
88 public function save($key, $value, $ttl = NULL)
89 {
90 return ($ttl)
91 ? $this->_redis->setex($key, $ttl, $value)
92 : $this->_redis->set($key, $value);
93 }
94
95 /**
96 * Delete from cache
97 *
98 * @access public
99 * @param string $key Cache key
100 * @return boolean
101 */
102 public function delete($key)
103 {
104 return ($this->_redis->delete($key) === 1);
105 }
106
107 /**
108 * Clean cache
109 *
110 * @access public
111 * @return boolean
112 * @see Redis::flushDB()
113 */
114 public function clean()
115 {
116 return $this->_redis->flushDB();
117 }
118
119 /**
120 * Get cache driver info
121 *
122 * @access public
123 * @param string $type Not supported in Redis. Only included in order to offer a
124 * consistent cache API.
125 * @return array
126 * @see Redis::info()
127 */
128 public function cache_info($type = NULL)
129 {
130 return $this->_redis->info();
131 }
132
133 /**
134 * Get cache metadata
135 *
136 * @access public
137 * @param string $key Cache key
138 * @return array
139 */
140 public function get_metadata($key)
141 {
142 $value = $this->get($key);
143
144 if ($value)
145 {
146 return array(
147 'expire' => time() + $this->_redis->ttl($key),
148 'data' => $value
149 );
150 }
151 }
152
153 /**
154 * Check if Redis driver is supported
155 *
156 * @access public
157 * @return boolean
158 */
159 public function is_supported()
160 {
161 if (extension_loaded('redis'))
162 {
163 $this->_setup_redis();
164
165 return TRUE;
166 }
167 else
168 {
169 log_message(
170 'error',
171 'The Redis extension must be loaded to use Redis cache.'
172 );
173
174 return FALSE;
175 }
176
177 }
178
179 /**
180 * Setup Redis config and connection
181 *
182 * Loads Redis config file if present. Will halt execution if a Redis connection
183 * can't be established.
184 *
185 * @access private
186 * @return void
187 * @see Redis::connect()
188 */
189 private function _setup_redis()
190 {
191 $config = array();
192 $CI =& get_instance();
193
194 if ($CI->config->load('redis', TRUE, TRUE))
195 {
196 $config += $CI->config->item('redis');
197 }
198
199 $config = array_merge(self::$_default_config, $config);
200
201 $this->_redis = new Redis();
202
203 try
204 {
205 $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
206 }
207 catch (RedisException $e)
208 {
209 show_error('Redis connection refused. ' . $e->getMessage());
210 }
211 }
212
213}
214// End Class
215
216/* End of file Cache_redis.php */
217/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */