This is a beta release of documentation for Magento 2.4, published for previewing soon-to-be-released functionality. Content in this version is subject to change. Links to the v2.4 code base may not properly resolve until the code is officially released.

Create a cache type

A cache type enables you to specify what is cached and enables merchants to clear that cache type using the Cache Management page in the Magento Admin.

The tag scope provides a mechanism for a cache type.

Cache type configuration

Declare a new cache type in the etc/cache.xml file with the following attributes:

Attribute Required? Description
name Yes A unique cache type ID
translate No Parameters that will be translated on the “Cache Management” page
instance Yes The cache type model class

Also, cache type configuration have the following required parameters:

Parameter Description
label The “Cache Type” field to be displayed on the System > Tools > Cache Management page.
description The “Description” field to be displayed on the System > Tools > Cache Management page.

For example:

1
2
3
4
5
6
7
<?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\ModuleName\Model\Cache\Type\CacheType">
        <label>Cache Type Label</label>
        <description>Cache Type Description</description>
    </type>
</config>

You may declare multiple cache types.

Cache type model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
namespace VendorName\ModuleName\Model\Cache\Type;

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

/**
 * System / Cache Management / Cache type "Your Cache Type Label"
 */
class CacheType extends TagScope
{
    /**
     * Cache type code unique among all cache types
     */
    const TYPE_IDENTIFIER = '%cache_type_id%';

    /**
     * The tag name that limits the cache cleaning scope within a particular tag
     */
    const CACHE_TAG = '%CACHE_TYPE_TAG%';

    /**
     * @param FrontendPool $cacheFrontendPool
     */
    public function __construct(FrontendPool $cacheFrontendPool)
    {
        parent::__construct(
            $cacheFrontendPool->get(self::TYPE_IDENTIFIER),
            self::CACHE_TAG
        );
    }
}

You must specify the following parameters:

  • VendorName\ModuleName defines the name of a module that uses a cache type. A module can use several cache types and a cache type can be used in several modules.
  • %cache_type_id% defines the unique identifier of a cache type.
  • %CACHE_TYPE_TAG% defines the unique tag to be used in the cache type scoping.