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 | * |
| 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 25 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 29 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 30 | * CodeIgniter Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 36 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 38 | class CI_Cache extends CI_Driver_Library { |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 39 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 40 | /** |
| 41 | * Valid cache drivers |
| 42 | * |
| 43 | * @var array |
| 44 | */ |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 45 | protected $valid_drivers = array( |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 46 | 'apc', |
| 47 | 'dummy', |
| 48 | 'file', |
| 49 | 'memcached', |
| 50 | 'redis', |
| 51 | 'wincache' |
Anton Lindqvist | 3093042 | 2012-04-25 12:32:58 +0200 | [diff] [blame] | 52 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 53 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Path of cache files (if file-based cache) |
| 56 | * |
| 57 | * @var string |
| 58 | */ |
| 59 | protected $_cache_path = NULL; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 60 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 61 | /** |
| 62 | * Reference to the driver |
| 63 | * |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 64 | * @var mixed |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 65 | */ |
| 66 | protected $_adapter = 'dummy'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 67 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 68 | /** |
| 69 | * Fallback driver |
| 70 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 71 | * @var string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 72 | */ |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 73 | protected $_backup_driver = 'dummy'; |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 74 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 75 | /** |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 76 | * Cache key prefix |
| 77 | * |
| 78 | * @var string |
| 79 | */ |
| 80 | public $key_prefix = ''; |
| 81 | |
| 82 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 83 | * Constructor |
| 84 | * |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 85 | * Initialize class properties based on the configuration array. |
| 86 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 87 | * @param array $config = array() |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 88 | * @return void |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 89 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 90 | public function __construct($config = array()) |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 91 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 92 | $default_config = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 93 | 'adapter', |
| 94 | 'memcached' |
| 95 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 96 | |
| 97 | foreach ($default_config as $key) |
| 98 | { |
| 99 | if (isset($config[$key])) |
| 100 | { |
| 101 | $param = '_'.$key; |
| 102 | |
| 103 | $this->{$param} = $config[$key]; |
| 104 | } |
| 105 | } |
| 106 | |
vkeranov | 94b1f76 | 2012-10-27 18:19:59 +0300 | [diff] [blame] | 107 | isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix']; |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 108 | |
Tyler Brownell | 516527c | 2013-07-24 10:51:04 -0400 | [diff] [blame] | 109 | if (isset($config['backup']) && in_array($config['backup'], $this->valid_drivers)) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 110 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 111 | $this->_backup_driver = $config['backup']; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 112 | } |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 113 | |
| 114 | // If the specified adapter isn't available, check the backup. |
| 115 | if ( ! $this->is_supported($this->_adapter)) |
| 116 | { |
| 117 | if ( ! $this->is_supported($this->_backup_driver)) |
| 118 | { |
| 119 | // Backup isn't supported either. Default to 'Dummy' driver. |
| 120 | log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.'); |
| 121 | $this->_adapter = 'dummy'; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | // Backup is supported. Set it to primary. |
Tyler Brownell | 516527c | 2013-07-24 10:51:04 -0400 | [diff] [blame] | 126 | 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] | 127 | $this->_adapter = $this->_backup_driver; |
| 128 | } |
| 129 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // ------------------------------------------------------------------------ |
| 133 | |
| 134 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 135 | * Get |
| 136 | * |
| 137 | * Look for a value in the cache. If it exists, return the data |
| 138 | * if not, return FALSE |
| 139 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 140 | * @param string $id |
| 141 | * @return mixed value matching $id or FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 142 | */ |
| 143 | public function get($id) |
| 144 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 145 | return $this->{$this->_adapter}->get($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // ------------------------------------------------------------------------ |
| 149 | |
| 150 | /** |
| 151 | * Cache Save |
| 152 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame^] | 153 | * @param string $id Cache ID |
| 154 | * @param mixed $data Data to store |
| 155 | * @param int $ttl Cache TTL (in seconds) |
| 156 | * @param bool $raw Whether to store the raw value |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 157 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 158 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame^] | 159 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 160 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame^] | 161 | return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // ------------------------------------------------------------------------ |
| 165 | |
| 166 | /** |
| 167 | * Delete from Cache |
| 168 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 169 | * @param string $id Cache ID |
| 170 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 171 | */ |
| 172 | public function delete($id) |
| 173 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 174 | return $this->{$this->_adapter}->delete($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // ------------------------------------------------------------------------ |
| 178 | |
| 179 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame^] | 180 | * Increment a raw value |
| 181 | * |
| 182 | * @param string $id Cache ID |
| 183 | * @param int $offset Step/value to add |
| 184 | * @return mixed New value on success or FALSE on failure |
| 185 | */ |
| 186 | public function increment($id, $offset = 1) |
| 187 | { |
| 188 | return $this->{$this->_adapter}->increment($id, $offset); |
| 189 | } |
| 190 | |
| 191 | // ------------------------------------------------------------------------ |
| 192 | |
| 193 | /** |
| 194 | * Decrement a raw value |
| 195 | * |
| 196 | * @param string $id Cache ID |
| 197 | * @param int $offset Step/value to reduce by |
| 198 | * @return mixed New value on success or FALSE on failure |
| 199 | */ |
| 200 | public function decrement($id, $offset = 1) |
| 201 | { |
| 202 | return $this->{$this->_adapter}->decrement($id, $offset); |
| 203 | } |
| 204 | |
| 205 | // ------------------------------------------------------------------------ |
| 206 | |
| 207 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 208 | * Clean the cache |
| 209 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 210 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 211 | */ |
| 212 | public function clean() |
| 213 | { |
| 214 | return $this->{$this->_adapter}->clean(); |
| 215 | } |
| 216 | |
| 217 | // ------------------------------------------------------------------------ |
| 218 | |
| 219 | /** |
| 220 | * Cache Info |
| 221 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 222 | * @param string $type = 'user' user/filehits |
| 223 | * @return mixed array containing cache info on success OR FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 224 | */ |
| 225 | public function cache_info($type = 'user') |
| 226 | { |
| 227 | return $this->{$this->_adapter}->cache_info($type); |
| 228 | } |
| 229 | |
| 230 | // ------------------------------------------------------------------------ |
| 231 | |
| 232 | /** |
| 233 | * Get Cache Metadata |
| 234 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 235 | * @param string $id key to get cache metadata on |
| 236 | * @return mixed cache item metadata |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 237 | */ |
| 238 | public function get_metadata($id) |
| 239 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 240 | return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // ------------------------------------------------------------------------ |
| 244 | |
| 245 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 246 | * Is the requested driver supported in this environment? |
| 247 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 248 | * @param string $driver The driver to test |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 249 | * @return array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 250 | */ |
| 251 | public function is_supported($driver) |
| 252 | { |
| 253 | static $support = array(); |
| 254 | |
| 255 | if ( ! isset($support[$driver])) |
| 256 | { |
| 257 | $support[$driver] = $this->{$driver}->is_supported(); |
| 258 | } |
| 259 | |
| 260 | return $support[$driver]; |
| 261 | } |
| 262 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 263 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 264 | |
| 265 | /* End of file Cache.php */ |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 266 | /* Location: ./system/libraries/Cache/Cache.php */ |