|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CodeIgniter |
| 4 | + * |
| 5 | + * An open source application development framework for PHP |
| 6 | + * |
| 7 | + * This content is released under the MIT License (MIT) |
| 8 | + * |
| 9 | + * Copyright (c) 2019 - 2022, CodeIgniter Foundation |
| 10 | + * |
| 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: |
| 17 | + * |
| 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 |
| 31 | + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
| 32 | + * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | + * @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/) |
| 34 | + * @license https://opensource.org/licenses/MIT MIT License |
| 35 | + * @link https://codeigniter.com |
| 36 | + * @since Version 2.0.0 |
| 37 | + * @filesource |
| 38 | + */ |
| 39 | +defined('BASEPATH') OR exit('No direct script access allowed'); |
| 40 | + |
| 41 | +/** |
| 42 | + * CodeIgniter Caching Class |
| 43 | + * |
| 44 | + * @package CodeIgniter |
| 45 | + * @subpackage Libraries |
| 46 | + * @category Core |
| 47 | + * @author EllisLab Dev Team |
| 48 | + * @link |
| 49 | + */ |
| 50 | +class CI_Cache extends CI_Driver_Library { |
| 51 | + |
| 52 | + /** |
| 53 | + * Valid cache drivers |
| 54 | + * |
| 55 | + * @var array |
| 56 | + */ |
| 57 | + protected $valid_drivers = array( |
| 58 | + 'apc', |
| 59 | + 'dummy', |
| 60 | + 'file', |
| 61 | + 'memcached', |
| 62 | + 'redis', |
| 63 | + 'wincache' |
| 64 | + ); |
| 65 | + |
| 66 | + /** |
| 67 | + * Path of cache files (if file-based cache) |
| 68 | + * |
| 69 | + * @var string |
| 70 | + */ |
| 71 | + protected $_cache_path = NULL; |
| 72 | + |
| 73 | + /** |
| 74 | + * Reference to the driver |
| 75 | + * |
| 76 | + * @var mixed |
| 77 | + */ |
| 78 | + protected $_adapter = 'dummy'; |
| 79 | + |
| 80 | + /** |
| 81 | + * Fallback driver |
| 82 | + * |
| 83 | + * @var string |
| 84 | + */ |
| 85 | + protected $_backup_driver = 'dummy'; |
| 86 | + |
| 87 | + /** |
| 88 | + * Cache key prefix |
| 89 | + * |
| 90 | + * @var string |
| 91 | + */ |
| 92 | + public $key_prefix = ''; |
| 93 | + |
| 94 | + /** |
| 95 | + * Constructor |
| 96 | + * |
| 97 | + * Initialize class properties based on the configuration array. |
| 98 | + * |
| 99 | + * @param array $config = array() |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + public function __construct($config = array()) |
| 103 | + { |
| 104 | + isset($config['adapter']) && $this->_adapter = $config['adapter']; |
| 105 | + isset($config['backup']) && $this->_backup_driver = $config['backup']; |
| 106 | + isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix']; |
| 107 | + |
| 108 | + // If the specified adapter isn't available, check the backup. |
| 109 | + if ( ! $this->is_supported($this->_adapter)) |
| 110 | + { |
| 111 | + if ( ! $this->is_supported($this->_backup_driver)) |
| 112 | + { |
| 113 | + // Backup isn't supported either. Default to 'Dummy' driver. |
| 114 | + log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.'); |
| 115 | + $this->_adapter = 'dummy'; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + // Backup is supported. Set it to primary. |
| 120 | + log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.'); |
| 121 | + $this->_adapter = $this->_backup_driver; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // ------------------------------------------------------------------------ |
| 127 | + |
| 128 | + /** |
| 129 | + * Get |
| 130 | + * |
| 131 | + * Look for a value in the cache. If it exists, return the data |
| 132 | + * if not, return FALSE |
| 133 | + * |
| 134 | + * @param string $id |
| 135 | + * @return mixed value matching $id or FALSE on failure |
| 136 | + */ |
| 137 | + public function get($id) |
| 138 | + { |
| 139 | + return $this->{$this->_adapter}->get($this->key_prefix.$id); |
| 140 | + } |
| 141 | + |
| 142 | + // ------------------------------------------------------------------------ |
| 143 | + |
| 144 | + /** |
| 145 | + * Cache Save |
| 146 | + * |
| 147 | + * @param string $id Cache ID |
| 148 | + * @param mixed $data Data to store |
| 149 | + * @param int $ttl Cache TTL (in seconds) |
| 150 | + * @param bool $raw Whether to store the raw value |
| 151 | + * @return bool TRUE on success, FALSE on failure |
| 152 | + */ |
| 153 | + public function save($id, $data, $ttl = 60, $raw = FALSE) |
| 154 | + { |
| 155 | + return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw); |
| 156 | + } |
| 157 | + |
| 158 | + // ------------------------------------------------------------------------ |
| 159 | + |
| 160 | + /** |
| 161 | + * Delete from Cache |
| 162 | + * |
| 163 | + * @param string $id Cache ID |
| 164 | + * @return bool TRUE on success, FALSE on failure |
| 165 | + */ |
| 166 | + public function delete($id) |
| 167 | + { |
| 168 | + return $this->{$this->_adapter}->delete($this->key_prefix.$id); |
| 169 | + } |
| 170 | + |
| 171 | + // ------------------------------------------------------------------------ |
| 172 | + |
| 173 | + /** |
| 174 | + * Increment a raw value |
| 175 | + * |
| 176 | + * @param string $id Cache ID |
| 177 | + * @param int $offset Step/value to add |
| 178 | + * @return mixed New value on success or FALSE on failure |
| 179 | + */ |
| 180 | + public function increment($id, $offset = 1) |
| 181 | + { |
| 182 | + return $this->{$this->_adapter}->increment($this->key_prefix.$id, $offset); |
| 183 | + } |
| 184 | + |
| 185 | + // ------------------------------------------------------------------------ |
| 186 | + |
| 187 | + /** |
| 188 | + * Decrement a raw value |
| 189 | + * |
| 190 | + * @param string $id Cache ID |
| 191 | + * @param int $offset Step/value to reduce by |
| 192 | + * @return mixed New value on success or FALSE on failure |
| 193 | + */ |
| 194 | + public function decrement($id, $offset = 1) |
| 195 | + { |
| 196 | + return $this->{$this->_adapter}->decrement($this->key_prefix.$id, $offset); |
| 197 | + } |
| 198 | + |
| 199 | + // ------------------------------------------------------------------------ |
| 200 | + |
| 201 | + /** |
| 202 | + * Clean the cache |
| 203 | + * |
| 204 | + * @return bool TRUE on success, FALSE on failure |
| 205 | + */ |
| 206 | + public function clean() |
| 207 | + { |
| 208 | + return $this->{$this->_adapter}->clean(); |
| 209 | + } |
| 210 | + |
| 211 | + // ------------------------------------------------------------------------ |
| 212 | + |
| 213 | + /** |
| 214 | + * Cache Info |
| 215 | + * |
| 216 | + * @param string $type = 'user' user/filehits |
| 217 | + * @return mixed array containing cache info on success OR FALSE on failure |
| 218 | + */ |
| 219 | + public function cache_info($type = 'user') |
| 220 | + { |
| 221 | + return $this->{$this->_adapter}->cache_info($type); |
| 222 | + } |
| 223 | + |
| 224 | + // ------------------------------------------------------------------------ |
| 225 | + |
| 226 | + /** |
| 227 | + * Get Cache Metadata |
| 228 | + * |
| 229 | + * @param string $id key to get cache metadata on |
| 230 | + * @return mixed cache item metadata |
| 231 | + */ |
| 232 | + public function get_metadata($id) |
| 233 | + { |
| 234 | + return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id); |
| 235 | + } |
| 236 | + |
| 237 | + // ------------------------------------------------------------------------ |
| 238 | + |
| 239 | + /** |
| 240 | + * Is the requested driver supported in this environment? |
| 241 | + * |
| 242 | + * @param string $driver The driver to test |
| 243 | + * @return array |
| 244 | + */ |
| 245 | + public function is_supported($driver) |
| 246 | + { |
| 247 | + static $support; |
| 248 | + |
| 249 | + if ( ! isset($support, $support[$driver])) |
| 250 | + { |
| 251 | + $support[$driver] = $this->{$driver}->is_supported(); |
| 252 | + } |
| 253 | + |
| 254 | + return $support[$driver]; |
| 255 | + } |
| 256 | +} |
0 commit comments