blob: 205f17cd1c9cb44f383d5f79b1f640b94f654218 [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
24 * @since Version 2.0
Anton Lindqvist1e8be292012-01-21 12:25:08 +010025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Redis Caching Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Core
36 * @author Anton Lindqvist <anton@qvister.se>
37 * @link
38 */
39class CI_Cache_redis extends CI_Driver
40{
41
42 /**
43 * Default config
44 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010045 * @static
46 * @var array
47 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010048 protected static $_default_config = array(
Anton Lindqvist1e8be292012-01-21 12:25:08 +010049 'host' => '127.0.0.1',
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +020050 'password' => NULL,
Anton Lindqvist1e8be292012-01-21 12:25:08 +010051 'port' => 6379,
52 'timeout' => 0
53 );
54
55 /**
56 * Redis connection
57 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010058 * @var Redis
59 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010060 protected $_redis;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010061
62 /**
63 * Class destructor
64 *
65 * Closes the connection to Redis if present.
66 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010067 * @return void
68 */
69 public function __destruct()
70 {
71 if ($this->_redis)
72 {
73 $this->_redis->close();
74 }
75 }
76
77 /**
78 * Get cache
79 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010080 * @param string $key Cache key identifier
81 * @return mixed
82 */
83 public function get($key)
84 {
85 return $this->_redis->get($key);
86 }
87
88 /**
89 * Save cache
90 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +010091 * @param string $key Cache key identifier
92 * @param mixed $value Data to save
93 * @param integer $ttl Time to live
94 * @return boolean
95 */
96 public function save($key, $value, $ttl = NULL)
97 {
98 return ($ttl)
99 ? $this->_redis->setex($key, $ttl, $value)
100 : $this->_redis->set($key, $value);
101 }
102
103 /**
104 * Delete from cache
105 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100106 * @param string $key Cache key
107 * @return boolean
108 */
109 public function delete($key)
110 {
111 return ($this->_redis->delete($key) === 1);
112 }
113
114 /**
115 * Clean cache
116 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100117 * @return boolean
118 * @see Redis::flushDB()
119 */
120 public function clean()
121 {
122 return $this->_redis->flushDB();
123 }
124
125 /**
126 * Get cache driver info
127 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100128 * @param string $type Not supported in Redis. Only included in order to offer a
129 * consistent cache API.
130 * @return array
131 * @see Redis::info()
132 */
133 public function cache_info($type = NULL)
134 {
135 return $this->_redis->info();
136 }
137
138 /**
139 * Get cache metadata
140 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100141 * @param string $key Cache key
142 * @return array
143 */
144 public function get_metadata($key)
145 {
146 $value = $this->get($key);
147
148 if ($value)
149 {
150 return array(
151 'expire' => time() + $this->_redis->ttl($key),
152 'data' => $value
153 );
154 }
155 }
156
157 /**
158 * Check if Redis driver is supported
159 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100160 * @return boolean
161 */
162 public function is_supported()
163 {
164 if (extension_loaded('redis'))
165 {
166 $this->_setup_redis();
167
168 return TRUE;
169 }
170 else
171 {
Anton Lindqvist3573af82012-01-21 20:33:12 +0100172 log_message('error', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100173
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 *
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100185 * @return void
186 * @see Redis::connect()
187 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200188 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100189 {
190 $config = array();
191 $CI =& get_instance();
192
193 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200194 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100195 $config += $CI->config->item('redis');
196 }
197
198 $config = array_merge(self::$_default_config, $config);
199
200 $this->_redis = new Redis();
201
202 try
203 {
204 $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
205 }
206 catch (RedisException $e)
207 {
208 show_error('Redis connection refused. ' . $e->getMessage());
209 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200210
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200211 if (isset($config['password']))
212 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200213 $this->_redis->auth($config['password']);
214 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100215 }
216
217}
218// End Class
219
220/* End of file Cache_redis.php */
221/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */