blob: ec2626a1dd13487d1ea0c97f40554dc80064859b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @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 */
77 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 */
Eric Robertsbf50a3b2012-05-27 19:04:43 -050084 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 {
Greg Akerbde25d92010-12-21 09:31:21 -0600103 $default_config = array(
Timothy Warren0688ac92012-04-20 10:25:04 -0400104 'adapter',
105 'memcached'
106 );
Greg Akerbde25d92010-12-21 09:31:21 -0600107
108 foreach ($default_config as $key)
109 {
110 if (isset($config[$key]))
111 {
112 $param = '_'.$key;
113
114 $this->{$param} = $config[$key];
115 }
116 }
117
vkeranov94b1f762012-10-27 18:19:59 +0300118 isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
Andrey Andreev58c2b102012-10-26 14:42:29 +0300119
Tyler Brownell516527c2013-07-24 10:51:04 -0400120 if (isset($config['backup']) && in_array($config['backup'], $this->valid_drivers))
Greg Akerbde25d92010-12-21 09:31:21 -0600121 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300122 $this->_backup_driver = $config['backup'];
Greg Akerbde25d92010-12-21 09:31:21 -0600123 }
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500124
125 // If the specified adapter isn't available, check the backup.
126 if ( ! $this->is_supported($this->_adapter))
127 {
128 if ( ! $this->is_supported($this->_backup_driver))
129 {
130 // Backup isn't supported either. Default to 'Dummy' driver.
131 log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
132 $this->_adapter = 'dummy';
133 }
134 else
135 {
136 // Backup is supported. Set it to primary.
Tyler Brownell516527c2013-07-24 10:51:04 -0400137 log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500138 $this->_adapter = $this->_backup_driver;
139 }
140 }
Greg Akerbde25d92010-12-21 09:31:21 -0600141 }
142
143 // ------------------------------------------------------------------------
144
145 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300146 * Get
147 *
148 * Look for a value in the cache. If it exists, return the data
149 * if not, return FALSE
150 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300151 * @param string $id
152 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300153 */
154 public function get($id)
155 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300156 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300157 }
158
159 // ------------------------------------------------------------------------
160
161 /**
162 * Cache Save
163 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200164 * @param string $id Cache ID
165 * @param mixed $data Data to store
166 * @param int $ttl Cache TTL (in seconds)
167 * @param bool $raw Whether to store the raw value
Andrey Andreev58c2b102012-10-26 14:42:29 +0300168 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300169 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200170 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreevb24b0332012-03-26 15:34:39 +0300171 {
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200172 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300173 }
174
175 // ------------------------------------------------------------------------
176
177 /**
178 * Delete from Cache
179 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300180 * @param string $id Cache ID
181 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300182 */
183 public function delete($id)
184 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300185 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300186 }
187
188 // ------------------------------------------------------------------------
189
190 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200191 * Increment a raw value
192 *
193 * @param string $id Cache ID
194 * @param int $offset Step/value to add
195 * @return mixed New value on success or FALSE on failure
196 */
197 public function increment($id, $offset = 1)
198 {
199 return $this->{$this->_adapter}->increment($id, $offset);
200 }
201
202 // ------------------------------------------------------------------------
203
204 /**
205 * Decrement a raw value
206 *
207 * @param string $id Cache ID
208 * @param int $offset Step/value to reduce by
209 * @return mixed New value on success or FALSE on failure
210 */
211 public function decrement($id, $offset = 1)
212 {
213 return $this->{$this->_adapter}->decrement($id, $offset);
214 }
215
216 // ------------------------------------------------------------------------
217
218 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300219 * Clean the cache
220 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300221 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300222 */
223 public function clean()
224 {
225 return $this->{$this->_adapter}->clean();
226 }
227
228 // ------------------------------------------------------------------------
229
230 /**
231 * Cache Info
232 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300233 * @param string $type = 'user' user/filehits
234 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300235 */
236 public function cache_info($type = 'user')
237 {
238 return $this->{$this->_adapter}->cache_info($type);
239 }
240
241 // ------------------------------------------------------------------------
242
243 /**
244 * Get Cache Metadata
245 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300246 * @param string $id key to get cache metadata on
247 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300248 */
249 public function get_metadata($id)
250 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300251 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300252 }
253
254 // ------------------------------------------------------------------------
255
256 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600257 * Is the requested driver supported in this environment?
258 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300259 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300260 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600261 */
262 public function is_supported($driver)
263 {
264 static $support = array();
265
266 if ( ! isset($support[$driver]))
267 {
268 $support[$driver] = $this->{$driver}->is_supported();
269 }
270
271 return $support[$driver];
272 }
273
Greg Akerbde25d92010-12-21 09:31:21 -0600274}
Greg Akerbde25d92010-12-21 09:31:21 -0600275
276/* End of file Cache.php */
Andrey Andreev9e674f72012-06-09 21:02:52 +0300277/* Location: ./system/libraries/Cache/Cache.php */