Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Zend_Cache_Backend_TwoLevels Class Reference
Inheritance diagram for Zend_Cache_Backend_TwoLevels:
Zend_Cache_Backend Zend_Cache_Backend_ExtendedInterface Zend_Cache_Backend_Interface

Public Member Functions

 __construct (array $options=array())
 
 test ($id)
 
 save ($data, $id, $tags=array(), $specificLifetime=false, $priority=8)
 
 load ($id, $doNotTestCacheValidity=false)
 
 remove ($id)
 
 clean ($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
 
 getIds ()
 
 getTags ()
 
 getIdsMatchingTags ($tags=array())
 
 getIdsNotMatchingTags ($tags=array())
 
 getIdsMatchingAnyTags ($tags=array())
 
 getFillingPercentage ()
 
 getMetadatas ($id)
 
 touch ($id, $extraLifetime)
 
 getCapabilities ()
 
 ___expire ($id)
 
- Public Member Functions inherited from Zend_Cache_Backend
 __construct (array $options=array())
 
 setDirectives ($directives)
 
 setOption ($name, $value)
 
 getOption ($name)
 
 getLifetime ($specificLifetime)
 
 isAutomaticCleaningAvailable ()
 
 getTmpDir ()
 
- Public Member Functions inherited from Zend_Cache_Backend_Interface
 setDirectives ($directives)
 
 save ($data, $id, $tags=array(), $specificLifetime=false)
 

Protected Attributes

 $_options
 
 $_slowBackend
 
 $_fastBackend
 
 $_fastBackendFillingPercentage = null
 
- Protected Attributes inherited from Zend_Cache_Backend
 $_directives
 
 $_options = array()
 

Additional Inherited Members

- Protected Member Functions inherited from Zend_Cache_Backend
 _isGoodTmpDir ($dir)
 
 _loggerSanity ()
 
 _log ($message, $priority=4)
 

Detailed Description

Definition at line 42 of file TwoLevels.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $options = array())

Constructor

Parameters
array$optionsAssociative array of options
Exceptions
Zend_Cache_Exception
Returns
void

Definition at line 127 of file TwoLevels.php.

128  {
129  parent::__construct($options);
130 
131  if ($this->_options['slow_backend'] === null) {
132  Zend_Cache::throwException('slow_backend option has to set');
133  } elseif ($this->_options['slow_backend'] instanceof Zend_Cache_Backend_ExtendedInterface) {
134  $this->_slowBackend = $this->_options['slow_backend'];
135  } else {
136  $this->_slowBackend = Zend_Cache::_makeBackend(
137  $this->_options['slow_backend'],
138  $this->_options['slow_backend_options'],
139  $this->_options['slow_backend_custom_naming'],
140  $this->_options['slow_backend_autoload']
141  );
142  if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_slowBackend))) {
143  Zend_Cache::throwException('slow_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
144  }
145  }
146 
147  if ($this->_options['fast_backend'] === null) {
148  Zend_Cache::throwException('fast_backend option has to set');
149  } elseif ($this->_options['fast_backend'] instanceof Zend_Cache_Backend_ExtendedInterface) {
150  $this->_fastBackend = $this->_options['fast_backend'];
151  } else {
152  $this->_fastBackend = Zend_Cache::_makeBackend(
153  $this->_options['fast_backend'],
154  $this->_options['fast_backend_options'],
155  $this->_options['fast_backend_custom_naming'],
156  $this->_options['fast_backend_autoload']
157  );
158  if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_fastBackend))) {
159  Zend_Cache::throwException('fast_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
160  }
161  }
162 
163  $this->_slowBackend->setDirectives($this->_directives);
164  $this->_fastBackend->setDirectives($this->_directives);
165  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
static _makeBackend($backend, $backendOptions, $customBackendNaming=false, $autoload=false)
Definition: Cache.php:124

Member Function Documentation

◆ ___expire()

___expire (   $id)

PUBLIC METHOD FOR UNIT TESTING ONLY !

Force a cache record to expire

Parameters
string$idcache id

Definition at line 518 of file TwoLevels.php.

519  {
520  $this->_fastBackend->remove($id);
521  $this->_slowBackend->___expire($id);
522  }
$id
Definition: fieldset.phtml:14

◆ clean()

clean (   $mode = Zend_Cache::CLEANING_MODE_ALL,
  $tags = array() 
)

Clean some cache records

Available modes are : Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags ($tags can be an array of strings or a single string)

Parameters
string$modeClean mode
array$tagsArray of tags
Exceptions
Zend_Cache_Exception
Returns
boolean true if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 296 of file TwoLevels.php.

297  {
298  switch($mode) {
300  $boolFast = $this->_fastBackend->clean(Zend_Cache::CLEANING_MODE_ALL);
301  $boolSlow = $this->_slowBackend->clean(Zend_Cache::CLEANING_MODE_ALL);
302  return $boolFast && $boolSlow;
303  break;
305  return $this->_slowBackend->clean(Zend_Cache::CLEANING_MODE_OLD);
307  $ids = $this->_slowBackend->getIdsMatchingTags($tags);
308  $res = true;
309  foreach ($ids as $id) {
310  $bool = $this->remove($id);
311  $res = $res && $bool;
312  }
313  return $res;
314  break;
316  $ids = $this->_slowBackend->getIdsNotMatchingTags($tags);
317  $res = true;
318  foreach ($ids as $id) {
319  $bool = $this->remove($id);
320  $res = $res && $bool;
321  }
322  return $res;
323  break;
325  $ids = $this->_slowBackend->getIdsMatchingAnyTags($tags);
326  $res = true;
327  foreach ($ids as $id) {
328  $bool = $this->remove($id);
329  $res = $res && $bool;
330  }
331  return $res;
332  break;
333  default:
334  Zend_Cache::throwException('Invalid mode for clean() method');
335  break;
336  }
337  }
$id
Definition: fieldset.phtml:14
const CLEANING_MODE_OLD
Definition: Cache.php:73
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 CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74

◆ getCapabilities()

getCapabilities ( )

Return an associative array of capabilities (booleans) of the backend

The array must include these keys :

  • automatic_cleaning (is automating cleaning necessary)
  • tags (are tags supported)
  • expired_read (is it possible to read expired cache records (for doNotTestCacheValidity option for example))
  • priority does the backend deal with priority when saving
  • infinite_lifetime (is infinite lifetime can work with this backend)
  • get_list (is it possible to get the list of cache ids and the complete list of tags)
Returns
array associative of with capabilities

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 450 of file TwoLevels.php.

451  {
452  $slowBackendCapabilities = $this->_slowBackend->getCapabilities();
453  return array(
454  'automatic_cleaning' => $slowBackendCapabilities['automatic_cleaning'],
455  'tags' => $slowBackendCapabilities['tags'],
456  'expired_read' => $slowBackendCapabilities['expired_read'],
457  'priority' => $slowBackendCapabilities['priority'],
458  'infinite_lifetime' => $slowBackendCapabilities['infinite_lifetime'],
459  'get_list' => $slowBackendCapabilities['get_list']
460  );
461  }

◆ getFillingPercentage()

getFillingPercentage ( )

Return the filling percentage of the backend storage

Returns
int integer between 0 and 100

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 403 of file TwoLevels.php.

404  {
405  return $this->_slowBackend->getFillingPercentage();
406  }

◆ getIds()

getIds ( )

Return an array of stored cache ids

Returns
array array of stored cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 344 of file TwoLevels.php.

345  {
346  return $this->_slowBackend->getIds();
347  }

◆ getIdsMatchingAnyTags()

getIdsMatchingAnyTags (   $tags = array())

Return an array of stored cache ids which match any given tags

In case of multiple tags, a logical AND is made between tags

Parameters
array$tagsarray of tags
Returns
array array of any matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 393 of file TwoLevels.php.

394  {
395  return $this->_slowBackend->getIdsMatchingAnyTags($tags);
396  }

◆ getIdsMatchingTags()

getIdsMatchingTags (   $tags = array())

Return an array of stored cache ids which match given tags

In case of multiple tags, a logical AND is made between tags

Parameters
array$tagsarray of tags
Returns
array array of matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 367 of file TwoLevels.php.

368  {
369  return $this->_slowBackend->getIdsMatchingTags($tags);
370  }

◆ getIdsNotMatchingTags()

getIdsNotMatchingTags (   $tags = array())

Return an array of stored cache ids which don't match given tags

In case of multiple tags, a logical OR is made between tags

Parameters
array$tagsarray of tags
Returns
array array of not matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 380 of file TwoLevels.php.

381  {
382  return $this->_slowBackend->getIdsNotMatchingTags($tags);
383  }

◆ getMetadatas()

getMetadatas (   $id)

Return an array of metadatas for the given cache id

The array must include these keys :

  • expire : the expire timestamp
  • tags : a string array of tags
  • mtime : timestamp of last modification time
Parameters
string$idcache id
Returns
array array of metadatas (false if the cache id is not found)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 419 of file TwoLevels.php.

420  {
421  return $this->_slowBackend->getMetadatas($id);
422  }
$id
Definition: fieldset.phtml:14

◆ getTags()

getTags ( )

Return an array of stored tags

Returns
array array of stored tags (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 354 of file TwoLevels.php.

355  {
356  return $this->_slowBackend->getTags();
357  }

◆ load()

load (   $id,
  $doNotTestCacheValidity = false 
)

Test if a cache is available for the given id and (if yes) return it (false else)

Note : return value is always "string" (unserialization is done by the core not by the backend)

Parameters
string$idCache id
boolean$doNotTestCacheValidityIf set to true, the cache validity won't be tested
Returns
string|false cached datas

Implements Zend_Cache_Backend_Interface.

Definition at line 230 of file TwoLevels.php.

231  {
232  $resultFast = $this->_fastBackend->load($id, $doNotTestCacheValidity);
233  if ($resultFast === false) {
234  $resultSlow = $this->_slowBackend->load($id, $doNotTestCacheValidity);
235  if ($resultSlow === false) {
236  // there is no cache at all for this id
237  return false;
238  }
239  }
240  $array = $resultFast !== false ? unserialize($resultFast) : unserialize($resultSlow);
241 
242  //In case no cache entry was found in the FastCache and auto-filling is enabled, copy data to FastCache
243  if ($resultFast === false && $this->_options['auto_fill_fast_cache']) {
244  $preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
245  $this->_fastBackend->save($preparedData, $id, array(), $array['lifetime']);
246  }
247  // maybe, we have to refresh the fast cache ?
248  elseif ($this->_options['auto_refresh_fast_cache']) {
249  if ($array['priority'] == 10) {
250  // no need to refresh the fast cache with priority = 10
251  return $array['data'];
252  }
253  $newFastLifetime = $this->_getFastLifetime($array['lifetime'], $array['priority'], time() - $array['expire']);
254  // we have the time to refresh the fast cache
255  $usage = $this->_getFastFillingPercentage('loading');
256  if (($array['priority'] > 0) && (10 * $array['priority'] >= $usage)) {
257  // we can refresh the fast cache
258  $preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
259  $this->_fastBackend->save($preparedData, $id, array(), $newFastLifetime);
260  }
261  }
262  return $array['data'];
263  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14

◆ remove()

remove (   $id)

Remove a cache record

Parameters
string$idCache id
Returns
boolean True if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 271 of file TwoLevels.php.

272  {
273  $boolFast = $this->_fastBackend->remove($id);
274  $boolSlow = $this->_slowBackend->remove($id);
275  return $boolFast && $boolSlow;
276  }
$id
Definition: fieldset.phtml:14

◆ save()

save (   $data,
  $id,
  $tags = array(),
  $specificLifetime = false,
  $priority = 8 
)

Save some string datas into a cache record

Note : $data is always "string" (serialization is done by the core not by the backend)

Parameters
string$dataDatas to cache
string$idCache id
array$tagsArray of strings, the cache record will be tagged by each string entry
int$specificLifetimeIf != false, set a specific lifetime for this cache record (null => infinite lifetime)
int$priorityinteger between 0 (very low priority) and 10 (maximum priority) used by some particular backends
Returns
boolean true if no problem

Definition at line 196 of file TwoLevels.php.

197  {
198  $usage = $this->_getFastFillingPercentage('saving');
199  $boolFast = true;
200  $lifetime = $this->getLifetime($specificLifetime);
201  $preparedData = $this->_prepareData($data, $lifetime, $priority);
202  if (($priority > 0) && (10 * $priority >= $usage)) {
203  $fastLifetime = $this->_getFastLifetime($lifetime, $priority);
204  $boolFast = $this->_fastBackend->save($preparedData, $id, array(), $fastLifetime);
205  $boolSlow = $this->_slowBackend->save($preparedData, $id, $tags, $lifetime);
206  } else {
207  $boolSlow = $this->_slowBackend->save($preparedData, $id, $tags, $lifetime);
208  if ($boolSlow === true) {
209  $boolFast = $this->_fastBackend->remove($id);
210  if (!$boolFast && !$this->_fastBackend->test($id)) {
211  // some backends return false on remove() even if the key never existed. (and it won't if fast is full)
212  // all we care about is that the key doesn't exist now
213  $boolFast = true;
214  }
215  }
216  }
217 
218  return ($boolFast && $boolSlow);
219  }
$id
Definition: fieldset.phtml:14
getLifetime($specificLifetime)
Definition: Backend.php:143

◆ test()

test (   $id)

Test if a cache is available or not (for the given id)

Parameters
string$idcache id
Returns
mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Implements Zend_Cache_Backend_Interface.

Definition at line 173 of file TwoLevels.php.

174  {
175  $fastTest = $this->_fastBackend->test($id);
176  if ($fastTest) {
177  return $fastTest;
178  } else {
179  return $this->_slowBackend->test($id);
180  }
181  }
$id
Definition: fieldset.phtml:14

◆ touch()

touch (   $id,
  $extraLifetime 
)

Give (if possible) an extra lifetime to the given cache id

Parameters
string$idcache id
int$extraLifetime
Returns
boolean true if ok

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 431 of file TwoLevels.php.

432  {
433  return $this->_slowBackend->touch($id, $extraLifetime);
434  }
$id
Definition: fieldset.phtml:14

Field Documentation

◆ $_fastBackend

$_fastBackend
protected

Definition at line 111 of file TwoLevels.php.

◆ $_fastBackendFillingPercentage

$_fastBackendFillingPercentage = null
protected

Definition at line 118 of file TwoLevels.php.

◆ $_options

$_options
protected
Initial value:
= array(
'slow_backend' => 'File',
'fast_backend' => 'Apc',
'slow_backend_options' => array(),
'fast_backend_options' => array(),
'stats_update_factor' => 10,
'slow_backend_custom_naming' => false,
'fast_backend_custom_naming' => false,
'slow_backend_autoload' => false,
'fast_backend_autoload' => false,
'auto_fill_fast_cache' => true,
'auto_refresh_fast_cache' => true
)

Definition at line 85 of file TwoLevels.php.

◆ $_slowBackend

$_slowBackend
protected

Definition at line 104 of file TwoLevels.php.


The documentation for this class was generated from the following file: