blob: f13c0240255bc897d7040249f00fdd725f5755d9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Jonesf4a4bd82011-10-20 12:18:42 -05006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Akerbde25d92010-12-21 09:31:21 -060017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 2.0.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060039
Greg Akerbde25d92010-12-21 09:31:21 -060040/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020041 * CodeIgniter Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060042 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020047 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060048 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010049class CI_Cache extends CI_Driver_Library {
Andrey Andreev7d4ea072011-12-25 19:23:50 +020050
Timothy Warren0688ac92012-04-20 10:25:04 -040051 /**
52 * Valid cache drivers
53 *
54 * @var array
55 */
Andrey Andreev58c2b102012-10-26 14:42:29 +030056 protected $valid_drivers = array(
dchill426262d052012-11-24 18:41:13 -050057 'apc',
58 'dummy',
59 'file',
60 'memcached',
61 'redis',
62 'wincache'
Anton Lindqvist30930422012-04-25 12:32:58 +020063 );
Greg Akerbde25d92010-12-21 09:31:21 -060064
Timothy Warren0688ac92012-04-20 10:25:04 -040065 /**
66 * Path of cache files (if file-based cache)
67 *
68 * @var string
69 */
70 protected $_cache_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030071
Timothy Warren0688ac92012-04-20 10:25:04 -040072 /**
73 * Reference to the driver
74 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040075 * @var mixed
Timothy Warren0688ac92012-04-20 10:25:04 -040076 */
Tyler Brownell0416a7f2015-04-24 10:25:38 -040077 protected $_adapter = 'dummy';
Andrey Andreev56454792012-05-17 14:32:19 +030078
Timothy Warren0688ac92012-04-20 10:25:04 -040079 /**
80 * Fallback driver
81 *
Andrey Andreev58c2b102012-10-26 14:42:29 +030082 * @var string
Timothy Warren0688ac92012-04-20 10:25:04 -040083 */
Tyler Brownell0416a7f2015-04-24 10:25:38 -040084 protected $_backup_driver = 'dummy';
Andrey Andreev7d4ea072011-12-25 19:23:50 +020085
Greg Akerbde25d92010-12-21 09:31:21 -060086 /**
Andrey Andreev58c2b102012-10-26 14:42:29 +030087 * Cache key prefix
88 *
89 * @var string
90 */
91 public $key_prefix = '';
92
93 /**
Greg Akerbde25d92010-12-21 09:31:21 -060094 * Constructor
95 *
Greg Akerbde25d92010-12-21 09:31:21 -060096 * Initialize class properties based on the configuration array.
97 *
Andrey Andreev58c2b102012-10-26 14:42:29 +030098 * @param array $config = array()
Andrey Andreevb24b0332012-03-26 15:34:39 +030099 * @return void
Greg Akerbde25d92010-12-21 09:31:21 -0600100 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300101 public function __construct($config = array())
Greg Aker03abee32011-12-25 00:31:29 -0600102 {
Tyler Brownellef0173d2015-04-28 10:33:05 -0400103 isset($config['adapter']) && $this->_adapter = $config['adapter'];
104 isset($config['backup']) && $this->_backup_driver = $config['backup'];
vkeranov94b1f762012-10-27 18:19:59 +0300105 isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
Andrey Andreev58c2b102012-10-26 14:42:29 +0300106
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500107 // If the specified adapter isn't available, check the backup.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400108 if ( ! $this->is_supported($this->_adapter))
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500109 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400110 if ( ! $this->is_supported($this->_backup_driver))
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500111 {
112 // Backup isn't supported either. Default to 'Dummy' driver.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400113 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 Robertsbf50a3b2012-05-27 19:04:43 -0500115 }
116 else
117 {
118 // Backup is supported. Set it to primary.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400119 log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
120 $this->_adapter = $this->_backup_driver;
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500121 }
122 }
Greg Akerbde25d92010-12-21 09:31:21 -0600123 }
124
125 // ------------------------------------------------------------------------
126
127 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300128 * Get
129 *
130 * Look for a value in the cache. If it exists, return the data
131 * if not, return FALSE
132 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300133 * @param string $id
134 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300135 */
136 public function get($id)
137 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400138 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300139 }
140
141 // ------------------------------------------------------------------------
142
143 /**
144 * Cache Save
145 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200146 * @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 Andreev58c2b102012-10-26 14:42:29 +0300150 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300151 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200152 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreevb24b0332012-03-26 15:34:39 +0300153 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400154 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300155 }
156
157 // ------------------------------------------------------------------------
158
159 /**
160 * Delete from Cache
161 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300162 * @param string $id Cache ID
163 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300164 */
165 public function delete($id)
166 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400167 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300168 }
169
170 // ------------------------------------------------------------------------
171
172 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200173 * 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 {
ftwbzhao63b0c262015-07-05 22:07:58 +0800181 return $this->{$this->_adapter}->increment($this->key_prefix.$id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200182 }
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 {
ftwbzhao63b0c262015-07-05 22:07:58 +0800195 return $this->{$this->_adapter}->decrement($this->key_prefix.$id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200196 }
197
198 // ------------------------------------------------------------------------
199
200 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300201 * Clean the cache
202 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300203 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300204 */
205 public function clean()
206 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400207 return $this->{$this->_adapter}->clean();
Andrey Andreevb24b0332012-03-26 15:34:39 +0300208 }
209
210 // ------------------------------------------------------------------------
211
212 /**
213 * Cache Info
214 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300215 * @param string $type = 'user' user/filehits
216 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300217 */
218 public function cache_info($type = 'user')
219 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400220 return $this->{$this->_adapter}->cache_info($type);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300221 }
222
223 // ------------------------------------------------------------------------
224
225 /**
226 * Get Cache Metadata
227 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300228 * @param string $id key to get cache metadata on
229 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300230 */
231 public function get_metadata($id)
232 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400233 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300234 }
235
236 // ------------------------------------------------------------------------
237
238 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600239 * Is the requested driver supported in this environment?
240 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300241 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300242 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600243 */
244 public function is_supported($driver)
245 {
Andrey Andreev070ce052015-06-22 13:24:11 +0300246 static $support;
Greg Akerbde25d92010-12-21 09:31:21 -0600247
Andrey Andreev070ce052015-06-22 13:24:11 +0300248 if ( ! isset($support, $support[$driver]))
Greg Akerbde25d92010-12-21 09:31:21 -0600249 {
250 $support[$driver] = $this->{$driver}->is_supported();
251 }
252
253 return $support[$driver];
254 }
Greg Akerbde25d92010-12-21 09:31:21 -0600255}