Disable full page caching for specific block

2019-04-12 10:34发布

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:

Include unsetData to layout file

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

Set function _saveCache to return false

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

Set different values for 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?

3条回答
甜甜的少女心
2楼-- · 2019-04-12 11:19

cache_lifetime must be set to null to disable cache for this block

public function __construct()
 {
    $this->addData(array(
    ‘cache_lifetime’ => null,       
   ));
 }
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-12 11:24

Here is the solution for disabling FPC for a specific controller (could be extended to specific action as well).

First create an Observer to listen on the controller_action_predispatch event:

    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');
        }
    }

Then add the following to your config.xml file for the module. This goes in the section:

    <events>
        <controller_action_predispatch>
            <observers>
                <YOUR_UNIQUE_IDENTIFIER>
                    <class>YOURMODULE/observer</class>
                    <method>processPreDispatch</method>
                </YOUR_UNIQUE_IDENTIFIER>
            </observers>
        </controller_action_predispatch>
    </events>

Now Magento will serve up your page every time and bypass FPC for the request.

And You can also refer: http://mikebywaters.wordpress.com/2011/12/14/disable-magento-full-page-cache-on-a-per-controller-basis/

查看更多
Summer. ? 凉城
4楼-- · 2019-04-12 11:38

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

My 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>

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.

Code for 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;}
}

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.

查看更多
登录 后发表回答