How To

Magento 2: How to Create/Add Custom Cache Type in Cache List

Hello Magento Folks,

In this illustration, I will help you to learn How to Create/Add custom cache type in the cache list in Magento 2. What’s your plan for this Black Friday? Are you searching for deals? Checkout Big Magento Black Friday and Cyber Monday Deals 2020 to get great benefits on Magento Extensions and Services. Let’s learn Magento 2: How to Create/Add custom cache type in cache list. 

Why custom cache type is required?

Many times developers require to add a custom cache for obtaining fast output. Mainly, custom cache type is used to specify to the customers that what type of cache is used with the help of the Cache Management option. Let me guide you to Create/Add custom cache type in the cache list. 

Steps to Create/Add custom cache type in cache list:

Step 1: Firstly, Create One Vendor/Extenison/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_name" translate="description,label" instance="Vendor\Extension\Cache\Type">
        <label>Cache Label</label>
        <description> Description About Action or purpose</description>
    </type>
</config>

Step 2: Next, for performing this create type.php file into the Cache Folder in 

Path : Vendor\Extension\Cache\Type.php

<?php

namespace Vendor\Extension\Cache;

use Magento\Framework\App\Cache\StateInterface;
use Magento\Framework\App\Cache\Type\FrontendPool;

class Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{
    const TYPE_IDENTIFIER = 'uniq_identifire';
    const CACHE_TAG = 'uniq_tag';
    private $cacheState;

    public function __construct(
        FrontendPool $cacheFrontendPool,
        StateInterface $cacheState
    ) {
        parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
        $this->cacheState = $cacheState;
    }
    public function load($identifier)
    {
        if (!$this->isEnabled()) {
            return false;
        }
        return parent::load($identifier);
    }

    public function save($data, $identifier, array $tags = [], $lifeTime = null)
    {
        if (!$this->isEnabled()) {
            return false;
        }
        return parent::save($data, $identifier, $tags, $lifeTime);
    }

    public function isEnabled()
    {
        return $this->cacheState->isEnabled(self::TYPE_IDENTIFIER);
    }
}

That’s It.

Output:

After the application of the above steps, you will be able to check the output in the backend of your Magento 2 store. It will be similar to the below screenshot.

Final Words:

Hopefully, all are able to Create/Add custom cache type in the cache list for Magento 2. Incase any casualties in the above solution let me know in the comment section below.

Help your friends to learn this by sharing via social media.

Happy Reading!

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

18 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago