blob: 73dec72abe3ac127b8e37401df7fd34e2a346f11 [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 }
107
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400108 if (isset($config['backup']) && in_array($config['backup'], $this->valid_drivers))
109 {
110 $this->_backup_driver = $config['backup'];
111 }
112
vkeranov94b1f762012-10-27 18:19:59 +0300113 isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
Andrey Andreev58c2b102012-10-26 14:42:29 +0300114
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500115 // If the specified adapter isn't available, check the backup.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400116 if ( ! $this->is_supported($this->_adapter))
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500117 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400118 if ( ! $this->is_supported($this->_backup_driver))
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500119 {
120 // Backup isn't supported either. Default to 'Dummy' driver.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400121 log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
122 $this->_adapter = 'dummy';
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500123 }
124 else
125 {
126 // Backup is supported. Set it to primary.
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400127 log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
128 $this->_adapter = $this->_backup_driver;
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500129 }
130 }
Greg Akerbde25d92010-12-21 09:31:21 -0600131 }
132
133 // ------------------------------------------------------------------------
134
135 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300136 * Get
137 *
138 * Look for a value in the cache. If it exists, return the data
139 * if not, return FALSE
140 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300141 * @param string $id
142 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300143 */
144 public function get($id)
145 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400146 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300147 }
148
149 // ------------------------------------------------------------------------
150
151 /**
152 * Cache Save
153 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200154 * @param string $id Cache ID
155 * @param mixed $data Data to store
156 * @param int $ttl Cache TTL (in seconds)
157 * @param bool $raw Whether to store the raw value
Andrey Andreev58c2b102012-10-26 14:42:29 +0300158 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300159 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200160 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreevb24b0332012-03-26 15:34:39 +0300161 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400162 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300163 }
164
165 // ------------------------------------------------------------------------
166
167 /**
168 * Delete from Cache
169 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300170 * @param string $id Cache ID
171 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300172 */
173 public function delete($id)
174 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400175 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300176 }
177
178 // ------------------------------------------------------------------------
179
180 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200181 * Increment a raw value
182 *
183 * @param string $id Cache ID
184 * @param int $offset Step/value to add
185 * @return mixed New value on success or FALSE on failure
186 */
187 public function increment($id, $offset = 1)
188 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400189 return $this->{$this->_adapter}->increment($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200190 }
191
192 // ------------------------------------------------------------------------
193
194 /**
195 * Decrement a raw value
196 *
197 * @param string $id Cache ID
198 * @param int $offset Step/value to reduce by
199 * @return mixed New value on success or FALSE on failure
200 */
201 public function decrement($id, $offset = 1)
202 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400203 return $this->{$this->_adapter}->decrement($id, $offset);
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200204 }
205
206 // ------------------------------------------------------------------------
207
208 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300209 * Clean the cache
210 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300211 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300212 */
213 public function clean()
214 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400215 return $this->{$this->_adapter}->clean();
Andrey Andreevb24b0332012-03-26 15:34:39 +0300216 }
217
218 // ------------------------------------------------------------------------
219
220 /**
221 * Cache Info
222 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300223 * @param string $type = 'user' user/filehits
224 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300225 */
226 public function cache_info($type = 'user')
227 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400228 return $this->{$this->_adapter}->cache_info($type);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300229 }
230
231 // ------------------------------------------------------------------------
232
233 /**
234 * Get Cache Metadata
235 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300236 * @param string $id key to get cache metadata on
237 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300238 */
239 public function get_metadata($id)
240 {
Tyler Brownell0416a7f2015-04-24 10:25:38 -0400241 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300242 }
243
244 // ------------------------------------------------------------------------
245
246 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600247 * Is the requested driver supported in this environment?
248 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300249 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300250 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600251 */
252 public function is_supported($driver)
253 {
254 static $support = array();
255
256 if ( ! isset($support[$driver]))
257 {
258 $support[$driver] = $this->{$driver}->is_supported();
259 }
260
261 return $support[$driver];
262 }
263
Greg Akerbde25d92010-12-21 09:31:21 -0600264}