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 | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 21 | * @copyright Copyright (c) 2008 - 2012, 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( |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 47 | 'host' => '127.0.0.1', |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 48 | 'password' => NULL, |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 49 | 'port' => 6379, |
| 50 | 'timeout' => 0 |
| 51 | ); |
| 52 | |
| 53 | /** |
| 54 | * Redis connection |
| 55 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 56 | * @var Redis |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 57 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 58 | protected $_redis; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 59 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 60 | // ------------------------------------------------------------------------ |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Get cache |
| 64 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 65 | * @param string Cache key identifier |
| 66 | * @return mixed |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 67 | */ |
| 68 | public function get($key) |
| 69 | { |
| 70 | return $this->_redis->get($key); |
| 71 | } |
| 72 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 73 | // ------------------------------------------------------------------------ |
| 74 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 75 | /** |
| 76 | * Save cache |
| 77 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 78 | * @param string Cache key identifier |
| 79 | * @param mixed Data to save |
| 80 | * @param int Time to live |
| 81 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 82 | */ |
| 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 Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 90 | // ------------------------------------------------------------------------ |
| 91 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 92 | /** |
| 93 | * Delete from cache |
| 94 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 95 | * @param string Cache key |
| 96 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 97 | */ |
| 98 | public function delete($key) |
| 99 | { |
| 100 | return ($this->_redis->delete($key) === 1); |
| 101 | } |
| 102 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 103 | // ------------------------------------------------------------------------ |
| 104 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 105 | /** |
| 106 | * Clean cache |
| 107 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 108 | * @return bool |
| 109 | * @see Redis::flushDB() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 110 | */ |
| 111 | public function clean() |
| 112 | { |
| 113 | return $this->_redis->flushDB(); |
| 114 | } |
| 115 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 116 | // ------------------------------------------------------------------------ |
| 117 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 118 | /** |
| 119 | * Get cache driver info |
| 120 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 121 | * @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 Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 126 | */ |
| 127 | public function cache_info($type = NULL) |
| 128 | { |
| 129 | return $this->_redis->info(); |
| 130 | } |
| 131 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 132 | // ------------------------------------------------------------------------ |
| 133 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 134 | /** |
| 135 | * Get cache metadata |
| 136 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 137 | * @param string Cache key |
| 138 | * @return array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 139 | */ |
| 140 | public function get_metadata($key) |
| 141 | { |
| 142 | $value = $this->get($key); |
| 143 | |
| 144 | if ($value) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 145 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 146 | return array( |
| 147 | 'expire' => time() + $this->_redis->ttl($key), |
| 148 | 'data' => $value |
| 149 | ); |
| 150 | } |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 151 | |
| 152 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 155 | // ------------------------------------------------------------------------ |
| 156 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 157 | /** |
| 158 | * Check if Redis driver is supported |
| 159 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 160 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 161 | */ |
| 162 | public function is_supported() |
| 163 | { |
| 164 | if (extension_loaded('redis')) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 165 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 166 | $this->_setup_redis(); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 167 | return TRUE; |
| 168 | } |
| 169 | else |
| 170 | { |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 171 | log_message('error', 'The Redis extension must be loaded to use Redis cache.'); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 172 | return FALSE; |
| 173 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 176 | // ------------------------------------------------------------------------ |
| 177 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 178 | /** |
| 179 | * Setup Redis config and connection |
| 180 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 181 | * Loads Redis config file if present. Will halt execution |
| 182 | * if a Redis connection can't be established. |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 183 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 184 | * @return bool |
| 185 | * @see Redis::connect() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 186 | */ |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 187 | protected function _setup_redis() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 188 | { |
| 189 | $config = array(); |
| 190 | $CI =& get_instance(); |
| 191 | |
| 192 | if ($CI->config->load('redis', TRUE, TRUE)) |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 193 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 194 | $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 Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 209 | |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 210 | if (isset($config['password'])) |
| 211 | { |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 212 | $this->_redis->auth($config['password']); |
| 213 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 216 | // ------------------------------------------------------------------------ |
| 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 Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 234 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 235 | |
| 236 | /* End of file Cache_redis.php */ |
| 237 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |