Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014, British Columbia Institute of Technology |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 2.0.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 36 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 40 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 41 | * CodeIgniter Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 47 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 48 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 49 | class CI_Cache extends CI_Driver_Library { |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 50 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Valid cache drivers |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 56 | protected $valid_drivers = array( |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 57 | 'apc', |
| 58 | 'dummy', |
| 59 | 'file', |
| 60 | 'memcached', |
| 61 | 'redis', |
| 62 | 'wincache' |
Anton Lindqvist | 3093042 | 2012-04-25 12:32:58 +0200 | [diff] [blame] | 63 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 64 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 65 | /** |
| 66 | * Path of cache files (if file-based cache) |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
| 70 | protected $_cache_path = NULL; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 71 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 72 | /** |
| 73 | * Reference to the driver |
| 74 | * |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 75 | * @var mixed |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 76 | */ |
| 77 | protected $_adapter = 'dummy'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 78 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 79 | /** |
| 80 | * Fallback driver |
| 81 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 82 | * @var string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 83 | */ |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 84 | protected $_backup_driver = 'dummy'; |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 85 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 86 | /** |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 87 | * Cache key prefix |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
| 91 | public $key_prefix = ''; |
| 92 | |
| 93 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 94 | * Constructor |
| 95 | * |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 96 | * Initialize class properties based on the configuration array. |
| 97 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 98 | * @param array $config = array() |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 99 | * @return void |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 100 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 101 | public function __construct($config = array()) |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 102 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 103 | $default_config = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 104 | 'adapter', |
| 105 | 'memcached' |
| 106 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 107 | |
| 108 | foreach ($default_config as $key) |
| 109 | { |
| 110 | if (isset($config[$key])) |
| 111 | { |
| 112 | $param = '_'.$key; |
| 113 | |
| 114 | $this->{$param} = $config[$key]; |
| 115 | } |
| 116 | } |
| 117 | |
vkeranov | 94b1f76 | 2012-10-27 18:19:59 +0300 | [diff] [blame] | 118 | isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix']; |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 119 | |
Tyler Brownell | 516527c | 2013-07-24 10:51:04 -0400 | [diff] [blame] | 120 | if (isset($config['backup']) && in_array($config['backup'], $this->valid_drivers)) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 121 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 122 | $this->_backup_driver = $config['backup']; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 123 | } |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 124 | |
| 125 | // If the specified adapter isn't available, check the backup. |
| 126 | if ( ! $this->is_supported($this->_adapter)) |
| 127 | { |
| 128 | if ( ! $this->is_supported($this->_backup_driver)) |
| 129 | { |
| 130 | // Backup isn't supported either. Default to 'Dummy' driver. |
| 131 | log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.'); |
| 132 | $this->_adapter = 'dummy'; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | // Backup is supported. Set it to primary. |
Tyler Brownell | 516527c | 2013-07-24 10:51:04 -0400 | [diff] [blame] | 137 | log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.'); |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 138 | $this->_adapter = $this->_backup_driver; |
| 139 | } |
| 140 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // ------------------------------------------------------------------------ |
| 144 | |
| 145 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 146 | * Get |
| 147 | * |
| 148 | * Look for a value in the cache. If it exists, return the data |
| 149 | * if not, return FALSE |
| 150 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 151 | * @param string $id |
| 152 | * @return mixed value matching $id or FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 153 | */ |
| 154 | public function get($id) |
| 155 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 156 | return $this->{$this->_adapter}->get($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // ------------------------------------------------------------------------ |
| 160 | |
| 161 | /** |
| 162 | * Cache Save |
| 163 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 164 | * @param string $id Cache ID |
| 165 | * @param mixed $data Data to store |
| 166 | * @param int $ttl Cache TTL (in seconds) |
| 167 | * @param bool $raw Whether to store the raw value |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 168 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 169 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 170 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 171 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 172 | return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // ------------------------------------------------------------------------ |
| 176 | |
| 177 | /** |
| 178 | * Delete from Cache |
| 179 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 180 | * @param string $id Cache ID |
| 181 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 182 | */ |
| 183 | public function delete($id) |
| 184 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 185 | return $this->{$this->_adapter}->delete($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // ------------------------------------------------------------------------ |
| 189 | |
| 190 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 191 | * Increment a raw value |
| 192 | * |
| 193 | * @param string $id Cache ID |
| 194 | * @param int $offset Step/value to add |
| 195 | * @return mixed New value on success or FALSE on failure |
| 196 | */ |
| 197 | public function increment($id, $offset = 1) |
| 198 | { |
| 199 | return $this->{$this->_adapter}->increment($id, $offset); |
| 200 | } |
| 201 | |
| 202 | // ------------------------------------------------------------------------ |
| 203 | |
| 204 | /** |
| 205 | * Decrement a raw value |
| 206 | * |
| 207 | * @param string $id Cache ID |
| 208 | * @param int $offset Step/value to reduce by |
| 209 | * @return mixed New value on success or FALSE on failure |
| 210 | */ |
| 211 | public function decrement($id, $offset = 1) |
| 212 | { |
| 213 | return $this->{$this->_adapter}->decrement($id, $offset); |
| 214 | } |
| 215 | |
| 216 | // ------------------------------------------------------------------------ |
| 217 | |
| 218 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 219 | * Clean the cache |
| 220 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 221 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 222 | */ |
| 223 | public function clean() |
| 224 | { |
| 225 | return $this->{$this->_adapter}->clean(); |
| 226 | } |
| 227 | |
| 228 | // ------------------------------------------------------------------------ |
| 229 | |
| 230 | /** |
| 231 | * Cache Info |
| 232 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 233 | * @param string $type = 'user' user/filehits |
| 234 | * @return mixed array containing cache info on success OR FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 235 | */ |
| 236 | public function cache_info($type = 'user') |
| 237 | { |
| 238 | return $this->{$this->_adapter}->cache_info($type); |
| 239 | } |
| 240 | |
| 241 | // ------------------------------------------------------------------------ |
| 242 | |
| 243 | /** |
| 244 | * Get Cache Metadata |
| 245 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 246 | * @param string $id key to get cache metadata on |
| 247 | * @return mixed cache item metadata |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 248 | */ |
| 249 | public function get_metadata($id) |
| 250 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 251 | return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // ------------------------------------------------------------------------ |
| 255 | |
| 256 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 257 | * Is the requested driver supported in this environment? |
| 258 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 259 | * @param string $driver The driver to test |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 260 | * @return array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 261 | */ |
| 262 | public function is_supported($driver) |
| 263 | { |
| 264 | static $support = array(); |
| 265 | |
| 266 | if ( ! isset($support[$driver])) |
| 267 | { |
| 268 | $support[$driver] = $this->{$driver}->is_supported(); |
| 269 | } |
| 270 | |
| 271 | return $support[$driver]; |
| 272 | } |
| 273 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 274 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 275 | |
| 276 | /* End of file Cache.php */ |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 277 | /* Location: ./system/libraries/Cache/Cache.php */ |