Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Apc.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Cache/Backend/ExtendedInterface.php';
28 
32 #require_once 'Zend/Cache/Backend.php';
33 
34 
42 {
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 
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 
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 
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 
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 
125  public function remove($id)
126  {
127  return apc_delete($id);
128  }
129 
145  public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
146  {
147  switch ($mode) {
149  return apc_clear_cache('user');
150  break;
152  $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
153  break;
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 
174  {
175  return false;
176  }
177 
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 
204  public function getTags()
205  {
206  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
207  return array();
208  }
209 
218  public function getIdsMatchingTags($tags = array())
219  {
220  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
221  return array();
222  }
223 
232  public function getIdsNotMatchingTags($tags = array())
233  {
234  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
235  return array();
236  }
237 
246  public function getIdsMatchingAnyTags($tags = array())
247  {
248  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
249  return array();
250  }
251 
257  public function getIds()
258  {
259  $ids = array();
260  $iterator = new APCIterator('user', null, APC_ITER_KEY);
261  foreach ($iterator as $item) {
262  $ids[] = $item['key'];
263  }
264 
265  return $ids;
266  }
267 
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 
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 
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 }
clean($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
Definition: Apc.php:145
$id
Definition: fieldset.phtml:14
load($id, $doNotTestCacheValidity=false)
Definition: Apc.php:73
__construct(array $options=array())
Definition: Apc.php:56
touch($id, $extraLifetime)
Definition: Apc.php:307
const CLEANING_MODE_OLD
Definition: Cache.php:73
_log($message, $priority=4)
Definition: Backend.php:273
getIdsMatchingAnyTags($tags=array())
Definition: Apc.php:246
getIdsMatchingTags($tags=array())
Definition: Apc.php:218
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND
Definition: Apc.php:46
isAutomaticCleaningAvailable()
Definition: Apc.php:173
getIdsNotMatchingTags($tags=array())
Definition: Apc.php:232
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74
const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND
Definition: Apc.php:47
getLifetime($specificLifetime)
Definition: Backend.php:143
save($data, $id, $tags=array(), $specificLifetime=false)
Definition: Apc.php:109