magento2缓存-创建一种缓存类型
《Magento2.X企业级开发实战》
magento2缓存-创建一种缓存类型
缓存类型使您能够指定缓存的内容,并使商家能够使用管理中的缓存管理页面清除该缓存类型。
标签范围为缓存类型提供了一种机制。
缓存类型配置
在 /etc/cache.xml 文件中声明一个具有以下属性的新缓存类型:
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 |
此外,缓存类型配置具有以下必需参数:
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:
<?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>
您可以声明多种缓存类型。
缓存类型模型
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ 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 ); } }
您必须指定以下参数:
VendorName\ModuleName 定义使用缓存类型的模块的名称。 一个模块可以使用多个缓存类型,一个缓存类型可以在多个模块中使用。
%cache_type_id% 定义缓存类型的唯一标识符。
%CACHE_TYPE_TAG% 定义要在缓存类型范围内使用的唯一标记。
以自定义缓存类型存储数据
要将序列化数据存储在自定义缓存中,请执行以下步骤:
将参数传递给所需类(存储库、模型、块等)的构造函数 Magento\Framework\App\CacheInterface $cache。
/** * @param CacheInterface $cache * @param SerializerInterface $serializer */ public function __construct(CacheInterface $cache, SerializerInterface $serializer) { $this->cache = $cache; $this->serializer = $serializer; }
将数据存储在缓存中。
$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER; $cacheTag = \VendorName\ModuleName\Model\Cache\Type\CacheType::CACHE_TAG; $storeData = $this->cache->save( $this->serializer->serialize($cacheData), $cacheKey, [$cacheTag], 86400 );
从自定义缓存类型检索数据
使用以下命令从缓存中检索数据:
$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER;
$data = $this->serializer->unserialize($this->cache->load($cacheKey));
使自定义缓存类型无效
要使自定义缓存类型无效,请执行以下步骤:
将参数传递给所需类(存储库、模型、块等)的构造函数 Magento\Framework\App\Cache\TypeListInterface $typeList
/** * @param TypeListInterface $typeList */ public function __construct(TypeListInterface $typeList) { $this->typeList = $typeList; }
使缓存无效
$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER; $this->typeList->invalidate($cacheKey);
刷新自定义缓存类型
自定义缓存类型可以通过以下方式刷新:
转到 System -> Cache Management 并刷新自定义缓存类型
以编程方式,使用 TypeList。
$cacheKey = \VendorName\ModuleName\Model\Cache\Type\CacheType::TYPE_IDENTIFIER; $this->typeList->cleanType($cacheKey);
使用 cache.xml 配置文件在 Magento_Translation 模块中声明缓存类型转换。
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd"> <type name="translate" translate="label,description" instance="Magento\Framework\App\Cache\Type\Translate"> <label>Translations</label> <description>Translation files</description> </type> </config>
翻译缓存类型模型类定义在 Magento\Framework\App\Cache\Type\Translate.php 类中。 它必须扩展 Magento\Framework\Cache\Frontend\Decorator\TagScope 类。
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\App\Cache\Type; use Magento\Framework\Cache\Frontend\Decorator\TagScope; /** * System / Cache Management / Cache type "Translations" */ class Translate extends TagScope { /** * Cache type code unique among all cache types */ const TYPE_IDENTIFIER = 'translate'; /** * Cache tag used to distinguish the cache type from all other caches */ const CACHE_TAG = 'TRANSLATE'; /** * @param FrontendPool $cacheFrontendPool */ public function __construct(FrontendPool $cacheFrontendPool) { parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG); } }
magento2缓存-创建一种缓存类型
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。