禁用特定块的全页缓存 [英] Disable full page caching for specific block

查看:62
本文介绍了禁用特定块的全页缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有完整页面缓存功能的magento EE.有一个可以动态更新的块,但是我似乎无法禁用其缓存. 我要理想地实现的目标:仅对特定块禁用缓存,以便每次页面加载时都将再次呈现它. 我尝试过的事情:

I'm working with magento EE that has full page caching feature. There is a block that is updated dynamically, but I can't seem to disable its caching. What I want to achieve ideally: disable caching only for particular block so it would be rendered again each time the page loads. Things I tried:

将unsetData包含到布局文件中

Include unsetData to layout file

<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>

将函数_saveCache设置为返回false

Set function _saveCache to return false

protected function _saveCache($data, $id, $tags = array(), $lifetime = null) {
    return false;
}

cache_lifetime

public function __construct()
{
    $this->addData(array(
    ‘cache_lifetime’ => 0,
    ‘cache_tags’ => array(Mage_Catalog_Model_Product::CACHE_TAG),

    ));
}

也许我在全页缓存机制中缺少某些东西?

Perhaps I'm missing something in full page caching mechanics?

推荐答案

好吧,我发现了几个不错的帖子,并用etc/cache.xml实现了我的缓存,该缓存将我的代码块与容器对象包装在一起.

Well, I found a couple of good posts and implement my caching with etc/cache.xml, that wraps my block with container object.

我的cache.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>

在这里,我将不应缓存的块用作block,在布局中将块用作name块的名称,并在container中选择了我的容器.

I used here as block the block that should not be cached, as name name of block in my layout, and as container I've choose my container.

容器代码:

<?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;}
}

这里我放了microtime()函数来识别块,但是在我的模块内部,我使用了与我的模块相关的cookie变量.我相信,在什么都没有真正更改的情况下,可以节省块的冗余重载.

Here I putmicrotime() function to identify block, but inside my module I used cookie variables related to my module. I believe that saves redundant reloading of a block when nothing was really changed.

在其他教程中没有找到的是我必须创建布局变量并将其分配给我的块,否则我只会得到我的块而不是整个页面.

The thing that I didn't found in other tutorials is that I had to create layout variable and assign it to my block, otherwise I was getting only my block instead of whole page.

这篇关于禁用特定块的全页缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆