【问题标题】:Magento 2 save and load to custom cacheMagento 2 保存并加载到自定义缓存
【发布时间】:2018-01-14 14:08:40
【问题描述】:

我尝试将值保存到自定义 Magento 2 缓存中。

这是我在 Model/Cache/Type.php 下自定义缓存的代码

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

const TYPE_IDENTIFIER = 'cache_tag';

const CACHE_TAG = 'CACHE_TAG';

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

}

这是 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="xxx_cache" translate="label,description" instance="XXX\XXX\Model\Cache\Type">
    <label>XXX Cache type</label>
    <description>XXX cache description.</description>
</type>

现在我尝试将数据保存并加载到此自定义缓存中:

$this->cache->save(serialize("Data"), md5("Identifier"));

$data = $this->cache->load(md5("Identifier"));

$this->logger->debug("Data: " . print_r(unserialize($data)), true);

这是记录器输出:[2017-08-07 09:37:08] main.DEBUG: Data: {"is_exception":false} []

为什么我没有得到字符串“Data”?

【问题讨论】:

    标签: caching magento2


    【解决方案1】:

    CACHE_TAG 必须与缓存类型定义中使用的name= 匹配。大小写不相关。

    选项 1 - 将 const CACHE_TAG = 'CACHE_TAG'; 更改为 const CACHE_TAG = 'xxx_cache';

    或选项 2 更改

    &lt;type name="xxx_cache" translate="label,description" instance="XXX\XXX\Model\Cache\Type"&gt;

    &lt;type name="cache_tag" translate="label,description" instance="XXX\XXX\Model\Cache\Type"&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      相关资源
      最近更新 更多