Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 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 |
| 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 |
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 | */ |
| 27 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Redis Caching Class |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Core |
| 34 | * @author Anton Lindqvist <anton@qvister.se> |
| 35 | * @link |
| 36 | */ |
| 37 | class CI_Cache_redis extends CI_Driver |
| 38 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Default config |
| 41 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 42 | * @static |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 43 | * @var array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 44 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 45 | protected static $_default_config = array( |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 46 | 'host' => '127.0.0.1', |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 47 | 'password' => NULL, |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 48 | 'port' => 6379, |
| 49 | 'timeout' => 0 |
| 50 | ); |
| 51 | |
| 52 | /** |
| 53 | * Redis connection |
| 54 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 55 | * @var Redis |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 56 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 57 | protected $_redis; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 58 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 59 | // ------------------------------------------------------------------------ |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Get cache |
| 63 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 64 | * @param string Cache key identifier |
| 65 | * @return mixed |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 66 | */ |
| 67 | public function get($key) |
| 68 | { |
| 69 | return $this->_redis->get($key); |
| 70 | } |
| 71 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 72 | // ------------------------------------------------------------------------ |
| 73 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 74 | /** |
| 75 | * Save cache |
| 76 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 77 | * @param string Cache key identifier |
| 78 | * @param mixed Data to save |
| 79 | * @param int Time to live |
| 80 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 81 | */ |
| 82 | public function save($key, $value, $ttl = NULL) |
| 83 | { |
| 84 | return ($ttl) |
| 85 | ? $this->_redis->setex($key, $ttl, $value) |
| 86 | : $this->_redis->set($key, $value); |
| 87 | } |
| 88 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 89 | // ------------------------------------------------------------------------ |
| 90 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 91 | /** |
| 92 | * Delete from cache |
| 93 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 94 | * @param string Cache key |
| 95 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 96 | */ |
| 97 | public function delete($key) |
| 98 | { |
| 99 | return ($this->_redis->delete($key) === 1); |
| 100 | } |
| 101 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 102 | // ------------------------------------------------------------------------ |
| 103 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 104 | /** |
| 105 | * Clean cache |
| 106 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 107 | * @return bool |
| 108 | * @see Redis::flushDB() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 109 | */ |
| 110 | public function clean() |
| 111 | { |
| 112 | return $this->_redis->flushDB(); |
| 113 | } |
| 114 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 115 | // ------------------------------------------------------------------------ |
| 116 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 117 | /** |
| 118 | * Get cache driver info |
| 119 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 120 | * @param string Not supported in Redis. |
| 121 | * Only included in order to offer a |
| 122 | * consistent cache API. |
| 123 | * @return array |
| 124 | * @see Redis::info() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 125 | */ |
| 126 | public function cache_info($type = NULL) |
| 127 | { |
| 128 | return $this->_redis->info(); |
| 129 | } |
| 130 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 131 | // ------------------------------------------------------------------------ |
| 132 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 133 | /** |
| 134 | * Get cache metadata |
| 135 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 136 | * @param string Cache key |
| 137 | * @return array |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 138 | */ |
| 139 | public function get_metadata($key) |
| 140 | { |
| 141 | $value = $this->get($key); |
| 142 | |
| 143 | if ($value) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 144 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 145 | return array( |
| 146 | 'expire' => time() + $this->_redis->ttl($key), |
| 147 | 'data' => $value |
| 148 | ); |
| 149 | } |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 150 | |
| 151 | return FALSE; |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 154 | // ------------------------------------------------------------------------ |
| 155 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 156 | /** |
| 157 | * Check if Redis driver is supported |
| 158 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 159 | * @return bool |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 160 | */ |
| 161 | public function is_supported() |
| 162 | { |
| 163 | if (extension_loaded('redis')) |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 164 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 165 | $this->_setup_redis(); |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 166 | return TRUE; |
| 167 | } |
| 168 | else |
| 169 | { |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 170 | 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] | 171 | return FALSE; |
| 172 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 175 | // ------------------------------------------------------------------------ |
| 176 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 177 | /** |
| 178 | * Setup Redis config and connection |
| 179 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 180 | * Loads Redis config file if present. Will halt execution |
| 181 | * if a Redis connection can't be established. |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 182 | * |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 183 | * @return bool |
| 184 | * @see Redis::connect() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 185 | */ |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 186 | protected function _setup_redis() |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 187 | { |
| 188 | $config = array(); |
| 189 | $CI =& get_instance(); |
| 190 | |
| 191 | if ($CI->config->load('redis', TRUE, TRUE)) |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 192 | { |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 193 | $config += $CI->config->item('redis'); |
| 194 | } |
| 195 | |
| 196 | $config = array_merge(self::$_default_config, $config); |
| 197 | |
| 198 | $this->_redis = new Redis(); |
| 199 | |
| 200 | try |
| 201 | { |
| 202 | $this->_redis->connect($config['host'], $config['port'], $config['timeout']); |
| 203 | } |
| 204 | catch (RedisException $e) |
| 205 | { |
| 206 | show_error('Redis connection refused. ' . $e->getMessage()); |
| 207 | } |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 208 | |
Anton Lindqvist | 5ccf5ce | 2012-06-08 11:47:17 +0200 | [diff] [blame] | 209 | if (isset($config['password'])) |
| 210 | { |
Anton Lindqvist | 210e664 | 2012-04-23 10:13:46 +0200 | [diff] [blame] | 211 | $this->_redis->auth($config['password']); |
| 212 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame^] | 215 | // ------------------------------------------------------------------------ |
| 216 | |
| 217 | /** |
| 218 | |
| 219 | * Class destructor |
| 220 | * |
| 221 | * Closes the connection to Redis if present. |
| 222 | * |
| 223 | * @return void |
| 224 | */ |
| 225 | public function __destruct() |
| 226 | { |
| 227 | if ($this->_redis) |
| 228 | { |
| 229 | $this->_redis->close(); |
| 230 | } |
| 231 | } |
| 232 | |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 233 | } |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 234 | |
| 235 | /* End of file Cache_redis.php */ |
| 236 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |