Magento 禁用特定块的完整页缓存
我正在使用MagentoEE,它具有完整的页面缓存功能。有一个块是动态更新的,但我似乎无法禁用它的缓存。 理想情况下,我想要实现的是:仅对特定块禁用缓存,以便在每次加载页面时再次呈现该块。 我尝试过的事情: 将未设置的数据包括到布局文件中Magento 禁用特定块的完整页缓存,magento,caching,Magento,Caching,我正在使用MagentoEE,它具有完整的页面缓存功能。有一个块是动态更新的,但我似乎无法禁用它的缓存。 理想情况下,我想要实现的是:仅对特定块禁用缓存,以便在每次加载页面时再次呈现该块。 我尝试过的事情: 将未设置的数据包括到布局文件中 <action method="unsetData"><key>cache_lifetime</key></action> <action method="unsetData"><key>
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
为缓存\u生存期设置不同的值
public function __construct()
{
$this->addData(array(
‘cache_lifetime’ => 0,
‘cache_tags’ => array(Mage_Catalog_Model_Product::CACHE_TAG),
));
}
也许我在整页缓存机制中遗漏了什么 以下是针对特定控制器禁用FPC的解决方案(也可以扩展到特定操作)
首先创建一个观察者来监听控制器\u动作\u预触发事件:
public function processPreDispatch(Varien_Event_Observer $observer)
{
$action = $observer->getEvent()->getControllerAction();
// Check to see if $action is a Product controller
if ($action instanceof Mage_Catalog_ProductController)
{
$request = $action->getRequest();
$cache = Mage::app()->getCacheInstance();
// Tell Magento to 'ban' the use of FPC for this request
$cache->banUse('full_page');
}
}
然后将以下内容添加到模块的config.xml文件中。这一部分是:
<events>
<controller_action_predispatch>
<observers>
<YOUR_UNIQUE_IDENTIFIER>
<class>YOURMODULE/observer</class>
<method>processPreDispatch</method>
</YOUR_UNIQUE_IDENTIFIER>
</observers>
</controller_action_predispatch>
</events>
你的模块/观察员
过程预分离
现在Magento每次都会为您的页面提供服务,并绕过FPC请求
您还可以参考:好吧,我找到了几篇好文章,并用etc/cache.xml
实现了缓存,它用容器对象包装了我的块
Mycache.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<placeholders>
<namespace_block_unique_node>
<block>module/block_class</block>
<name>name_of_block_in_my_layout</name>
<template>path/to/my/template</template>
<placeholder>UNIQUE_PLACEHOLDER_HERE</placeholder>
<container>Namespace_Module_Model_Caching_Container_BlockName</container>
<cache_lifetime>86400</cache_lifetime>
</namespace_block_unique_node>
</placeholders>
</config>
在这里,我使用microtime()
函数来标识块,但在我的模块中,我使用了与模块相关的cookie变量。我相信这样可以避免在没有任何更改的情况下重复加载块
我在其他教程中没有发现的一点是,我必须创建布局变量并将其分配给我的块,否则我只能得到我的块而不是整个页面。cache\u life必须设置为null以禁用此块的缓存
public function __construct()
{
$this->addData(array(
‘cache_lifetime’ => null,
));
}
你要找的术语是打孔
;对于varnish FPC,您还需要通过javascript Ajax对动态内容进行打孔。你使用什么缓存方法?@Luceos老实说,我还不知道,我想这是Magento EE附带的方法。这里值得一提的是,当cache_lifetime设置为0时,Magento会令人惊讶地将块缓存7200秒,如果你使用这个,每次调用请求时,缓存都无效$cache->banUse('full_page')代码>感谢此解决方案,但我每次只需要渲染一个块,任何控制器都需要。仅对特定控制器禁用FPC,而不是所有控制器,您还可以使用以下代码Mage::app()->getCacheInstance()->banUse(Mage\u Core\u block\u Abstract::CACHE\u GROUP);这可以禁用此容器块的缓存,但我用这种方法创建的任何无缓存容器块的子块都有问题。问题是,在容器块的缓存过期后,我不再能够通过$this->getChildHtml甚至$this->getBlockHtml获取子块的任何内容。有什么建议吗?重新加载时也有同样的问题。运行EE
<?php
class Namespace_Module_Model_Caching_Container_BlockName extends Enterprise_PageCache_Model_Container_Abstract
{
protected function _getCacheId()
{
return 'NAMESPACE_MODULE_BLOCKNAME' . $this->_getIdentifier();
}
protected function _getIdentifier()
{
return microtime();
}
protected function _renderBlock()
{
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
$block = new $blockClass;
$block->setTemplate($template);
$layout = Mage::app()->getLayout();
$block->setLayout($layout);
return $block->toHtml();
}
protected function _saveCache($data, $id, $tags = array(), $lifetime = null) { return false;}
}
public function __construct()
{
$this->addData(array(
‘cache_lifetime’ => null,
));
}