|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Cache |
|
17 * @subpackage Zend_Cache_Backend |
|
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id$ |
|
21 */ |
|
22 |
|
23 |
|
24 /** |
|
25 * @see Zend_Cache_Backend_Interface |
|
26 */ |
|
27 require_once 'Zend/Cache/Backend/ExtendedInterface.php'; |
|
28 |
|
29 /** |
|
30 * @see Zend_Cache_Backend |
|
31 */ |
|
32 require_once 'Zend/Cache/Backend.php'; |
|
33 |
|
34 |
|
35 /** |
|
36 * @package Zend_Cache |
|
37 * @subpackage Zend_Cache_Backend |
|
38 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
39 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
40 */ |
|
41 class Zend_Cache_Backend_WinCache extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface |
|
42 { |
|
43 /** |
|
44 * Log message |
|
45 */ |
|
46 const TAGS_UNSUPPORTED_BY_CLEAN_OF_WINCACHE_BACKEND = 'Zend_Cache_Backend_WinCache::clean() : tags are unsupported by the WinCache backend'; |
|
47 const TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND = 'Zend_Cache_Backend_WinCache::save() : tags are unsupported by the WinCache backend'; |
|
48 |
|
49 /** |
|
50 * Constructor |
|
51 * |
|
52 * @param array $options associative array of options |
|
53 * @throws Zend_Cache_Exception |
|
54 * @return void |
|
55 */ |
|
56 public function __construct(array $options = array()) |
|
57 { |
|
58 if (!extension_loaded('wincache')) { |
|
59 Zend_Cache::throwException('The wincache extension must be loaded for using this backend !'); |
|
60 } |
|
61 parent::__construct($options); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Test if a cache is available for the given id and (if yes) return it (false else) |
|
66 * |
|
67 * WARNING $doNotTestCacheValidity=true is unsupported by the WinCache backend |
|
68 * |
|
69 * @param string $id cache id |
|
70 * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
|
71 * @return string cached datas (or false) |
|
72 */ |
|
73 public function load($id, $doNotTestCacheValidity = false) |
|
74 { |
|
75 $tmp = wincache_ucache_get($id); |
|
76 if (is_array($tmp)) { |
|
77 return $tmp[0]; |
|
78 } |
|
79 return false; |
|
80 } |
|
81 |
|
82 /** |
|
83 * Test if a cache is available or not (for the given id) |
|
84 * |
|
85 * @param string $id cache id |
|
86 * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record |
|
87 */ |
|
88 public function test($id) |
|
89 { |
|
90 $tmp = wincache_ucache_get($id); |
|
91 if (is_array($tmp)) { |
|
92 return $tmp[1]; |
|
93 } |
|
94 return false; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Save some string datas into a cache record |
|
99 * |
|
100 * Note : $data is always "string" (serialization is done by the |
|
101 * core not by the backend) |
|
102 * |
|
103 * @param string $data datas to cache |
|
104 * @param string $id cache id |
|
105 * @param array $tags array of strings, the cache record will be tagged by each string entry |
|
106 * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime) |
|
107 * @return boolean true if no problem |
|
108 */ |
|
109 public function save($data, $id, $tags = array(), $specificLifetime = false) |
|
110 { |
|
111 $lifetime = $this->getLifetime($specificLifetime); |
|
112 $result = wincache_ucache_set($id, array($data, time(), $lifetime), $lifetime); |
|
113 if (count($tags) > 0) { |
|
114 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND); |
|
115 } |
|
116 return $result; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Remove a cache record |
|
121 * |
|
122 * @param string $id cache id |
|
123 * @return boolean true if no problem |
|
124 */ |
|
125 public function remove($id) |
|
126 { |
|
127 return wincache_ucache_delete($id); |
|
128 } |
|
129 |
|
130 /** |
|
131 * Clean some cache records |
|
132 * |
|
133 * Available modes are : |
|
134 * 'all' (default) => remove all cache entries ($tags is not used) |
|
135 * 'old' => unsupported |
|
136 * 'matchingTag' => unsupported |
|
137 * 'notMatchingTag' => unsupported |
|
138 * 'matchingAnyTag' => unsupported |
|
139 * |
|
140 * @param string $mode clean mode |
|
141 * @param array $tags array of tags |
|
142 * @throws Zend_Cache_Exception |
|
143 * @return boolean true if no problem |
|
144 */ |
|
145 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) |
|
146 { |
|
147 switch ($mode) { |
|
148 case Zend_Cache::CLEANING_MODE_ALL: |
|
149 return wincache_ucache_clear(); |
|
150 break; |
|
151 case Zend_Cache::CLEANING_MODE_OLD: |
|
152 $this->_log("Zend_Cache_Backend_WinCache::clean() : CLEANING_MODE_OLD is unsupported by the WinCache backend"); |
|
153 break; |
|
154 case Zend_Cache::CLEANING_MODE_MATCHING_TAG: |
|
155 case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: |
|
156 case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG: |
|
157 $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_WINCACHE_BACKEND); |
|
158 break; |
|
159 default: |
|
160 Zend_Cache::throwException('Invalid mode for clean() method'); |
|
161 break; |
|
162 } |
|
163 } |
|
164 |
|
165 /** |
|
166 * Return true if the automatic cleaning is available for the backend |
|
167 * |
|
168 * DEPRECATED : use getCapabilities() instead |
|
169 * |
|
170 * @deprecated |
|
171 * @return boolean |
|
172 */ |
|
173 public function isAutomaticCleaningAvailable() |
|
174 { |
|
175 return false; |
|
176 } |
|
177 |
|
178 /** |
|
179 * Return the filling percentage of the backend storage |
|
180 * |
|
181 * @throws Zend_Cache_Exception |
|
182 * @return int integer between 0 and 100 |
|
183 */ |
|
184 public function getFillingPercentage() |
|
185 { |
|
186 $mem = wincache_ucache_meminfo(); |
|
187 $memSize = $mem['memory_total']; |
|
188 $memUsed = $memSize - $mem['memory_free']; |
|
189 if ($memSize == 0) { |
|
190 Zend_Cache::throwException('can\'t get WinCache memory size'); |
|
191 } |
|
192 if ($memUsed > $memSize) { |
|
193 return 100; |
|
194 } |
|
195 return ((int) (100. * ($memUsed / $memSize))); |
|
196 } |
|
197 |
|
198 /** |
|
199 * Return an array of stored tags |
|
200 * |
|
201 * @return array array of stored tags (string) |
|
202 */ |
|
203 public function getTags() |
|
204 { |
|
205 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND); |
|
206 return array(); |
|
207 } |
|
208 |
|
209 /** |
|
210 * Return an array of stored cache ids which match given tags |
|
211 * |
|
212 * In case of multiple tags, a logical AND is made between tags |
|
213 * |
|
214 * @param array $tags array of tags |
|
215 * @return array array of matching cache ids (string) |
|
216 */ |
|
217 public function getIdsMatchingTags($tags = array()) |
|
218 { |
|
219 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND); |
|
220 return array(); |
|
221 } |
|
222 |
|
223 /** |
|
224 * Return an array of stored cache ids which don't match given tags |
|
225 * |
|
226 * In case of multiple tags, a logical OR is made between tags |
|
227 * |
|
228 * @param array $tags array of tags |
|
229 * @return array array of not matching cache ids (string) |
|
230 */ |
|
231 public function getIdsNotMatchingTags($tags = array()) |
|
232 { |
|
233 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND); |
|
234 return array(); |
|
235 } |
|
236 |
|
237 /** |
|
238 * Return an array of stored cache ids which match any given tags |
|
239 * |
|
240 * In case of multiple tags, a logical AND is made between tags |
|
241 * |
|
242 * @param array $tags array of tags |
|
243 * @return array array of any matching cache ids (string) |
|
244 */ |
|
245 public function getIdsMatchingAnyTags($tags = array()) |
|
246 { |
|
247 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND); |
|
248 return array(); |
|
249 } |
|
250 |
|
251 /** |
|
252 * Return an array of stored cache ids |
|
253 * |
|
254 * @return array array of stored cache ids (string) |
|
255 */ |
|
256 public function getIds() |
|
257 { |
|
258 $res = array(); |
|
259 $array = wincache_ucache_info(); |
|
260 $records = $array['ucache_entries']; |
|
261 foreach ($records as $record) { |
|
262 $res[] = $record['key_name']; |
|
263 } |
|
264 return $res; |
|
265 } |
|
266 |
|
267 /** |
|
268 * Return an array of metadatas for the given cache id |
|
269 * |
|
270 * The array must include these keys : |
|
271 * - expire : the expire timestamp |
|
272 * - tags : a string array of tags |
|
273 * - mtime : timestamp of last modification time |
|
274 * |
|
275 * @param string $id cache id |
|
276 * @return array array of metadatas (false if the cache id is not found) |
|
277 */ |
|
278 public function getMetadatas($id) |
|
279 { |
|
280 $tmp = wincache_ucache_get($id); |
|
281 if (is_array($tmp)) { |
|
282 $data = $tmp[0]; |
|
283 $mtime = $tmp[1]; |
|
284 if (!isset($tmp[2])) { |
|
285 return false; |
|
286 } |
|
287 $lifetime = $tmp[2]; |
|
288 return array( |
|
289 'expire' => $mtime + $lifetime, |
|
290 'tags' => array(), |
|
291 'mtime' => $mtime |
|
292 ); |
|
293 } |
|
294 return false; |
|
295 } |
|
296 |
|
297 /** |
|
298 * Give (if possible) an extra lifetime to the given cache id |
|
299 * |
|
300 * @param string $id cache id |
|
301 * @param int $extraLifetime |
|
302 * @return boolean true if ok |
|
303 */ |
|
304 public function touch($id, $extraLifetime) |
|
305 { |
|
306 $tmp = wincache_ucache_get($id); |
|
307 if (is_array($tmp)) { |
|
308 $data = $tmp[0]; |
|
309 $mtime = $tmp[1]; |
|
310 if (!isset($tmp[2])) { |
|
311 return false; |
|
312 } |
|
313 $lifetime = $tmp[2]; |
|
314 $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime; |
|
315 if ($newLifetime <=0) { |
|
316 return false; |
|
317 } |
|
318 return wincache_ucache_set($id, array($data, time(), $newLifetime), $newLifetime); |
|
319 } |
|
320 return false; |
|
321 } |
|
322 |
|
323 /** |
|
324 * Return an associative array of capabilities (booleans) of the backend |
|
325 * |
|
326 * The array must include these keys : |
|
327 * - automatic_cleaning (is automating cleaning necessary) |
|
328 * - tags (are tags supported) |
|
329 * - expired_read (is it possible to read expired cache records |
|
330 * (for doNotTestCacheValidity option for example)) |
|
331 * - priority does the backend deal with priority when saving |
|
332 * - infinite_lifetime (is infinite lifetime can work with this backend) |
|
333 * - get_list (is it possible to get the list of cache ids and the complete list of tags) |
|
334 * |
|
335 * @return array associative of with capabilities |
|
336 */ |
|
337 public function getCapabilities() |
|
338 { |
|
339 return array( |
|
340 'automatic_cleaning' => false, |
|
341 'tags' => false, |
|
342 'expired_read' => false, |
|
343 'priority' => false, |
|
344 'infinite_lifetime' => false, |
|
345 'get_list' => true |
|
346 ); |
|
347 } |
|
348 |
|
349 } |