Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 6 | * |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 7 | * 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 Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Anton Lindqvist | 5a1d953 | 2012-01-23 23:20:26 +0100 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 24 | * @since Version 3.0 |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 28 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 29 | /** |
| 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 | */ |
| 38 | class CI_Cache_redis extends CI_Driver |
| 39 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 40 | /** |
| 41 | * Default config |
| 42 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 43 | * @static |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 44 | * @var array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 45 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 46 | protected static $_default_config = array( |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame^] | 47 | 'socket_type' => 'tcp', |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 48 | 'host' => '127.0.0.1', |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 49 | 'password' => NULL, |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 50 | 'port' => 6379, |
| 51 | 'timeout' => 0 |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * Redis connection |
| 56 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 57 | * @var Redis |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 58 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 59 | protected $_redis; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 60 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 61 | // ------------------------------------------------------------------------ |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Get cache |
| 65 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 66 | * @param string Cache key identifier |
| 67 | * @return mixed |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 68 | */ |
| 69 | public function get($key) |
| 70 | { |
| 71 | return $this->_redis->get($key); |
| 72 | } |
| 73 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 74 | // ------------------------------------------------------------------------ |
| 75 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 76 | /** |
| 77 | * Save cache |
| 78 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 79 | * @param string Cache key identifier |
| 80 | * @param mixed Data to save |
| 81 | * @param int Time to live |
| 82 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 83 | */ |
| 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 Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 91 | // ------------------------------------------------------------------------ |
| 92 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 93 | /** |
| 94 | * Delete from cache |
| 95 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 96 | * @param string Cache key |
| 97 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 98 | */ |
| 99 | public function delete($key) |
| 100 | { |
| 101 | return ($this->_redis->delete($key) === 1); |
| 102 | } |
| 103 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 104 | // ------------------------------------------------------------------------ |
| 105 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 106 | /** |
| 107 | * Clean cache |
| 108 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 109 | * @return bool |
| 110 | * @see Redis::flushDB() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 111 | */ |
| 112 | public function clean() |
| 113 | { |
| 114 | return $this->_redis->flushDB(); |
| 115 | } |
| 116 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 117 | // ------------------------------------------------------------------------ |
| 118 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 119 | /** |
| 120 | * Get cache driver info |
| 121 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 122 | * @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 Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 127 | */ |
| 128 | public function cache_info($type = NULL) |
| 129 | { |
| 130 | return $this->_redis->info(); |
| 131 | } |
| 132 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 133 | // ------------------------------------------------------------------------ |
| 134 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 135 | /** |
| 136 | * Get cache metadata |
| 137 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 138 | * @param string Cache key |
| 139 | * @return array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 140 | */ |
| 141 | public function get_metadata($key) |
| 142 | { |
| 143 | $value = $this->get($key); |
| 144 | |
| 145 | if ($value) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 146 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 147 | return array( |
| 148 | 'expire' => time() + $this->_redis->ttl($key), |
| 149 | 'data' => $value |
| 150 | ); |
| 151 | } |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 152 | |
| 153 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 156 | // ------------------------------------------------------------------------ |
| 157 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 158 | /** |
| 159 | * Check if Redis driver is supported |
| 160 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 161 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 162 | */ |
| 163 | public function is_supported() |
| 164 | { |
| 165 | if (extension_loaded('redis')) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 166 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 167 | $this->_setup_redis(); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 168 | return TRUE; |
| 169 | } |
| 170 | else |
| 171 | { |
Tyler Brownell | d967f72 | 2013-07-29 19:06:52 -0400 | [diff] [blame] | 172 | log_message('debug', 'The Redis extension must be loaded to use Redis cache.'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 173 | return FALSE; |
| 174 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 177 | // ------------------------------------------------------------------------ |
| 178 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 179 | /** |
| 180 | * Setup Redis config and connection |
| 181 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 182 | * Loads Redis config file if present. Will halt execution |
| 183 | * if a Redis connection can't be established. |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 184 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 185 | * @return bool |
| 186 | * @see Redis::connect() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 187 | */ |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 188 | protected function _setup_redis() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 189 | { |
| 190 | $config = array(); |
| 191 | $CI =& get_instance(); |
| 192 | |
| 193 | if ($CI->config->load('redis', TRUE, TRUE)) |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 194 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 195 | $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 | { |
Kakysha | 80d663a | 2013-10-28 01:39:17 +0400 | [diff] [blame^] | 204 | 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 Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 211 | } |
| 212 | catch (RedisException $e) |
| 213 | { |
| 214 | show_error('Redis connection refused. ' . $e->getMessage()); |
| 215 | } |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 216 | |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 217 | if (isset($config['password'])) |
| 218 | { |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 219 | $this->_redis->auth($config['password']); |
| 220 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 223 | // ------------------------------------------------------------------------ |
| 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 Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 241 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 242 | |
| 243 | /* End of file Cache_redis.php */ |
| 244 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |