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 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 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 |
| 24 | * @since Version 2.0 |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter Redis Caching Class |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Core |
| 36 | * @author Anton Lindqvist <anton@qvister.se> |
| 37 | * @link |
| 38 | */ |
| 39 | class CI_Cache_redis extends CI_Driver |
| 40 | { |
| 41 | |
| 42 | /** |
| 43 | * Default config |
| 44 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 45 | * @static |
| 46 | * @var array |
| 47 | */ |
Anton Lindqvist | 3573af8 | 2012-01-21 20:33:12 +0100 | [diff] [blame] | 48 | protected static $_default_config = array( |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 49 | 'host' => '127.0.0.1', |
| 50 | 'port' => 6379, |
| 51 | 'timeout' => 0 |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * Redis connection |
| 56 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 57 | * @var Redis |
| 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 | |
| 61 | /** |
| 62 | * Class destructor |
| 63 | * |
| 64 | * Closes the connection to Redis if present. |
| 65 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 66 | * @return void |
| 67 | */ |
| 68 | public function __destruct() |
| 69 | { |
| 70 | if ($this->_redis) |
| 71 | { |
| 72 | $this->_redis->close(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get cache |
| 78 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 79 | * @param string $key Cache key identifier |
| 80 | * @return mixed |
| 81 | */ |
| 82 | public function get($key) |
| 83 | { |
| 84 | return $this->_redis->get($key); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Save cache |
| 89 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 90 | * @param string $key Cache key identifier |
| 91 | * @param mixed $value Data to save |
| 92 | * @param integer $ttl Time to live |
| 93 | * @return boolean |
| 94 | */ |
| 95 | public function save($key, $value, $ttl = NULL) |
| 96 | { |
| 97 | return ($ttl) |
| 98 | ? $this->_redis->setex($key, $ttl, $value) |
| 99 | : $this->_redis->set($key, $value); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Delete from cache |
| 104 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 105 | * @param string $key Cache key |
| 106 | * @return boolean |
| 107 | */ |
| 108 | public function delete($key) |
| 109 | { |
| 110 | return ($this->_redis->delete($key) === 1); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Clean cache |
| 115 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 116 | * @return boolean |
| 117 | * @see Redis::flushDB() |
| 118 | */ |
| 119 | public function clean() |
| 120 | { |
| 121 | return $this->_redis->flushDB(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get cache driver info |
| 126 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 127 | * @param string $type Not supported in Redis. Only included in order to offer a |
| 128 | * consistent cache API. |
| 129 | * @return array |
| 130 | * @see Redis::info() |
| 131 | */ |
| 132 | public function cache_info($type = NULL) |
| 133 | { |
| 134 | return $this->_redis->info(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get cache metadata |
| 139 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 140 | * @param string $key Cache key |
| 141 | * @return array |
| 142 | */ |
| 143 | public function get_metadata($key) |
| 144 | { |
| 145 | $value = $this->get($key); |
| 146 | |
| 147 | if ($value) |
| 148 | { |
| 149 | return array( |
| 150 | 'expire' => time() + $this->_redis->ttl($key), |
| 151 | 'data' => $value |
| 152 | ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Check if Redis driver is supported |
| 158 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 159 | * @return boolean |
| 160 | */ |
| 161 | public function is_supported() |
| 162 | { |
| 163 | if (extension_loaded('redis')) |
| 164 | { |
| 165 | $this->_setup_redis(); |
| 166 | |
| 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 | |
| 173 | return FALSE; |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Setup Redis config and connection |
| 180 | * |
| 181 | * Loads Redis config file if present. Will halt execution if a Redis connection |
| 182 | * can't be established. |
| 183 | * |
Anton Lindqvist | 1e8be29 | 2012-01-21 12:25:08 +0100 | [diff] [blame] | 184 | * @return void |
| 185 | * @see Redis::connect() |
| 186 | */ |
| 187 | private function _setup_redis() |
| 188 | { |
| 189 | $config = array(); |
| 190 | $CI =& get_instance(); |
| 191 | |
| 192 | if ($CI->config->load('redis', TRUE, TRUE)) |
| 193 | { |
| 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 | } |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | // End Class |
| 213 | |
| 214 | /* End of file Cache_redis.php */ |
| 215 | /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ |