缓存失效
Magento 2实体变化后可以清空缓存,立即查看效果。我们使用 IdentityInterface 将应用程序中的实体与缓存的内容连接起来,并知道当实体改变时要清除哪些缓存。
本节讨论当你改变一个实体时如何告知Magento 2应用清除缓存。
首先,你的实体模块必须实现 Magento/Framework/DataObject/IdentityInterface 如下:
use Magento\Framework\DataObject\IdentityInterface;
class Product implements IdentityInterface
{
/**
* Product cache tag
*/
const CACHE_TAG = 'catalog_product';
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
}
其次,块对象也必须实现 Magento/Framework/DataObject/IdentityInterface 如下:
class View extends AbstractProduct implements \Magento\Framework\DataObject\IdentityInterface
{
/**
* Return identifiers for produced content
*
* @return array
*/
public function getIdentities()
{
return $this->getProduct()->getIdentities();
}
}
私人内容版本
私有内容存储在浏览器本地存储中,使用private_content_version cookie存储版本.
本文介绍Magento2中如何通过实体模块实现缓存的更新与清除。通过实现IdentityInterface接口,可以确保实体发生变化时,相关联的缓存会被及时清理。
1559

被折叠的 条评论
为什么被折叠?



