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

Public Member Functions

 __construct (\Magento\Framework\Cache\FrontendInterface $cache, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, $cacheIdPrefix='', array $cacheTags=[], $cacheLifetime=null, SerializerInterface $serializer=null)
 
 fetchAll (Select $select, array $bindParams=[])
 

Protected Member Functions

 _getSelectCacheId ($select)
 

Protected Attributes

 $_cacheIdPrefix = ''
 
 $_cacheTags = []
 
 $_cacheLifetime = null
 

Detailed Description

Retrieve collection data from cache, fail over to another fetch strategy, if cache does not exist yet

Definition at line 15 of file Cache.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Cache\FrontendInterface  $cache,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface  $fetchStrategy,
  $cacheIdPrefix = '',
array  $cacheTags = [],
  $cacheLifetime = null,
SerializerInterface  $serializer = null 
)

Constructor

Parameters
\Magento\Framework\Cache\FrontendInterface$cache
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface$fetchStrategy
string$cacheIdPrefix
array$cacheTags
int | bool | null$cacheLifetime
SerializerInterface | null$serializer

Definition at line 57 of file Cache.php.

64  {
65  $this->_cache = $cache;
66  $this->_fetchStrategy = $fetchStrategy;
67  $this->_cacheIdPrefix = $cacheIdPrefix;
68  $this->_cacheTags = $cacheTags;
69  $this->_cacheLifetime = $cacheLifetime;
70  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
71  }

Member Function Documentation

◆ _getSelectCacheId()

_getSelectCacheId (   $select)
protected

Determine cache identifier based on select query

Parameters
\Magento\Framework\DB\Select | string$select
Returns
string

Definition at line 100 of file Cache.php.

101  {
102  return $this->_cacheIdPrefix . md5((string)$select);
103  }

◆ fetchAll()

fetchAll ( Select  $select,
array  $bindParams = [] 
)

{Retrieve all records

Parameters
Select$select
array$bindParams
Returns
array
}

Implements FetchStrategyInterface.

Definition at line 76 of file Cache.php.

77  {
78  $cacheId = $this->_getSelectCacheId($select);
79  $result = $this->_cache->load($cacheId);
80  if ($result) {
81  $result = $this->serializer->unserialize($result);
82  } else {
83  $result = $this->_fetchStrategy->fetchAll($select, $bindParams);
84  $this->_cache->save(
85  $this->serializer->serialize($result),
86  $cacheId,
89  );
90  }
91  return $result;
92  }

Field Documentation

◆ $_cacheIdPrefix

$_cacheIdPrefix = ''
protected

Definition at line 30 of file Cache.php.

◆ $_cacheLifetime

$_cacheLifetime = null
protected

Definition at line 40 of file Cache.php.

◆ $_cacheTags

$_cacheTags = []
protected

Definition at line 35 of file Cache.php.


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