Magento
has eleven out-of-the-box cache types, according to the following list. These are used across many levels within the system:
There is also Additional Cache Management that manages the cache for the following files:
Each of these caches can be cleared separately.
We can easily define our own cache type. We can do so by first creating an app/code/Foggyline/Office/etc/cache.xml
file with content, as follows:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/ cache.xsd"> <type name="foggyline_office" instance="FoggylineOfficeModelCache"> <label>Foggyline Office Example</label> <description>Example cache from Foggyline Office module.</description> </type> </config>
When defining a new cache type, we need to specify its name
and instance
attributes. The name
attribute of the type
element should be set to foggyline_office
and should be unique across Magento
. This value should match the TYPE_IDENTIFIER
constant value on the FoggylineOfficeModelCache
class, which will be created soon. The instance
attribute holds the class name that we will use for caching.
Then, we will define the FoggylineOfficeModelCache
class in the app/code/Foggyline/Office/Model/Cache.php
file with the following content:
namespace FoggylineOfficeModel; class Cache extends MagentoFrameworkCacheFrontendDecoratorTagScope { const TYPE_IDENTIFIER = 'foggyline_office'; const CACHE_TAG = 'OFFICE'; public function __construct( MagentoFrameworkAppCacheTypeFrontendPool $cacheFrontendPool ) { parent::__construct( $cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG ); } }
The Cache
class extends from TagScope
and specifies its own values for TYPE_IDENTIFIER
and CACHE_TAG
, passing them along to the parent constructor in the __construct
method. With these two files (cache.xml
and Cache
), we have basically defined a new cache type.
Once we have specified the cache.xml
file and the referenced cache
class, we should be able to see our cache type in the Magento
admin under the System | Tools | Cache Management menu, as shown in the following screenshot:
On its own, simply defining a new cache does not mean that it will get filled and used by Magento
.
If you would like to use the cache anywhere within your code, you can do so by first passing the instance of the cache class to the constructor, as follows:
protected $cache; public function __construct( FoggylineOfficeModelCache $cache ) { $this->cache = $cache; }
Then, you can execute a chunk of code, as follows:
$cacheId = 'some-specific-id'; $objInfo = null; $_objInfo = $this->cache->load($cacheId); if ($_objInfo) { $objInfo = unserialize($_objInfo); } else { $objInfo = [ 'var1'=> 'val1', 'var2' => 'val2', 'var3' => 'val3' ]; $this->cache->save(serialize($objInfo), $cacheId); }
The preceding code shows how we first try to load the value from the existing cache entry, and if there is none, we save it. If the cache type is set to disabled
under the Cache Management menu, then the preceding code will never save and pull the data from the cache, as it is not in effect.
If you take a look at the var/cache
folder of Magento
at this point, you will see something similar to what's shown in the following screenshot:
Magento
created two cache entries for us, namely var/cache/mage-tags/mage---a8a_OFFICE
and var/cache/mage--f/mage---a8a_SOME_SPECIFIC_ID
. The mage---a8a_OFFICE
file has only a single line of entry in this specific case, and the entry is the a8a_SOME_SPECIFIC_ID
string, which obviously points to the other file. The mage---a8a_SOME_SPECIFIC_ID
file contains the actual serialized $objInfo
array.
The a8a_
prefix and other prefixes in the cache
file names are not really relevant to us; this is something that Magento
adds on its own. What is relevant to us is the passing of proper individual cache tags to the chunks or variables that we want to cache, like in the preceding example, and the TYPE_IDENTIFIER
and CACHE_TAG
tags that we set for the Cache
class.
13.58.39.23