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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
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 | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 | */ |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 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 | */ |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [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 | { |
Tyler Brownell | ef0173d | 2015-04-28 10:33:05 -0400 | [diff] [blame] | 103 | isset($config['adapter']) && $this->_adapter = $config['adapter']; |
| 104 | isset($config['backup']) && $this->_backup_driver = $config['backup']; |
vkeranov | 94b1f76 | 2012-10-27 18:19:59 +0300 | [diff] [blame] | 105 | isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix']; |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 106 | |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 107 | // If the specified adapter isn't available, check the backup. |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 108 | if ( ! $this->is_supported($this->_adapter)) |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 109 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 110 | if ( ! $this->is_supported($this->_backup_driver)) |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 111 | { |
| 112 | // Backup isn't supported either. Default to 'Dummy' driver. |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 113 | log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.'); |
| 114 | $this->_adapter = 'dummy'; |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
| 118 | // Backup is supported. Set it to primary. |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 119 | log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.'); |
| 120 | $this->_adapter = $this->_backup_driver; |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 121 | } |
| 122 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // ------------------------------------------------------------------------ |
| 126 | |
| 127 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 128 | * Get |
| 129 | * |
| 130 | * Look for a value in the cache. If it exists, return the data |
| 131 | * if not, return FALSE |
| 132 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 133 | * @param string $id |
| 134 | * @return mixed value matching $id or FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 135 | */ |
| 136 | public function get($id) |
| 137 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 138 | return $this->{$this->_adapter}->get($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // ------------------------------------------------------------------------ |
| 142 | |
| 143 | /** |
| 144 | * Cache Save |
| 145 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 146 | * @param string $id Cache ID |
| 147 | * @param mixed $data Data to store |
| 148 | * @param int $ttl Cache TTL (in seconds) |
| 149 | * @param bool $raw Whether to store the raw value |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 150 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 151 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 152 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 153 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 154 | return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // ------------------------------------------------------------------------ |
| 158 | |
| 159 | /** |
| 160 | * Delete from Cache |
| 161 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 162 | * @param string $id Cache ID |
| 163 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 164 | */ |
| 165 | public function delete($id) |
| 166 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 167 | return $this->{$this->_adapter}->delete($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // ------------------------------------------------------------------------ |
| 171 | |
| 172 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 173 | * Increment a raw value |
| 174 | * |
| 175 | * @param string $id Cache ID |
| 176 | * @param int $offset Step/value to add |
| 177 | * @return mixed New value on success or FALSE on failure |
| 178 | */ |
| 179 | public function increment($id, $offset = 1) |
| 180 | { |
ftwbzhao | 63b0c26 | 2015-07-05 22:07:58 +0800 | [diff] [blame] | 181 | return $this->{$this->_adapter}->increment($this->key_prefix.$id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // ------------------------------------------------------------------------ |
| 185 | |
| 186 | /** |
| 187 | * Decrement a raw value |
| 188 | * |
| 189 | * @param string $id Cache ID |
| 190 | * @param int $offset Step/value to reduce by |
| 191 | * @return mixed New value on success or FALSE on failure |
| 192 | */ |
| 193 | public function decrement($id, $offset = 1) |
| 194 | { |
ftwbzhao | 63b0c26 | 2015-07-05 22:07:58 +0800 | [diff] [blame] | 195 | return $this->{$this->_adapter}->decrement($this->key_prefix.$id, $offset); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // ------------------------------------------------------------------------ |
| 199 | |
| 200 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 201 | * Clean the cache |
| 202 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 203 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 204 | */ |
| 205 | public function clean() |
| 206 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 207 | return $this->{$this->_adapter}->clean(); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // ------------------------------------------------------------------------ |
| 211 | |
| 212 | /** |
| 213 | * Cache Info |
| 214 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 215 | * @param string $type = 'user' user/filehits |
| 216 | * @return mixed array containing cache info on success OR FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 217 | */ |
| 218 | public function cache_info($type = 'user') |
| 219 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 220 | return $this->{$this->_adapter}->cache_info($type); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // ------------------------------------------------------------------------ |
| 224 | |
| 225 | /** |
| 226 | * Get Cache Metadata |
| 227 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 228 | * @param string $id key to get cache metadata on |
| 229 | * @return mixed cache item metadata |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 230 | */ |
| 231 | public function get_metadata($id) |
| 232 | { |
Tyler Brownell | 0416a7f | 2015-04-24 10:25:38 -0400 | [diff] [blame] | 233 | return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // ------------------------------------------------------------------------ |
| 237 | |
| 238 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 239 | * Is the requested driver supported in this environment? |
| 240 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 241 | * @param string $driver The driver to test |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 242 | * @return array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 243 | */ |
| 244 | public function is_supported($driver) |
| 245 | { |
Andrey Andreev | 070ce05 | 2015-06-22 13:24:11 +0300 | [diff] [blame] | 246 | static $support; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 247 | |
Andrey Andreev | 070ce05 | 2015-06-22 13:24:11 +0300 | [diff] [blame] | 248 | if ( ! isset($support, $support[$driver])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 249 | { |
| 250 | $support[$driver] = $this->{$driver}->is_supported(); |
| 251 | } |
| 252 | |
| 253 | return $support[$driver]; |
| 254 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 255 | } |