|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Apc.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
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-2010 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_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface |
|
42 { |
|
43 /** |
|
44 * Log message |
|
45 */ |
|
46 const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend'; |
|
47 const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc 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('apc')) { |
|
59 Zend_Cache::throwException('The apc 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 Apc 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 = apc_fetch($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 = apc_fetch($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 = apc_store($id, array($data, time(), $lifetime), $lifetime); |
|
113 if (count($tags) > 0) { |
|
114 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_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 apc_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 apc_clear_cache('user'); |
|
150 break; |
|
151 case Zend_Cache::CLEANING_MODE_OLD: |
|
152 $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc 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_APC_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 = apc_sma_info(true); |
|
187 $memSize = $mem['num_seg'] * $mem['seg_size']; |
|
188 $memAvailable= $mem['avail_mem']; |
|
189 $memUsed = $memSize - $memAvailable; |
|
190 if ($memSize == 0) { |
|
191 Zend_Cache::throwException('can\'t get apc memory size'); |
|
192 } |
|
193 if ($memUsed > $memSize) { |
|
194 return 100; |
|
195 } |
|
196 return ((int) (100. * ($memUsed / $memSize))); |
|
197 } |
|
198 |
|
199 /** |
|
200 * Return an array of stored tags |
|
201 * |
|
202 * @return array array of stored tags (string) |
|
203 */ |
|
204 public function getTags() |
|
205 { |
|
206 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND); |
|
207 return array(); |
|
208 } |
|
209 |
|
210 /** |
|
211 * Return an array of stored cache ids which match given tags |
|
212 * |
|
213 * In case of multiple tags, a logical AND is made between tags |
|
214 * |
|
215 * @param array $tags array of tags |
|
216 * @return array array of matching cache ids (string) |
|
217 */ |
|
218 public function getIdsMatchingTags($tags = array()) |
|
219 { |
|
220 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND); |
|
221 return array(); |
|
222 } |
|
223 |
|
224 /** |
|
225 * Return an array of stored cache ids which don't match given tags |
|
226 * |
|
227 * In case of multiple tags, a logical OR is made between tags |
|
228 * |
|
229 * @param array $tags array of tags |
|
230 * @return array array of not matching cache ids (string) |
|
231 */ |
|
232 public function getIdsNotMatchingTags($tags = array()) |
|
233 { |
|
234 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND); |
|
235 return array(); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Return an array of stored cache ids which match any given tags |
|
240 * |
|
241 * In case of multiple tags, a logical AND is made between tags |
|
242 * |
|
243 * @param array $tags array of tags |
|
244 * @return array array of any matching cache ids (string) |
|
245 */ |
|
246 public function getIdsMatchingAnyTags($tags = array()) |
|
247 { |
|
248 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND); |
|
249 return array(); |
|
250 } |
|
251 |
|
252 /** |
|
253 * Return an array of stored cache ids |
|
254 * |
|
255 * @return array array of stored cache ids (string) |
|
256 */ |
|
257 public function getIds() |
|
258 { |
|
259 $res = array(); |
|
260 $array = apc_cache_info('user', false); |
|
261 $records = $array['cache_list']; |
|
262 foreach ($records as $record) { |
|
263 $res[] = $record['info']; |
|
264 } |
|
265 return $res; |
|
266 } |
|
267 |
|
268 /** |
|
269 * Return an array of metadatas for the given cache id |
|
270 * |
|
271 * The array must include these keys : |
|
272 * - expire : the expire timestamp |
|
273 * - tags : a string array of tags |
|
274 * - mtime : timestamp of last modification time |
|
275 * |
|
276 * @param string $id cache id |
|
277 * @return array array of metadatas (false if the cache id is not found) |
|
278 */ |
|
279 public function getMetadatas($id) |
|
280 { |
|
281 $tmp = apc_fetch($id); |
|
282 if (is_array($tmp)) { |
|
283 $data = $tmp[0]; |
|
284 $mtime = $tmp[1]; |
|
285 if (!isset($tmp[2])) { |
|
286 // because this record is only with 1.7 release |
|
287 // if old cache records are still there... |
|
288 return false; |
|
289 } |
|
290 $lifetime = $tmp[2]; |
|
291 return array( |
|
292 'expire' => $mtime + $lifetime, |
|
293 'tags' => array(), |
|
294 'mtime' => $mtime |
|
295 ); |
|
296 } |
|
297 return false; |
|
298 } |
|
299 |
|
300 /** |
|
301 * Give (if possible) an extra lifetime to the given cache id |
|
302 * |
|
303 * @param string $id cache id |
|
304 * @param int $extraLifetime |
|
305 * @return boolean true if ok |
|
306 */ |
|
307 public function touch($id, $extraLifetime) |
|
308 { |
|
309 $tmp = apc_fetch($id); |
|
310 if (is_array($tmp)) { |
|
311 $data = $tmp[0]; |
|
312 $mtime = $tmp[1]; |
|
313 if (!isset($tmp[2])) { |
|
314 // because this record is only with 1.7 release |
|
315 // if old cache records are still there... |
|
316 return false; |
|
317 } |
|
318 $lifetime = $tmp[2]; |
|
319 $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime; |
|
320 if ($newLifetime <=0) { |
|
321 return false; |
|
322 } |
|
323 apc_store($id, array($data, time(), $newLifetime), $newLifetime); |
|
324 return true; |
|
325 } |
|
326 return false; |
|
327 } |
|
328 |
|
329 /** |
|
330 * Return an associative array of capabilities (booleans) of the backend |
|
331 * |
|
332 * The array must include these keys : |
|
333 * - automatic_cleaning (is automating cleaning necessary) |
|
334 * - tags (are tags supported) |
|
335 * - expired_read (is it possible to read expired cache records |
|
336 * (for doNotTestCacheValidity option for example)) |
|
337 * - priority does the backend deal with priority when saving |
|
338 * - infinite_lifetime (is infinite lifetime can work with this backend) |
|
339 * - get_list (is it possible to get the list of cache ids and the complete list of tags) |
|
340 * |
|
341 * @return array associative of with capabilities |
|
342 */ |
|
343 public function getCapabilities() |
|
344 { |
|
345 return array( |
|
346 'automatic_cleaning' => false, |
|
347 'tags' => false, |
|
348 'expired_read' => false, |
|
349 'priority' => false, |
|
350 'infinite_lifetime' => false, |
|
351 'get_list' => true |
|
352 ); |
|
353 } |
|
354 |
|
355 } |