blob: 2dffa350c20c59edfff68cc8dfff1e994e5af45d [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 *
7 * NOTICE OF LICENSE
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Akerbde25d92010-12-21 09:31:21 -060018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Greg Akerbde25d92010-12-21 09:31:21 -060023 * @link http://codeigniter.com
24 * @since Version 2.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060028
Greg Akerbde25d92010-12-21 09:31:21 -060029/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020030 * CodeIgniter Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060031 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010038class CI_Cache extends CI_Driver_Library {
Andrey Andreev7d4ea072011-12-25 19:23:50 +020039
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Valid cache drivers
42 *
43 * @var array
44 */
Andrey Andreev58c2b102012-10-26 14:42:29 +030045 protected $valid_drivers = array(
dchill426262d052012-11-24 18:41:13 -050046 'apc',
47 'dummy',
48 'file',
49 'memcached',
50 'redis',
51 'wincache'
Anton Lindqvist30930422012-04-25 12:32:58 +020052 );
Greg Akerbde25d92010-12-21 09:31:21 -060053
Timothy Warren0688ac92012-04-20 10:25:04 -040054 /**
55 * Path of cache files (if file-based cache)
56 *
57 * @var string
58 */
59 protected $_cache_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030060
Timothy Warren0688ac92012-04-20 10:25:04 -040061 /**
62 * Reference to the driver
63 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040064 * @var mixed
Timothy Warren0688ac92012-04-20 10:25:04 -040065 */
66 protected $_adapter = 'dummy';
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warren0688ac92012-04-20 10:25:04 -040068 /**
69 * Fallback driver
70 *
Andrey Andreev58c2b102012-10-26 14:42:29 +030071 * @var string
Timothy Warren0688ac92012-04-20 10:25:04 -040072 */
Eric Robertsbf50a3b2012-05-27 19:04:43 -050073 protected $_backup_driver = 'dummy';
Andrey Andreev7d4ea072011-12-25 19:23:50 +020074
Greg Akerbde25d92010-12-21 09:31:21 -060075 /**
Andrey Andreev58c2b102012-10-26 14:42:29 +030076 * Cache key prefix
77 *
78 * @var string
79 */
80 public $key_prefix = '';
81
82 /**
Greg Akerbde25d92010-12-21 09:31:21 -060083 * Constructor
84 *
Greg Akerbde25d92010-12-21 09:31:21 -060085 * Initialize class properties based on the configuration array.
86 *
Andrey Andreev58c2b102012-10-26 14:42:29 +030087 * @param array $config = array()
Andrey Andreevb24b0332012-03-26 15:34:39 +030088 * @return void
Greg Akerbde25d92010-12-21 09:31:21 -060089 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030090 public function __construct($config = array())
Greg Aker03abee32011-12-25 00:31:29 -060091 {
Greg Akerbde25d92010-12-21 09:31:21 -060092 $default_config = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040093 'adapter',
94 'memcached'
95 );
Greg Akerbde25d92010-12-21 09:31:21 -060096
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
vkeranov94b1f762012-10-27 18:19:59 +0300107 isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
Andrey Andreev58c2b102012-10-26 14:42:29 +0300108
Tyler Brownell516527c2013-07-24 10:51:04 -0400109 if (isset($config['backup']) && in_array($config['backup'], $this->valid_drivers))
Greg Akerbde25d92010-12-21 09:31:21 -0600110 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300111 $this->_backup_driver = $config['backup'];
Greg Akerbde25d92010-12-21 09:31:21 -0600112 }
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500113
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 Brownell516527c2013-07-24 10:51:04 -0400126 log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500127 $this->_adapter = $this->_backup_driver;
128 }
129 }
Greg Akerbde25d92010-12-21 09:31:21 -0600130 }
131
132 // ------------------------------------------------------------------------
133
134 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300135 * Get
136 *
137 * Look for a value in the cache. If it exists, return the data
138 * if not, return FALSE
139 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300140 * @param string $id
141 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300142 */
143 public function get($id)
144 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300145 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300146 }
147
148 // ------------------------------------------------------------------------
149
150 /**
151 * Cache Save
152 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200153 * @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 Andreev58c2b102012-10-26 14:42:29 +0300157 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300158 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200159 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreevb24b0332012-03-26 15:34:39 +0300160 {
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200161 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300162 }
163
164 // ------------------------------------------------------------------------
165
166 /**
167 * Delete from Cache
168 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300169 * @param string $id Cache ID
170 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300171 */
172 public function delete($id)
173 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300174 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300175 }
176
177 // ------------------------------------------------------------------------
178
179 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200180 * 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 Andreevb24b0332012-03-26 15:34:39 +0300208 * Clean the cache
209 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300210 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300211 */
212 public function clean()
213 {
214 return $this->{$this->_adapter}->clean();
215 }
216
217 // ------------------------------------------------------------------------
218
219 /**
220 * Cache Info
221 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300222 * @param string $type = 'user' user/filehits
223 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300224 */
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 Andreev58c2b102012-10-26 14:42:29 +0300235 * @param string $id key to get cache metadata on
236 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300237 */
238 public function get_metadata($id)
239 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300240 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300241 }
242
243 // ------------------------------------------------------------------------
244
245 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600246 * Is the requested driver supported in this environment?
247 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300248 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300249 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600250 */
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 Akerbde25d92010-12-21 09:31:21 -0600263}
Greg Akerbde25d92010-12-21 09:31:21 -0600264
265/* End of file Cache.php */
Andrey Andreev9e674f72012-06-09 21:02:52 +0300266/* Location: ./system/libraries/Cache/Cache.php */