Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable full page caching for specific block

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?

like image 252
nevermourn Avatar asked Dec 05 '25 14:12

nevermourn


1 Answers

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.

like image 167
nevermourn Avatar answered Dec 08 '25 07:12

nevermourn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!