Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/cache/file.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
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-2010, 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-2010, 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 if ($this->settings['lock']) { | |
140 $this->_File->lock = true; | |
141 } | |
142 $expires = time() + $duration; | |
143 $contents = $expires . $lineBreak . $data . $lineBreak; | |
144 $success = $this->_File->write($contents); | |
145 $this->_File->close(); | |
146 return $success; | |
147 } | |
148 | |
149 /** | |
150 * Read a key from the cache | |
151 * | |
152 * @param string $key Identifier for the data | |
153 * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it | |
154 * @access public | |
155 */ | |
156 function read($key) { | |
157 if ($this->_setKey($key) === false || !$this->_init || !$this->_File->exists()) { | |
158 return false; | |
159 } | |
160 if ($this->settings['lock']) { | |
161 $this->_File->lock = true; | |
162 } | |
163 $time = time(); | |
164 $cachetime = intval($this->_File->read(11)); | |
165 | |
166 if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) { | |
167 $this->_File->close(); | |
168 return false; | |
169 } | |
170 $data = $this->_File->read(true); | |
171 | |
172 if ($data !== '' && !empty($this->settings['serialize'])) { | |
173 if ($this->settings['isWindows']) { | |
174 $data = str_replace('\\\\\\\\', '\\', $data); | |
175 } | |
176 $data = unserialize((string)$data); | |
177 } | |
178 $this->_File->close(); | |
179 return $data; | |
180 } | |
181 | |
182 /** | |
183 * Delete a key from the cache | |
184 * | |
185 * @param string $key Identifier for the data | |
186 * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed | |
187 * @access public | |
188 */ | |
189 function delete($key) { | |
190 if ($this->_setKey($key) === false || !$this->_init) { | |
191 return false; | |
192 } | |
193 return $this->_File->delete(); | |
194 } | |
195 | |
196 /** | |
197 * Delete all values from the cache | |
198 * | |
199 * @param boolean $check Optional - only delete expired cache items | |
200 * @return boolean True if the cache was succesfully cleared, false otherwise | |
201 * @access public | |
202 */ | |
203 function clear($check) { | |
204 if (!$this->_init) { | |
205 return false; | |
206 } | |
207 $dir = dir($this->settings['path']); | |
208 if ($check) { | |
209 $now = time(); | |
210 $threshold = $now - $this->settings['duration']; | |
211 } | |
212 $prefixLength = strlen($this->settings['prefix']); | |
213 while (($entry = $dir->read()) !== false) { | |
214 if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) { | |
215 continue; | |
216 } | |
217 if ($this->_setKey($entry) === false) { | |
218 continue; | |
219 } | |
220 if ($check) { | |
221 $mtime = $this->_File->lastChange(); | |
222 | |
223 if ($mtime === false || $mtime > $threshold) { | |
224 continue; | |
225 } | |
226 | |
227 $expires = $this->_File->read(11); | |
228 $this->_File->close(); | |
229 | |
230 if ($expires > $now) { | |
231 continue; | |
232 } | |
233 } | |
234 $this->_File->delete(); | |
235 } | |
236 $dir->close(); | |
237 return true; | |
238 } | |
239 | |
240 /** | |
241 * Get absolute file for a given key | |
242 * | |
243 * @param string $key The key | |
244 * @return mixed Absolute cache file for the given key or false if erroneous | |
245 * @access private | |
246 */ | |
247 function _setKey($key) { | |
248 $this->_File->Folder->cd($this->settings['path']); | |
249 if ($key !== $this->_File->name) { | |
250 $this->_File->name = $key; | |
251 $this->_File->path = null; | |
252 } | |
253 if (!$this->_File->Folder->inPath($this->_File->pwd(), true)) { | |
254 return false; | |
255 } | |
256 } | |
257 | |
258 /** | |
259 * Determine is cache directory is writable | |
260 * | |
261 * @return boolean | |
262 * @access private | |
263 */ | |
264 function __active() { | |
265 if ($this->_init && !is_writable($this->settings['path'])) { | |
266 $this->_init = false; | |
267 trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING); | |
268 return false; | |
269 } | |
270 return true; | |
271 } | |
272 } |