Magento 2: How to Create Custom Cache?

How to create a custom cache in Magento 2

Let’s see How we can create a custom cache in Magento 2. We are aware of the caching mechanism of Magento 2. We can see a list of the cache in the Cache Management Grid at admin.

For example, Configuration, Layout, Block HTML output etc.

We also can create our own custom type in Magento 2. For example, If you are creating a News module and want to save the data in your custom cache. You may create your own cache type in Magento 2.

To create a custom cache, We will require two files. The First one is cache.xml which use as Cache type configuration and the second is the class used in cache.xml as an instance argument.

Cache type configuration

AttributeIs Require?DESCRIPTION
nameYesA unique cache type ID
translateNoParameters that will be translated on the “Cache Management” page
instanceYesThe cache type model class

This configuration requires two sub parameters as given below.

ParameterDescription
labelThe “Cache Type” field to be displayed on the System > Tools > Cache Management page.
descriptionThe “Description” field to be displayed on the System > Tools > Cache Management page.

How to create a custom cache in Magento 2?

It’s time to see practically how we can create a custom cache type in Magento 2.

I have created a module with the name SbDevBlog_Cache. I assume that you have crated the module with suitable name.

Let’s create the file cache.xml inside the etc directory of my module.

<?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="sbdevblog_cache" translate="label,description" instance="SbDevBlog\Cache\Model\Cache\Type\SbDevBlogCache">
        <label>SbDevBlog Cache</label>
        <description>Ceated Custom Cache For SbDevBlog Tutorial</description>
    </type>
</config>

Now, Let’s create SbDevBlogCache.php class inside the mentioned path as an argument with name instance in the file cache.xml

<?php

namespace SbDevBlog\Cache\Model\Cache\Type;

use Magento\Framework\App\Cache\Type\FrontendPool;
use Magento\Framework\Cache\Frontend\Decorator\TagScope;

class SbDevBlogCache extends TagScope
{
    /**
    * Cache type code unique among all cache types
    */
    const TYPE_IDENTIFIER = "sbdevblog_cache";

    /**
     * The tag name that limits the cache cleaning scope within a particular tag
     */
    const CACHE_TAG = "sbdevblog_cache_tag";

    /**
     * Constructor to initialize cache.
     *
     * @param FrontendPool $frontend
     * @param string $tag
     */
    public function __construct(FrontendPool $frontend)
    {
        parent::__construct($frontend->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
    }
}

Magento 2: How to create a custom cache type?

That’s it. It’s very simple. Let’s check how we can store the data to the custom cache type and retrieve data from the custom cache type and the next blog. https://sbdevblog.com/magento-2-store-retrieve-from-the-custom-cache/

Check out the post to invalidate and flush the cache https://sbdevblog.com/magento-2-how-to-invalidate-and-flush-custom-cache-type/

Stay tuned and Please use the comment box for your suggestion. Your suggestions are most welcome.

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

One thought on “Magento 2: How to Create Custom Cache?

Leave a Reply

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