Milind Singh
Software Engineer
Monthly Magento Meetup - 21st Dec 2019
A cache is a collection of duplicate data, where the original data is expensive to fetch or compute (usually in terms of access time) relative to the cache.
Caching is used to minimize page generation time. PHP basically has two main types of caching: 'output caching' and 'parser caching'.
//app/code/VendorName/ModuleName/Model/Cache/Type.php
class VendorName\ModuleName\Model\Cache\Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{
const TYPE_IDENTIFIER = '%cache_type_id%';
const CACHE_TAG = '%CACHE_TYPE_TAG%';
public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
{
parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
}
}
//app/code/VendorName/ModuleName/etc/cache.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="cache_type_id" translate="label,description" instance="VendorName\ModuleNale\Model\Cache\Type">
<label>Custom Cache type</label>
<description>Custom cache description.</description>
</type>
</config>
//app/code/VendorName/ModuleName/etc/di.xml
<type name="VendorName\ModuleName\Model\Cache">
<arguments>
<argument name="cache" xsi:type="object">VendorName\ModuleName\Model\Cache\Type</argument>
</arguments>
</type>
<?php
//app/code/VendorName/ModuleName/Model/Cache.php
namespace VendorName\ModuleName\Model;
use Magento\Framework\Cache\FrontendInterface;
class Cache
{
/**
* Cache key identifier
*/
const CACHE_KEY_IDENTIFIER = 'my_key_'; // Prefix for Unique Key in Cache
/**
* @var FrontendInterface
*/
public $cache;
/**
* Cache constructor.
* @param FrontendInterface $cache
*/
public function __construct(
FrontendInterface $cache
) {
$this->cache = $cache;
}
/**
* Get values
* @param $key
* @return mixed|null
*/
public function getValue($key)
{
$cacheKey = self::CACHE_KEY_IDENTIFIER . $key;
$value = $this->cache->load($cacheKey);
return $value === false ? null : json_decode($value, true);
}
/**
* Set Values
* @param $key
* @param $value
* @param array $tags
* @param null $lifeTime
* @throws \Exception
*/
public function setValue($key, $value, array $tags = [], $lifeTime = null)
{
if ($value === null) {
$value = [];
}
if ($lifeTime === null || (int)$lifeTime <= 0) {
$lifeTime = 60 * 60 * 24 * 365 * 5;
}
$cacheKey = self::CACHE_KEY_IDENTIFIER . $key;
$preparedTags = [self::CACHE_KEY_IDENTIFIER . '_main'];
foreach ($tags as $tag) {
$preparedTags[] = self::CACHE_KEY_IDENTIFIER . '_' . $tag;
}
$this->cache->save(json_encode($value), $cacheKey, $preparedTags, (int)$lifeTime);
}
public function removeValue($key)
{
$cacheKey = self::CACHE_KEY_IDENTIFIER . $key;
$this->cache->remove($cacheKey);
}
public function removeAllValues()
{
$this->removeTagValues('main');
}
public function removeTagValues($tag)
{
$tags = [self::CACHE_KEY_IDENTIFIER . '_' . $tag];
$this->cache->clean(\Zend_Cache::CLEANING_MODE_ALL, $tags);
}
}
<?php
//app/code/VendorName/ModuleName/Model/MyModel.php
namespace VendorName\ModuleName\Model;
use VendorName\ModuleName\Cache;
class MyModel
{
/**
* @var Cache
*/
public $cache;
/**
* Cache constructor.
* @param Cache $cache
*/
public function __construct(
Cache $cache
) {
$this->cache = $cache;
}
public funtion get($id)
{
return $this->cache->getValue($id);
}
}
+
By Milind Singh
Magento Meetup Lucknow - 21st Dec 2019