create cache type

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="menu" translate="label,description" instance="VendorName\TestModule\Model\Cache\Type">
        <label>Test cache type</label>
        <description>Test cache type Description</description>
    </type>
</config>
<?php
namespace VendorName\TestModule\Model\Cache;

class Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{

    /**
     * Cache type code unique among all cache types
     */
    const TYPE_IDENTIFIER = 'menu';

    /**
     * Cache tag used to distinguish the cache type from all other cache
     */
    const CACHE_TAG = ' MENU_TAG';

    /**
     * Type constructor.
     * @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);
    }

}

Let’s enable it and some experiments.

$menuCache = $this->_objectManager->get('VendorName\TestModule\Model\Cache\Type');
$cacheId = 'custom_menu_cache';
$cacheTag = ['menu_tag_1', 'menu_tag_2'];
$data = 'I am custom menu content';
$lifeTime = '600';
$menuCache->save($data, $cacheId, $cacheTag, $lifeTime);

If you look at the var/cache folder of Magento at this point, you will see something similar to the following screenshot.

we find four 44d_CUSTOM_MENU_CACHE(this is the cache id) records in four files(var/cache/mage-tags/mage---44d_MAGE, var/cache/mage-tags/mage---44d_MENU_TAG(the cache tag),var/cache/mage-tags/mage---44d_MENU_TAG_1,var/cache/mage-tags/mage---44d_MENU_TAG_2).

And a file named var/cache/mage--6/mage---44d_CUSTOM_MENU_CACHE.

So, if I want to find all the cache taged with BLOCK_HTML (specified in Magento\Framework\App\Cache\Type\Block), I can search file name with ‘BLOCK_HTML’

This file writes down all the cache id taged with ‘BLOCK_HTML’.

the prefix 44d_ is sepecified in app/etc/env.php

'cache' => [
        'frontend' => [
            'default' => [
                'id_prefix' => '44d_'
            ],
            'page_cache' => [
                'id_prefix' => '44d_'
            ]
        ]
    ],
$menuCache = $this->_objectManager->get('VendorName\TestModule\Model\Cache\Type');
$cacheId = 'custom_menu_cache';
$cacheTag = ['menu_tag_1', 'menu_tag_2'];
$data = 'I am custom menu content';
$lifeTime = '600';
$menuCache->save($data, $cacheId, $cacheTag, $lifeTime);
$content = $menuCache->load($cacheId);
var_dump($content);

We can get the string ‘I am custom menu content’;

$menuCache = $this->_objectManager->get('VendorName\TestModule\Model\Cache\Type');
$cacheId = 'custom_menu_cache';
$cacheTag = ['menu_tag_1', 'menu_tag_2'];
$data = 'I am custom menu content';
$lifeTime = '600';
$menuCache->save($data, $cacheId, $cacheTag, $lifeTime);


var_dump($menuCache->getTag());
echo get_class($menuCache->getLowLevelFrontend());

// we will get all the cache ids in system
var_dump($menuCache->getLowLevelFrontend()->getIds());
// we will get all the cache tags in system
var_dump($menuCache->getLowLevelFrontend()->getTags());

Practices Tests

How do you create a new cache type?
A. Use a plugin to append to the method arguments for the \Magento\Framework\App\CacheList::collect method.
B. Create etc/cache.xml and specify a <type name="cache_name" instance="Class\Path"/>.
C. Inject a new caching class into \Magento\Framework\App\CacheList.
D. Add an event observer for the cache_type_collector observer.

Answer B

发表评论

电子邮件地址不会被公开。 必填项已用*标注