blob: 60033344b336b3cc8c3e1c05eae9da7592bd5b77 [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 Andreevc5536aa2012-11-01 17:33:58 +020021 * @copyright Copyright (c) 2008 - 2012, 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(
Anton Lindqvist1e8be292012-01-21 12:25:08 +010047 'host' => '127.0.0.1',
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +020048 'password' => NULL,
Anton Lindqvist1e8be292012-01-21 12:25:08 +010049 'port' => 6379,
50 'timeout' => 0
51 );
52
53 /**
54 * Redis connection
55 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030056 * @var Redis
Anton Lindqvist1e8be292012-01-21 12:25:08 +010057 */
Anton Lindqvist3573af82012-01-21 20:33:12 +010058 protected $_redis;
Anton Lindqvist1e8be292012-01-21 12:25:08 +010059
Andrey Andreev9e674f72012-06-09 21:02:52 +030060 // ------------------------------------------------------------------------
Anton Lindqvist1e8be292012-01-21 12:25:08 +010061
62 /**
63 * Get cache
64 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030065 * @param string Cache key identifier
66 * @return mixed
Anton Lindqvist1e8be292012-01-21 12:25:08 +010067 */
68 public function get($key)
69 {
70 return $this->_redis->get($key);
71 }
72
Andrey Andreev9e674f72012-06-09 21:02:52 +030073 // ------------------------------------------------------------------------
74
Anton Lindqvist1e8be292012-01-21 12:25:08 +010075 /**
76 * Save cache
77 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030078 * @param string Cache key identifier
79 * @param mixed Data to save
80 * @param int Time to live
81 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010082 */
83 public function save($key, $value, $ttl = NULL)
84 {
85 return ($ttl)
86 ? $this->_redis->setex($key, $ttl, $value)
87 : $this->_redis->set($key, $value);
88 }
89
Andrey Andreev9e674f72012-06-09 21:02:52 +030090 // ------------------------------------------------------------------------
91
Anton Lindqvist1e8be292012-01-21 12:25:08 +010092 /**
93 * Delete from cache
94 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030095 * @param string Cache key
96 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010097 */
98 public function delete($key)
99 {
100 return ($this->_redis->delete($key) === 1);
101 }
102
Andrey Andreev9e674f72012-06-09 21:02:52 +0300103 // ------------------------------------------------------------------------
104
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100105 /**
106 * Clean cache
107 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300108 * @return bool
109 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100110 */
111 public function clean()
112 {
113 return $this->_redis->flushDB();
114 }
115
Andrey Andreev9e674f72012-06-09 21:02:52 +0300116 // ------------------------------------------------------------------------
117
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100118 /**
119 * Get cache driver info
120 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300121 * @param string Not supported in Redis.
122 * Only included in order to offer a
123 * consistent cache API.
124 * @return array
125 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100126 */
127 public function cache_info($type = NULL)
128 {
129 return $this->_redis->info();
130 }
131
Andrey Andreev9e674f72012-06-09 21:02:52 +0300132 // ------------------------------------------------------------------------
133
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100134 /**
135 * Get cache metadata
136 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300137 * @param string Cache key
138 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100139 */
140 public function get_metadata($key)
141 {
142 $value = $this->get($key);
143
144 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300145 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100146 return array(
147 'expire' => time() + $this->_redis->ttl($key),
148 'data' => $value
149 );
150 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300151
152 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100153 }
154
Andrey Andreev9e674f72012-06-09 21:02:52 +0300155 // ------------------------------------------------------------------------
156
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100157 /**
158 * Check if Redis driver is supported
159 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300160 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100161 */
162 public function is_supported()
163 {
164 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300165 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100166 $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100167 return TRUE;
168 }
169 else
170 {
Anton Lindqvist3573af82012-01-21 20:33:12 +0100171 log_message('error', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100172 return FALSE;
173 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100174 }
175
Andrey Andreev9e674f72012-06-09 21:02:52 +0300176 // ------------------------------------------------------------------------
177
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100178 /**
179 * Setup Redis config and connection
180 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300181 * Loads Redis config file if present. Will halt execution
182 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100183 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300184 * @return bool
185 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100186 */
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200187 protected function _setup_redis()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100188 {
189 $config = array();
190 $CI =& get_instance();
191
192 if ($CI->config->load('redis', TRUE, TRUE))
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200193 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100194 $config += $CI->config->item('redis');
195 }
196
197 $config = array_merge(self::$_default_config, $config);
198
199 $this->_redis = new Redis();
200
201 try
202 {
203 $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
204 }
205 catch (RedisException $e)
206 {
207 show_error('Redis connection refused. ' . $e->getMessage());
208 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200209
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200210 if (isset($config['password']))
211 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200212 $this->_redis->auth($config['password']);
213 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100214 }
215
Andrey Andreev9e674f72012-06-09 21:02:52 +0300216 // ------------------------------------------------------------------------
217
218 /**
219
220 * Class destructor
221 *
222 * Closes the connection to Redis if present.
223 *
224 * @return void
225 */
226 public function __destruct()
227 {
228 if ($this->_redis)
229 {
230 $this->_redis->close();
231 }
232 }
233
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100234}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100235
236/* End of file Cache_redis.php */
237/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */