00001 <?php
00027 require_once 'Zend/Cache/Backend/ExtendedInterface.php';
00028
00032 require_once 'Zend/Cache/Backend.php';
00033
00034
00041 class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
00042 {
00046 const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend';
00047 const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend';
00048
00056 public function __construct(array $options = array())
00057 {
00058 if (!extension_loaded('apc')) {
00059 Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
00060 }
00061 parent::__construct($options);
00062 }
00063
00073 public function load($id, $doNotTestCacheValidity = false)
00074 {
00075 $tmp = apc_fetch($id);
00076 if (is_array($tmp)) {
00077 return $tmp[0];
00078 }
00079 return false;
00080 }
00081
00088 public function test($id)
00089 {
00090 $tmp = apc_fetch($id);
00091 if (is_array($tmp)) {
00092 return $tmp[1];
00093 }
00094 return false;
00095 }
00096
00109 public function save($data, $id, $tags = array(), $specificLifetime = false)
00110 {
00111 $lifetime = $this->getLifetime($specificLifetime);
00112 $result = apc_store($id, array($data, time(), $lifetime), $lifetime);
00113 if (count($tags) > 0) {
00114 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
00115 }
00116 return $result;
00117 }
00118
00125 public function remove($id)
00126 {
00127 return apc_delete($id);
00128 }
00129
00145 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
00146 {
00147 switch ($mode) {
00148 case Zend_Cache::CLEANING_MODE_ALL:
00149 return apc_clear_cache('user');
00150 break;
00151 case Zend_Cache::CLEANING_MODE_OLD:
00152 $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
00153 break;
00154 case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
00155 case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
00156 case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
00157 $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
00158 break;
00159 default:
00160 Zend_Cache::throwException('Invalid mode for clean() method');
00161 break;
00162 }
00163 }
00164
00173 public function isAutomaticCleaningAvailable()
00174 {
00175 return false;
00176 }
00177
00184 public function getFillingPercentage()
00185 {
00186 $mem = apc_sma_info(true);
00187 $memSize = $mem['num_seg'] * $mem['seg_size'];
00188 $memAvailable= $mem['avail_mem'];
00189 $memUsed = $memSize - $memAvailable;
00190 if ($memSize == 0) {
00191 Zend_Cache::throwException('can\'t get apc memory size');
00192 }
00193 if ($memUsed > $memSize) {
00194 return 100;
00195 }
00196 return ((int) (100. * ($memUsed / $memSize)));
00197 }
00198
00204 public function getTags()
00205 {
00206 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
00207 return array();
00208 }
00209
00218 public function getIdsMatchingTags($tags = array())
00219 {
00220 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
00221 return array();
00222 }
00223
00232 public function getIdsNotMatchingTags($tags = array())
00233 {
00234 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
00235 return array();
00236 }
00237
00246 public function getIdsMatchingAnyTags($tags = array())
00247 {
00248 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
00249 return array();
00250 }
00251
00257 public function getIds()
00258 {
00259 $res = array();
00260 $array = apc_cache_info('user', false);
00261 $records = $array['cache_list'];
00262 foreach ($records as $record) {
00263 $res[] = $record['info'];
00264 }
00265 return $res;
00266 }
00267
00279 public function getMetadatas($id)
00280 {
00281 $tmp = apc_fetch($id);
00282 if (is_array($tmp)) {
00283 $data = $tmp[0];
00284 $mtime = $tmp[1];
00285 if (!isset($tmp[2])) {
00286
00287
00288 return false;
00289 }
00290 $lifetime = $tmp[2];
00291 return array(
00292 'expire' => $mtime + $lifetime,
00293 'tags' => array(),
00294 'mtime' => $mtime
00295 );
00296 }
00297 return false;
00298 }
00299
00307 public function touch($id, $extraLifetime)
00308 {
00309 $tmp = apc_fetch($id);
00310 if (is_array($tmp)) {
00311 $data = $tmp[0];
00312 $mtime = $tmp[1];
00313 if (!isset($tmp[2])) {
00314
00315
00316 return false;
00317 }
00318 $lifetime = $tmp[2];
00319 $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
00320 if ($newLifetime <=0) {
00321 return false;
00322 }
00323 apc_store($id, array($data, time(), $newLifetime), $newLifetime);
00324 return true;
00325 }
00326 return false;
00327 }
00328
00343 public function getCapabilities()
00344 {
00345 return array(
00346 'automatic_cleaning' => false,
00347 'tags' => false,
00348 'expired_read' => false,
00349 'priority' => false,
00350 'infinite_lifetime' => false,
00351 'get_list' => true
00352 );
00353 }
00354
00355 }