How to create a Custom Cache type in Magento2
Discover the step-by-step process to build a custom Magento 2 cache type, including all important configuration and implementation details
Here is a brief summary of the steps involved:
Creating a new module named “CustomCache”, the module directory is app/code/Webkul/CustomCache.
Now create a new directory called “etc” inside your module directory and create “cache.xml” file at app/code/Webkul/CustomCache/etc/ with the below code.
<?xml version="1.0"?>
<!--
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_CustomCache
* @author Webkul Software Private Limited
* @copyright Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="webkulcustomcache" translate="label,description" instance="Webkul\CustomCache\Model\Cache\Type">
<label>Webkul Custom Cache</label>
<description>Webkul Custom Cache Description.</description>
</type>
</config>
In the cache.xml file, define your custom cache type.
Now, create “Type.php” in the directory Webkul/CustomCache/Model/Cache/ with the below code.
<?php
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_CustomCache
* @author Webkul Software Private Limited
* @copyright Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
namespace Webkul\CustomCache\Model\Cache;
class Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{
/**
* Type Code for Cache. It should be unique
*/
const TYPE_IDENTIFIER = 'webkulcustomcache';
/**
* Tag of Cache
*/
const CACHE_TAG = 'WEBKULCUSTOMCACHE';
/**
* @param \Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool
*/
public function __construct(
\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool
){
parent::__construct(
$cacheFrontendPool->get(self::TYPE_IDENTIFIER),
self::CACHE_TAG
);
}
}
After registering the module by upgrade command “php bin/magento setup:upgrade“,
after that when you go to setting System ==> Cache Management in the admin panel, there you will see your newly created cache type a new entry
Make sure to select and enable the newly added Cache from the mass actions dropdown and click Submit so that the cache becomes active.
Use your new cache type in your Magento 2 store. You can do this by storing the data in the custom cache type and also retrieving it later from a custom cache type.
To store serialized data in a custom cache, follow these steps:
Pass the argument to the constructor Magento\Framework\Cache\FrontendInterface $cache of a required class (Repository, Model, Block, etc).
<?php
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_CustomCache
* @author Webkul Software Private Limited
* @copyright Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
namespace Webkul\CustomCache\Model;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Serialize\SerializerInterface;
class OperateCustomCache
{
/**
* @param CacheInterface $cache
* @param SerializerInterface $serializer
*/
public function __construct(
CacheInterface $cache,
SerializerInterface $serializer
){
$this->cache = $cache;
$this->serializer = $serializer;
}
public function saveCache() {
$cacheKey = \Webkul\CustomCache\Model\Cache\Type::TYPE_IDENTIFIER;
$cacheTag = \Webkul\CustomCache\Model\Cache\Type::CACHE_TAG;
$time = date('Y-m-d H:i:s');
$cacheData = [
'name' => 'Webkul',
'type' => 'Custom Cache',
'description' => 'This is custom cache created by Webkul',
'created_at' => $time
];
$storeData = $this->cache->save(
$this->serializer->serialize($cacheData),
$cacheKey,
[$cacheTag],
86400
);
}
public function getCache() {
$cacheKey = \Webkul\CustomCache\Model\Cache\Type::TYPE_IDENTIFIER;
$data = $this->serializer->unserialize(
$this->cache->load($cacheKey)
);
}
}
Create di.xml to Inject Custom Cache Type
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Webkul\CustomCache\Model\OperateCustomCache">
<arguments>
<argument name="cache" xsi:type="object">Webkul\CustomCache\Model\Cache\Type</argument>
</arguments>
</type>
</config>
You have to take care of passing classes to the constructor of the class that is responsible for saving and getting cached data. As shown below.
/**
* @param FrontendInterface $cache
* @param SerializerInterface $serializer
*/
public function __construct(FrontendInterface $cache, SerializerInterface $serializer)
{
$this->cache = $cache;
$this->serializer = $serializer;
}
The code section of the class “Webkul\CustomCache\Model\OperateCustomCache” is responsible for storing data in the cache.
$cacheKey = \Webkul\CustomCache\Model\Cache\Type::TYPE_IDENTIFIER;
$cacheTag = \Webkul\CustomCache\Model\Cache\Type::CACHE_TAG;
$time = date('Y-m-d H:i:s');
$cacheData = [
'name' => 'Webkul',
'type' => 'Custom Cache',
'description' => 'This is custom cache created by Webkul',
'created_at' => $time
];
$storeData = $this->cache->save(
$this->serializer->serialize($cacheData),
$cacheKey,
[$cacheTag],
86400
);
The code section of class “Webkul\CustomCache\Model\OperateCustomCache” is responsible for retrieving data from the cache type
$cacheKey = \Webkul\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER; $data = $this->serializer->unserialize($this->cache->load($cacheKey));
Overall, creating a custom cache type in Magento 2 can help improve the performance and scalability of your store.
By caching frequently accessed data, you can reduce the load on your server and improve the user experience for your customers.
If you’re looking for expert help implementing this architecture, trust a seasoned Magento development company to bring your vision to life.
Know more about Magento 2 architecture.
Continue Reading -> Magento 2 Commands List