| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2254 |
lars |
9 |
* Copyright (c) 2014 - 2017, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* 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:
|
|
|
17 |
*
|
|
|
18 |
* 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
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2254 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 2.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CodeIgniter Caching Class
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Libraries
|
|
|
45 |
* @category Core
|
|
|
46 |
* @author EllisLab Dev Team
|
|
|
47 |
* @link
|
|
|
48 |
*/
|
|
|
49 |
class CI_Cache extends CI_Driver_Library {
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Valid cache drivers
|
|
|
53 |
*
|
|
|
54 |
* @var array
|
|
|
55 |
*/
|
|
|
56 |
protected $valid_drivers = array(
|
|
|
57 |
'apc',
|
|
|
58 |
'dummy',
|
|
|
59 |
'file',
|
|
|
60 |
'memcached',
|
|
|
61 |
'redis',
|
|
|
62 |
'wincache'
|
|
|
63 |
);
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Path of cache files (if file-based cache)
|
|
|
67 |
*
|
|
|
68 |
* @var string
|
|
|
69 |
*/
|
|
|
70 |
protected $_cache_path = NULL;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Reference to the driver
|
|
|
74 |
*
|
|
|
75 |
* @var mixed
|
|
|
76 |
*/
|
|
|
77 |
protected $_adapter = 'dummy';
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Fallback driver
|
|
|
81 |
*
|
|
|
82 |
* @var string
|
|
|
83 |
*/
|
|
|
84 |
protected $_backup_driver = 'dummy';
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Cache key prefix
|
|
|
88 |
*
|
|
|
89 |
* @var string
|
|
|
90 |
*/
|
|
|
91 |
public $key_prefix = '';
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Constructor
|
|
|
95 |
*
|
|
|
96 |
* Initialize class properties based on the configuration array.
|
|
|
97 |
*
|
|
|
98 |
* @param array $config = array()
|
|
|
99 |
* @return void
|
|
|
100 |
*/
|
|
|
101 |
public function __construct($config = array())
|
|
|
102 |
{
|
|
|
103 |
isset($config['adapter']) && $this->_adapter = $config['adapter'];
|
|
|
104 |
isset($config['backup']) && $this->_backup_driver = $config['backup'];
|
|
|
105 |
isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
|
|
|
106 |
|
|
|
107 |
// If the specified adapter isn't available, check the backup.
|
|
|
108 |
if ( ! $this->is_supported($this->_adapter))
|
|
|
109 |
{
|
|
|
110 |
if ( ! $this->is_supported($this->_backup_driver))
|
|
|
111 |
{
|
|
|
112 |
// Backup isn't supported either. Default to 'Dummy' driver.
|
|
|
113 |
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';
|
|
|
115 |
}
|
|
|
116 |
else
|
|
|
117 |
{
|
|
|
118 |
// Backup is supported. Set it to primary.
|
|
|
119 |
log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
|
|
|
120 |
$this->_adapter = $this->_backup_driver;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
// ------------------------------------------------------------------------
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Get
|
|
|
129 |
*
|
|
|
130 |
* Look for a value in the cache. If it exists, return the data
|
|
|
131 |
* if not, return FALSE
|
|
|
132 |
*
|
|
|
133 |
* @param string $id
|
|
|
134 |
* @return mixed value matching $id or FALSE on failure
|
|
|
135 |
*/
|
|
|
136 |
public function get($id)
|
|
|
137 |
{
|
|
|
138 |
return $this->{$this->_adapter}->get($this->key_prefix.$id);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
// ------------------------------------------------------------------------
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Cache Save
|
|
|
145 |
*
|
|
|
146 |
* @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
|
|
|
150 |
* @return bool TRUE on success, FALSE on failure
|
|
|
151 |
*/
|
|
|
152 |
public function save($id, $data, $ttl = 60, $raw = FALSE)
|
|
|
153 |
{
|
|
|
154 |
return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// ------------------------------------------------------------------------
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Delete from Cache
|
|
|
161 |
*
|
|
|
162 |
* @param string $id Cache ID
|
|
|
163 |
* @return bool TRUE on success, FALSE on failure
|
|
|
164 |
*/
|
|
|
165 |
public function delete($id)
|
|
|
166 |
{
|
|
|
167 |
return $this->{$this->_adapter}->delete($this->key_prefix.$id);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// ------------------------------------------------------------------------
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* 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 |
{
|
|
|
181 |
return $this->{$this->_adapter}->increment($this->key_prefix.$id, $offset);
|
|
|
182 |
}
|
|
|
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 |
{
|
|
|
195 |
return $this->{$this->_adapter}->decrement($this->key_prefix.$id, $offset);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
// ------------------------------------------------------------------------
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* Clean the cache
|
|
|
202 |
*
|
|
|
203 |
* @return bool TRUE on success, FALSE on failure
|
|
|
204 |
*/
|
|
|
205 |
public function clean()
|
|
|
206 |
{
|
|
|
207 |
return $this->{$this->_adapter}->clean();
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
// ------------------------------------------------------------------------
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Cache Info
|
|
|
214 |
*
|
|
|
215 |
* @param string $type = 'user' user/filehits
|
|
|
216 |
* @return mixed array containing cache info on success OR FALSE on failure
|
|
|
217 |
*/
|
|
|
218 |
public function cache_info($type = 'user')
|
|
|
219 |
{
|
|
|
220 |
return $this->{$this->_adapter}->cache_info($type);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// ------------------------------------------------------------------------
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Get Cache Metadata
|
|
|
227 |
*
|
|
|
228 |
* @param string $id key to get cache metadata on
|
|
|
229 |
* @return mixed cache item metadata
|
|
|
230 |
*/
|
|
|
231 |
public function get_metadata($id)
|
|
|
232 |
{
|
|
|
233 |
return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
// ------------------------------------------------------------------------
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Is the requested driver supported in this environment?
|
|
|
240 |
*
|
|
|
241 |
* @param string $driver The driver to test
|
|
|
242 |
* @return array
|
|
|
243 |
*/
|
|
|
244 |
public function is_supported($driver)
|
|
|
245 |
{
|
|
|
246 |
static $support;
|
|
|
247 |
|
|
|
248 |
if ( ! isset($support, $support[$driver]))
|
|
|
249 |
{
|
|
|
250 |
$support[$driver] = $this->{$driver}->is_supported();
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
return $support[$driver];
|
|
|
254 |
}
|
|
|
255 |
}
|