removing a template from the cache
-
13-12-2019 - |
Question
I'm getting a template via:
<?php echo $this->getChildHtml('ajaxCart'); ?>
and the block is declared like this:
<block type="page/html_topmenu" name="ajaxCart" as="ajaxCart" template="page/html/ajaxCart.phtml"/>
How can I remove this from our caching system?
Solution
If you are using the full page cache of Magento Enterprise, you need to do two things:
- disable block caching for the block
- exclude the block from otherwise cached pages with full page cache hole punching
1. Disable block caching
Change the block declaration to:
<block type="page/html_topmenu" name="ajaxCart" as="ajaxCart" template="page/html/ajaxCart.phtml">
<action method="setCacheLifetime"></action>
</block>
This calls setCacheLifetime(null)
and thus disables caching for the block.
More info about setCachelifetime()
and how to use it in layout XML can be found here: http://fbrnc.net/blog/2015/06/cache-and-layout-xml-tricks
2. Hole punching
create a module with a etc/cache.xml
file like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<placeholders>
<ajax_cart_cache>
<block>page/html_topmeu</block>
<name>ajaxCart</name>
<placeholder>AJAX_CART</placeholder>
<container>Your_Module_Model_Container_Ajaxcart</container>
<cache_lifetime></cache_lifetime>
</ajax_cart_cache>
</placeholders>
</config>
and the container model like this:
class Your_Module_Model_Container_Ajaxcart extends Enterprise_PageCache_Model_Container_Abstract
{
protected function _renderBlock()
{
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
$block = new $blockClass;
$block->setTemplate($template);
return $block->toHtml();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange