Reading list Switch to dark mode

    How to create a custom cache type in Magento

    Updated 19 March 2024

    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 custom cache type a new entry

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    customCacheType

    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\App\CacheInterface $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\App\CacheInterface;
    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;
            
            $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)
            );
        }
    }

    you, have to take care of passing classes to the constructor of the class that is responsible for saving and getting custom cached data. As shown below.

     /**
      * @param CacheInterface $cache
      * @param SerializerInterface $serializer
      */
     public function __construct(CacheInterface $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;
    
     $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 custom 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.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home