blob: 8daed8bc3e530130f516701505cfbec3ecff662f [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 Andreev9e674f72012-06-09 21:02:52 +030066 * @param string Cache key identifier
67 * @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 Andreev9e674f72012-06-09 21:02:52 +030079 * @param string Cache key identifier
80 * @param mixed Data to save
81 * @param int Time to live
82 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010083 */
84 public function save($key, $value, $ttl = NULL)
85 {
86 return ($ttl)
87 ? $this->_redis->setex($key, $ttl, $value)
88 : $this->_redis->set($key, $value);
89 }
90
Andrey Andreev9e674f72012-06-09 21:02:52 +030091 // ------------------------------------------------------------------------
92
Anton Lindqvist1e8be292012-01-21 12:25:08 +010093 /**
94 * Delete from cache
95 *
Andrey Andreev9e674f72012-06-09 21:02:52 +030096 * @param string Cache key
97 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +010098 */
99 public function delete($key)
100 {
101 return ($this->_redis->delete($key) === 1);
102 }
103
Andrey Andreev9e674f72012-06-09 21:02:52 +0300104 // ------------------------------------------------------------------------
105
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100106 /**
107 * Clean cache
108 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300109 * @return bool
110 * @see Redis::flushDB()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100111 */
112 public function clean()
113 {
114 return $this->_redis->flushDB();
115 }
116
Andrey Andreev9e674f72012-06-09 21:02:52 +0300117 // ------------------------------------------------------------------------
118
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100119 /**
120 * Get cache driver info
121 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300122 * @param string Not supported in Redis.
123 * Only included in order to offer a
124 * consistent cache API.
125 * @return array
126 * @see Redis::info()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100127 */
128 public function cache_info($type = NULL)
129 {
130 return $this->_redis->info();
131 }
132
Andrey Andreev9e674f72012-06-09 21:02:52 +0300133 // ------------------------------------------------------------------------
134
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100135 /**
136 * Get cache metadata
137 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300138 * @param string Cache key
139 * @return array
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100140 */
141 public function get_metadata($key)
142 {
143 $value = $this->get($key);
144
145 if ($value)
Andrey Andreev9e674f72012-06-09 21:02:52 +0300146 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100147 return array(
148 'expire' => time() + $this->_redis->ttl($key),
149 'data' => $value
150 );
151 }
Andrey Andreev9e674f72012-06-09 21:02:52 +0300152
153 return FALSE;
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100154 }
155
Andrey Andreev9e674f72012-06-09 21:02:52 +0300156 // ------------------------------------------------------------------------
157
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100158 /**
159 * Check if Redis driver is supported
160 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300161 * @return bool
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100162 */
163 public function is_supported()
164 {
165 if (extension_loaded('redis'))
Andrey Andreev9e674f72012-06-09 21:02:52 +0300166 {
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100167 $this->_setup_redis();
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100168 return TRUE;
169 }
170 else
171 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400172 log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100173 return FALSE;
174 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100175 }
176
Andrey Andreev9e674f72012-06-09 21:02:52 +0300177 // ------------------------------------------------------------------------
178
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100179 /**
180 * Setup Redis config and connection
181 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300182 * Loads Redis config file if present. Will halt execution
183 * if a Redis connection can't be established.
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100184 *
Andrey Andreev9e674f72012-06-09 21:02:52 +0300185 * @return bool
186 * @see Redis::connect()
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100187 */
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 {
Kakysha80d663a2013-10-28 01:39:17 +0400204 if ($config['socket_type'] === 'unix')
205 {
206 $v = $this->_redis->connect($config['socket']);
207 } else // tcp socket
208 {
209 $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
210 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100211 }
212 catch (RedisException $e)
213 {
214 show_error('Redis connection refused. ' . $e->getMessage());
215 }
Anton Lindqvist210e6642012-04-23 10:13:46 +0200216
Anton Lindqvist5ccf5ce2012-06-08 11:47:17 +0200217 if (isset($config['password']))
218 {
Anton Lindqvist210e6642012-04-23 10:13:46 +0200219 $this->_redis->auth($config['password']);
220 }
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100221 }
222
Andrey Andreev9e674f72012-06-09 21:02:52 +0300223 // ------------------------------------------------------------------------
224
225 /**
226
227 * Class destructor
228 *
229 * Closes the connection to Redis if present.
230 *
231 * @return void
232 */
233 public function __destruct()
234 {
235 if ($this->_redis)
236 {
237 $this->_redis->close();
238 }
239 }
240
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100241}
Anton Lindqvist1e8be292012-01-21 12:25:08 +0100242
243/* End of file Cache_redis.php */
244/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */