Magento – Disable cache on a block

block-cachecacheheader

Out of despair, I ask question here.

I have a phtml (top_links.phtml) file that is cached when I am not logged in but not cached when I am logged in. I need it to be never cached as I display the geolocation there.

I tried

<?php echo $this->getChildHtml('topLinks',false); ?>

in the header.phtml

I tried

<block type="page/html" name="top.links2" as="topLinks" template="page/html/top_links.phtml"> 
            <action method="unsetData"><key>cache_lifetime</key></action>
            <action method="unsetData"><key>cache_tags</key></action>
        </block>

in my layout.xml

I tried to do as recommended there.

none worked. Then I realized that when logged in, it is not cached…

Can somebody give me some pointers?

Thanks already

Best Answer

Start by identifying the block class instance by

var_dump(get_class_name($this))

Within the phtml file.

Then in the block (either by extending it if core, or editing it if local/community), define the cache lifetime.

There's three main (magic getter/setter) important functions used when Magento evaluates if a block is cacheable.

  • getCacheLifetime()
  • getCacheKeyInfo
  • getCacheTags

I say functions, but these are just the return methods for the $_data variables, so you could define the data in the block constructor.

Similarly, you can do this in your XML for a cleaner approach.

<action method="setCacheLifetime" />

I'll be honest and say I've never tried to set a null lifetime for a block in XML so I'm not fully sure if it will work, but it certainly works in the block class return method/constructor.

Be sure to set the lifetime to null - using 0 will still create a cache entry with 0 lifetime (thus filling your cache with thousands of surplus, unused cache entries).

Related Topic