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.
17
* @subpackage Zend_Cache_Backend
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
24
* @see Zend_Cache_Backend_Interface
26
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
29
* @see Zend_Cache_Backend
31
require_once 'Zend/Cache/Backend.php';
36
* @subpackage Zend_Cache_Backend
37
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
38
* @license http://framework.zend.com/license/new-bsd New BSD License
40
class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
45
const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend';
46
const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend';
51
* @param array $options associative array of options
52
* @throws Zend_Cache_Exception
55
public function __construct(array $options = array())
57
if (!extension_loaded('apc')) {
58
Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
60
parent::__construct($options);
64
* Test if a cache is available for the given id and (if yes) return it (false else)
66
* WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
68
* @param string $id cache id
69
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
70
* @return string cached datas (or false)
72
public function load($id, $doNotTestCacheValidity = false)
74
$tmp = apc_fetch($id);
82
* Test if a cache is available or not (for the given id)
84
* @param string $id cache id
85
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
87
public function test($id)
89
$tmp = apc_fetch($id);
97
* Save some string datas into a cache record
99
* Note : $data is always "string" (serialization is done by the
100
* core not by the backend)
102
* @param string $data datas to cache
103
* @param string $id cache id
104
* @param array $tags array of strings, the cache record will be tagged by each string entry
105
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
106
* @return boolean true if no problem
108
public function save($data, $id, $tags = array(), $specificLifetime = false)
110
$lifetime = $this->getLifetime($specificLifetime);
111
$result = apc_store($id, array($data, time(), $lifetime), $lifetime);
112
if (count($tags) > 0) {
113
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
119
* Remove a cache record
121
* @param string $id cache id
122
* @return boolean true if no problem
124
public function remove($id)
126
return apc_delete($id);
130
* Clean some cache records
132
* Available modes are :
133
* 'all' (default) => remove all cache entries ($tags is not used)
134
* 'old' => unsupported
135
* 'matchingTag' => unsupported
136
* 'notMatchingTag' => unsupported
137
* 'matchingAnyTag' => unsupported
139
* @param string $mode clean mode
140
* @param array $tags array of tags
141
* @throws Zend_Cache_Exception
142
* @return boolean true if no problem
144
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
147
case Zend_Cache::CLEANING_MODE_ALL:
148
return apc_clear_cache('user');
150
case Zend_Cache::CLEANING_MODE_OLD:
151
$this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
153
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
154
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
155
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
156
$this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
159
Zend_Cache::throwException('Invalid mode for clean() method');
165
* Return true if the automatic cleaning is available for the backend
167
* DEPRECATED : use getCapabilities() instead
172
public function isAutomaticCleaningAvailable()
178
* Return the filling percentage of the backend storage
180
* @throws Zend_Cache_Exception
181
* @return int integer between 0 and 100
183
public function getFillingPercentage()
185
$mem = apc_sma_info(true);
186
$memSize = $mem['num_seg'] * $mem['seg_size'];
187
$memAvailable= $mem['avail_mem'];
188
$memUsed = $memSize - $memAvailable;
190
Zend_Cache::throwException('can\'t get apc memory size');
192
if ($memUsed > $memSize) {
195
return ((int) (100. * ($memUsed / $memSize)));
199
* Return an array of stored tags
201
* @return array array of stored tags (string)
203
public function getTags()
205
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
210
* Return an array of stored cache ids which match given tags
212
* In case of multiple tags, a logical AND is made between tags
214
* @param array $tags array of tags
215
* @return array array of matching cache ids (string)
217
public function getIdsMatchingTags($tags = array())
219
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
224
* Return an array of stored cache ids which don't match given tags
226
* In case of multiple tags, a logical OR is made between tags
228
* @param array $tags array of tags
229
* @return array array of not matching cache ids (string)
231
public function getIdsNotMatchingTags($tags = array())
233
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
238
* Return an array of stored cache ids which match any given tags
240
* In case of multiple tags, a logical AND is made between tags
242
* @param array $tags array of tags
243
* @return array array of any matching cache ids (string)
245
public function getIdsMatchingAnyTags($tags = array())
247
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
252
* Return an array of stored cache ids
254
* @return array array of stored cache ids (string)
256
public function getIds()
259
$array = apc_cache_info('user', false);
260
$records = $array['cache_list'];
261
foreach ($records as $record) {
262
$res[] = $record['info'];
268
* Return an array of metadatas for the given cache id
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
275
* @param string $id cache id
276
* @return array array of metadatas (false if the cache id is not found)
278
public function getMetadatas($id)
280
$tmp = apc_fetch($id);
281
if (is_array($tmp)) {
284
if (!isset($tmp[2])) {
285
// because this record is only with 1.7 release
286
// if old cache records are still there...
291
'expire' => $mtime + $lifetime,
300
* Give (if possible) an extra lifetime to the given cache id
302
* @param string $id cache id
303
* @param int $extraLifetime
304
* @return boolean true if ok
306
public function touch($id, $extraLifetime)
308
$tmp = apc_fetch($id);
309
if (is_array($tmp)) {
312
if (!isset($tmp[2])) {
313
// because this record is only with 1.7 release
314
// if old cache records are still there...
318
$newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
319
if ($newLifetime <=0) {
322
apc_store($id, array($data, time(), $newLifetime), $newLifetime);
329
* Return an associative array of capabilities (booleans) of the backend
331
* The array must include these keys :
332
* - automatic_cleaning (is automating cleaning necessary)
333
* - tags (are tags supported)
334
* - expired_read (is it possible to read expired cache records
335
* (for doNotTestCacheValidity option for example))
336
* - priority does the backend deal with priority when saving
337
* - infinite_lifetime (is infinite lifetime can work with this backend)
338
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
340
* @return array associative of with capabilities
342
public function getCapabilities()
345
'automatic_cleaning' => false,
347
'expired_read' => false,
349
'infinite_lifetime' => false,