1 | <?php |
---|
2 | /** |
---|
3 | * File Storage engine for cache |
---|
4 | * |
---|
5 | * |
---|
6 | * PHP versions 4 and 5 |
---|
7 | * |
---|
8 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
9 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
10 | * |
---|
11 | * Licensed under The MIT License |
---|
12 | * Redistributions of files must retain the above copyright notice. |
---|
13 | * |
---|
14 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
15 | * @link http://cakephp.org CakePHP(tm) Project |
---|
16 | * @package cake |
---|
17 | * @subpackage cake.cake.libs.cache |
---|
18 | * @since CakePHP(tm) v 1.2.0.4933 |
---|
19 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
20 | */ |
---|
21 | |
---|
22 | if (!class_exists('File')) { |
---|
23 | require LIBS . 'file.php'; |
---|
24 | } |
---|
25 | /** |
---|
26 | * File Storage engine for cache |
---|
27 | * |
---|
28 | * @todo use the File and Folder classes (if it's not a too big performance hit) |
---|
29 | * @package cake |
---|
30 | * @subpackage cake.cake.libs.cache |
---|
31 | */ |
---|
32 | class FileEngine extends CacheEngine { |
---|
33 | |
---|
34 | /** |
---|
35 | * Instance of File class |
---|
36 | * |
---|
37 | * @var File |
---|
38 | * @access protected |
---|
39 | */ |
---|
40 | var $_File = null; |
---|
41 | |
---|
42 | /** |
---|
43 | * Settings |
---|
44 | * |
---|
45 | * - path = absolute path to cache directory, default => CACHE |
---|
46 | * - prefix = string prefix for filename, default => cake_ |
---|
47 | * - lock = enable file locking on write, default => false |
---|
48 | * - serialize = serialize the data, default => true |
---|
49 | * |
---|
50 | * @var array |
---|
51 | * @see CacheEngine::__defaults |
---|
52 | * @access public |
---|
53 | */ |
---|
54 | var $settings = array(); |
---|
55 | |
---|
56 | /** |
---|
57 | * True unless FileEngine::__active(); fails |
---|
58 | * |
---|
59 | * @var boolean |
---|
60 | * @access protected |
---|
61 | */ |
---|
62 | var $_init = true; |
---|
63 | |
---|
64 | /** |
---|
65 | * Initialize the Cache Engine |
---|
66 | * |
---|
67 | * Called automatically by the cache frontend |
---|
68 | * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); |
---|
69 | * |
---|
70 | * @param array $setting array of setting for the engine |
---|
71 | * @return boolean True if the engine has been successfully initialized, false if not |
---|
72 | * @access public |
---|
73 | */ |
---|
74 | function init($settings = array()) { |
---|
75 | parent::init(array_merge( |
---|
76 | array( |
---|
77 | 'engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false, |
---|
78 | 'serialize'=> true, 'isWindows' => false |
---|
79 | ), |
---|
80 | $settings |
---|
81 | )); |
---|
82 | if (!isset($this->_File)) { |
---|
83 | $this->_File =& new File($this->settings['path'] . DS . 'cake'); |
---|
84 | } |
---|
85 | |
---|
86 | if (DIRECTORY_SEPARATOR === '\\') { |
---|
87 | $this->settings['isWindows'] = true; |
---|
88 | } |
---|
89 | |
---|
90 | $path = $this->_File->Folder->cd($this->settings['path']); |
---|
91 | if ($path) { |
---|
92 | $this->settings['path'] = $path; |
---|
93 | } |
---|
94 | return $this->__active(); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Garbage collection. Permanently remove all expired and deleted data |
---|
99 | * |
---|
100 | * @return boolean True if garbage collection was succesful, false on failure |
---|
101 | * @access public |
---|
102 | */ |
---|
103 | function gc() { |
---|
104 | return $this->clear(true); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Write data for key into cache |
---|
109 | * |
---|
110 | * @param string $key Identifier for the data |
---|
111 | * @param mixed $data Data to be cached |
---|
112 | * @param mixed $duration How long to cache the data, in seconds |
---|
113 | * @return boolean True if the data was succesfully cached, false on failure |
---|
114 | * @access public |
---|
115 | */ |
---|
116 | function write($key, &$data, $duration) { |
---|
117 | if ($data === '' || !$this->_init) { |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | if ($this->_setKey($key) === false) { |
---|
122 | return false; |
---|
123 | } |
---|
124 | |
---|
125 | $lineBreak = "\n"; |
---|
126 | |
---|
127 | if ($this->settings['isWindows']) { |
---|
128 | $lineBreak = "\r\n"; |
---|
129 | } |
---|
130 | |
---|
131 | if (!empty($this->settings['serialize'])) { |
---|
132 | if ($this->settings['isWindows']) { |
---|
133 | $data = str_replace('\\', '\\\\\\\\', serialize($data)); |
---|
134 | } else { |
---|
135 | $data = serialize($data); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | $expires = time() + $duration; |
---|
140 | $contents = $expires . $lineBreak . $data . $lineBreak; |
---|
141 | |
---|
142 | if (!$handle = fopen($this->_File->path, 'a')) { |
---|
143 | return false; |
---|
144 | } |
---|
145 | |
---|
146 | if ($this->settings['lock']) { |
---|
147 | flock($handle, LOCK_EX); |
---|
148 | } |
---|
149 | |
---|
150 | $success = ftruncate($handle, 0) && fwrite($handle, $contents) && fflush($handle); |
---|
151 | |
---|
152 | if ($this->settings['lock']) { |
---|
153 | flock($handle, LOCK_UN); |
---|
154 | } |
---|
155 | |
---|
156 | fclose($handle); |
---|
157 | return $success; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Read a key from the cache |
---|
162 | * |
---|
163 | * @param string $key Identifier for the data |
---|
164 | * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it |
---|
165 | * @access public |
---|
166 | */ |
---|
167 | function read($key) { |
---|
168 | if ($this->_setKey($key) === false || !$this->_init || !$this->_File->exists()) { |
---|
169 | return false; |
---|
170 | } |
---|
171 | if ($this->settings['lock']) { |
---|
172 | $this->_File->lock = true; |
---|
173 | } |
---|
174 | $time = time(); |
---|
175 | $cachetime = intval($this->_File->read(11)); |
---|
176 | |
---|
177 | if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) { |
---|
178 | $this->_File->close(); |
---|
179 | return false; |
---|
180 | } |
---|
181 | $data = $this->_File->read(true); |
---|
182 | |
---|
183 | if ($data !== '' && !empty($this->settings['serialize'])) { |
---|
184 | if ($this->settings['isWindows']) { |
---|
185 | $data = str_replace('\\\\\\\\', '\\', $data); |
---|
186 | } |
---|
187 | $data = unserialize((string)$data); |
---|
188 | } |
---|
189 | $this->_File->close(); |
---|
190 | return $data; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Delete a key from the cache |
---|
195 | * |
---|
196 | * @param string $key Identifier for the data |
---|
197 | * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed |
---|
198 | * @access public |
---|
199 | */ |
---|
200 | function delete($key) { |
---|
201 | if ($this->_setKey($key) === false || !$this->_init) { |
---|
202 | return false; |
---|
203 | } |
---|
204 | return $this->_File->delete(); |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Delete all values from the cache |
---|
209 | * |
---|
210 | * @param boolean $check Optional - only delete expired cache items |
---|
211 | * @return boolean True if the cache was succesfully cleared, false otherwise |
---|
212 | * @access public |
---|
213 | */ |
---|
214 | function clear($check) { |
---|
215 | if (!$this->_init) { |
---|
216 | return false; |
---|
217 | } |
---|
218 | $dir = dir($this->settings['path']); |
---|
219 | if ($check) { |
---|
220 | $now = time(); |
---|
221 | $threshold = $now - $this->settings['duration']; |
---|
222 | } |
---|
223 | $prefixLength = strlen($this->settings['prefix']); |
---|
224 | while (($entry = $dir->read()) !== false) { |
---|
225 | if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) { |
---|
226 | continue; |
---|
227 | } |
---|
228 | if ($this->_setKey($entry) === false) { |
---|
229 | continue; |
---|
230 | } |
---|
231 | if ($check) { |
---|
232 | $mtime = $this->_File->lastChange(); |
---|
233 | |
---|
234 | if ($mtime === false || $mtime > $threshold) { |
---|
235 | continue; |
---|
236 | } |
---|
237 | |
---|
238 | $expires = $this->_File->read(11); |
---|
239 | $this->_File->close(); |
---|
240 | |
---|
241 | if ($expires > $now) { |
---|
242 | continue; |
---|
243 | } |
---|
244 | } |
---|
245 | $this->_File->delete(); |
---|
246 | } |
---|
247 | $dir->close(); |
---|
248 | return true; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * Get absolute file for a given key |
---|
253 | * |
---|
254 | * @param string $key The key |
---|
255 | * @return mixed Absolute cache file for the given key or false if erroneous |
---|
256 | * @access private |
---|
257 | */ |
---|
258 | function _setKey($key) { |
---|
259 | $this->_File->Folder->cd($this->settings['path']); |
---|
260 | if ($key !== $this->_File->name) { |
---|
261 | $this->_File->name = $key; |
---|
262 | $this->_File->path = null; |
---|
263 | } |
---|
264 | if (!$this->_File->Folder->inPath($this->_File->pwd(), true)) { |
---|
265 | return false; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Determine is cache directory is writable |
---|
271 | * |
---|
272 | * @return boolean |
---|
273 | * @access private |
---|
274 | */ |
---|
275 | function __active() { |
---|
276 | if ($this->_init && !is_writable($this->settings['path'])) { |
---|
277 | $this->_init = false; |
---|
278 | trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING); |
---|
279 | return false; |
---|
280 | } |
---|
281 | return true; |
---|
282 | } |
---|
283 | } |
---|