blob: ff68a4fd80d14c65652343a6861c6bc50f2a8a77 [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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 Brownell0416a7f2015-04-24 10:25:38 -0400103 if (isset($config['adapter']) && in_array($config['adapter'], $this->valid_drivers))
Greg Akerbde25d92010-12-21 09:31:21 -0600104 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400105 $this->_adapter = $config['adapter'];
Greg Akerbde25d92010-12-21 09:31:21 -0600106 }
Tyler Brownellb7661222015-04-27 11:54:08 -0400107 elseif (! in_array($config['adapter'], $this->valid_drivers))
108 {
109 log_message('error', 'Cache adapter "'.$config['adapter'].'" is invalid.');
110 }
Greg Akerbde25d92010-12-21 09:31:21 -0600111
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400112 if (isset($config['backup']) && in_array($config['backup'], $this->valid_drivers))
113 {
114 $this->_backup_driver = $config['backup'];
115 }
Tyler Brownellb7661222015-04-27 11:54:08 -0400116 elseif (! in_array($config['backup'], $this->valid_drivers))
117 {
118 log_message('error', 'Cache backup adapter "'.$config['backup'].'" is invalid.');
119 }
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400120
vkeranov94b1f762012-10-27 18:19:59 +0300121 isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
Andrey Andreev58c2b102012-10-26 14:42:29 +0300122
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500123 // If the specified adapter isn't available, check the backup.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400124 if ( ! $this->is_supported($this->_adapter))
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500125 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400126 if ( ! $this->is_supported($this->_backup_driver))
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500127 {
128 // Backup isn't supported either. Default to 'Dummy' driver.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400129 log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
130 $this->_adapter = 'dummy';
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500131 }
132 else
133 {
134 // Backup is supported. Set it to primary.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400135 log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
136 $this->_adapter = $this->_backup_driver;
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500137 }
138 }
Greg Akerbde25d92010-12-21 09:31:21 -0600139 }
140
141 // ------------------------------------------------------------------------
142
143 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300144 * Get
145 *
146 * Look for a value in the cache. If it exists, return the data
147 * if not, return FALSE
148 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300149 * @param string $id
150 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300151 */
152 public function get($id)
153 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400154 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300155 }
156
157 // ------------------------------------------------------------------------
158
159 /**
160 * Cache Save
161 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200162 * @param string $id Cache ID
163 * @param mixed $data Data to store
164 * @param int $ttl Cache TTL (in seconds)
165 * @param bool $raw Whether to store the raw value
Andrey Andreev58c2b102012-10-26 14:42:29 +0300166 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300167 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200168 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreevb24b0332012-03-26 15:34:39 +0300169 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400170 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300171 }
172
173 // ------------------------------------------------------------------------
174
175 /**
176 * Delete from Cache
177 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300178 * @param string $id Cache ID
179 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300180 */
181 public function delete($id)
182 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400183 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300184 }
185
186 // ------------------------------------------------------------------------
187
188 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200189 * Increment a raw value
190 *
191 * @param string $id Cache ID
192 * @param int $offset Step/value to add
193 * @return mixed New value on success or FALSE on failure
194 */
195 public function increment($id, $offset = 1)
196 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400197 return $this->{$this->_adapter}->increment($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200198 }
199
200 // ------------------------------------------------------------------------
201
202 /**
203 * Decrement a raw value
204 *
205 * @param string $id Cache ID
206 * @param int $offset Step/value to reduce by
207 * @return mixed New value on success or FALSE on failure
208 */
209 public function decrement($id, $offset = 1)
210 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400211 return $this->{$this->_adapter}->decrement($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200212 }
213
214 // ------------------------------------------------------------------------
215
216 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300217 * Clean the cache
218 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300219 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300220 */
221 public function clean()
222 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400223 return $this->{$this->_adapter}->clean();
Andrey Andreevb24b0332012-03-26 15:34:39 +0300224 }
225
226 // ------------------------------------------------------------------------
227
228 /**
229 * Cache Info
230 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300231 * @param string $type = 'user' user/filehits
232 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300233 */
234 public function cache_info($type = 'user')
235 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400236 return $this->{$this->_adapter}->cache_info($type);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300237 }
238
239 // ------------------------------------------------------------------------
240
241 /**
242 * Get Cache Metadata
243 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300244 * @param string $id key to get cache metadata on
245 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300246 */
247 public function get_metadata($id)
248 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400249 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300250 }
251
252 // ------------------------------------------------------------------------
253
254 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600255 * Is the requested driver supported in this environment?
256 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300257 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300258 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600259 */
260 public function is_supported($driver)
261 {
262 static $support = array();
263
264 if ( ! isset($support[$driver]))
265 {
266 $support[$driver] = $this->{$driver}->is_supported();
267 }
268
269 return $support[$driver];
270 }
271
Greg Akerbde25d92010-12-21 09:31:21 -0600272}