Edit in GitHubLog an issue

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 Admin.

The tag scope provides a mechanism for a cache type.

Cache type configuration#

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

AttributeRequired?Description
nameYesA unique cache type ID
translateNoParameters that will be translated on the "Cache Management" page
instanceYesThe cache type model class

Also, cache type configuration have the following required parameters:

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.

For example:

Copied to your clipboard
1<?xml version="1.0"?>
2<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
3 <type name="%cache_type_id%" translate="label,description" instance="VendorName\ModuleName\Model\Cache\Type\CacheType">
4 <label>Cache Type Label</label>
5 <description>Cache Type Description</description>
6 </type>
7</config>

You may declare multiple cache types.

Cache type model#

Copied to your clipboard
1<?php
2/**
3 * Copyright © Magento, Inc. All rights reserved.
4 * See COPYING.txt for license details.
5 */
6
7namespace VendorName\ModuleName\Model\Cache\Type;
8
9use Magento\Framework\App\Cache\Type\FrontendPool;
10use Magento\Framework\Cache\Frontend\Decorator\TagScope;
11
12/**
13 * System / Cache Management / Cache type "Your Cache Type Label"
14 */
15class CacheType extends TagScope
16{
17 /**
18 * Cache type code unique among all cache types
19 */
20 const TYPE_IDENTIFIER = '%cache_type_id%';
21
22 /**
23 * The tag name that limits the cache cleaning scope within a particular tag
24 */
25 const CACHE_TAG = '%CACHE_TYPE_TAG%';
26
27 /**
28 * @param FrontendPool $cacheFrontendPool
29 */
30 public function __construct(FrontendPool $cacheFrontendPool)
31 {
32 parent::__construct(
33 $cacheFrontendPool->get(self::TYPE_IDENTIFIER),
34 self::CACHE_TAG
35 );
36 }
37}

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.

Store data in a custom cache type#

To store serialized data in a custom cache, follow these steps:

  1. Pass the argument to the constructor Magento\Framework\App\CacheInterface $cache of a required class (Repository, Model, Block, etc).

    Copied to your clipboard
    1/**
    2 * @param CacheInterface $cache
    3 * @param SerializerInterface $serializer
    4 */
    5public function __construct(CacheInterface $cache, SerializerInterface $serializer)
    6{
    7 $this->cache = $cache;
    8 $this->serializer = $serializer;
    9}
  2. Store data in the cache.

    Copied to your clipboard
    1$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER;
    2$cacheTag = \VendorName\ModuleName\Model\Cache\Type\CacheType::CACHE_TAG;
    3
    4$storeData = $this->cache->save(
    5 $this->serializer->serialize($cacheData),
    6 $cacheKey,
    7 [$cacheTag],
    8 86400
    9);

Retrieve data from custom cache type#

Retrieve data from the cache with:

Copied to your clipboard
1$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER;
2
3$data = $this->serializer->unserialize($this->cache->load($cacheKey));

Invalidate custom cache type#

To invalidate a custom cache type, follow these steps:

  1. Pass the argument to the constructor Magento\Framework\App\Cache\TypeListInterface $typeList of a required class (Repository, Model, Block, etc).

    Copied to your clipboard
    1/**
    2 * @param TypeListInterface $typeList
    3 */
    4public function __construct(TypeListInterface $typeList)
    5{
    6 $this->typeList = $typeList;
    7}
  2. Invalidate the cache.

    Copied to your clipboard
    1$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER;
    2
    3$this->typeList->invalidate($cacheKey);

Flush custom cache type#

The custom cache type can be flushed in the following ways:

  • Go to System -> Cache Management and flush the custom cache type
  • Programmatically, using the TypeList.
Copied to your clipboard
1$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER;
2
3$this->typeList->cleanType($cacheKey);

Examples#

A cache type translate is declared in the Magento_Translation module using the cache.xml configuration file.

Copied to your clipboard
1<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
2 <type name="translate" translate="label,description" instance="Magento\Framework\App\Cache\Type\Translate">
3 <label>Translations</label>
4 <description>Translation files</description>
5 </type>
6</config>

Translate cache type model class is defined in Magento\Framework\App\Cache\Type\Translate.php class. It must extend the Magento\Framework\Cache\Frontend\Decorator\TagScope class.

Copied to your clipboard
1<?php
2/**
3 * Copyright © Magento, Inc. All rights reserved.
4 * See COPYING.txt for license details.
5 */
6
7namespace Magento\Framework\App\Cache\Type;
8
9use Magento\Framework\Cache\Frontend\Decorator\TagScope;
10
11/**
12 * System / Cache Management / Cache type "Translations"
13 */
14class Translate extends TagScope
15{
16 /**
17 * Cache type code unique among all cache types
18 */
19 const TYPE_IDENTIFIER = 'translate';
20
21 /**
22 * Cache tag used to distinguish the cache type from all other caches
23 */
24 const CACHE_TAG = 'TRANSLATE';
25
26 /**
27 * @param FrontendPool $cacheFrontendPool
28 */
29 public function __construct(FrontendPool $cacheFrontendPool)
30 {
31 parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
32 }
33}
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.