Disable full page caching for specific block
Solution 1
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.
Solution 2
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/
nevermourn
Updated on June 05, 2022Comments
-
nevermourn about 2 years
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?
-
Mufaddal about 11 yearsmay be if you use this, cache is invalidated every time request is call
$cache->banUse('full_page');
-
nevermourn about 11 yearsThanks for this solution but I need only one block to be rendered each time, and I need it for any controller.
-
Anil Gupta about 11 yearsdisable FPC for a specific controller only not for all and you can use below code also Mage::app() ->getCacheInstance() ->banUse(Mage_Core_Block_Abstract::CACHE_GROUP);
-
Tyler V. about 10 yearsThis works to disable the cache for this container block, but I'm having issues with children blocks of any no-cache container block that I create in this way. The issue is that after the cache for the container block expires, I'm no longer able to get any content for children blocks through $this->getChildHtml or even $this->getBlockHtml. Any suggestions?
-
Adarsh Khatri almost 9 yearsSame problem on reload. running EE