Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to prevent Magento from Caching a Block

Tags:

magento

I'm working on a Magento 1.6 site, which has the following xml inside the home page's CMS "Layout Update XML" field:

<reference name="content">
   <block type="catalog/navigation" name="catalog.category.home" as="homecategory" template="catalog/category/homecategory.phtml" />
</reference>

As the template shows randomized categories, I would like to disable caching for this block. To do so, I attempted using getChildHtml('sub-block-template', false) with the following:

(homecategory has $this->getChildHtml('random_categories', false) in its template)

<reference name="content">
    <block type="catalog/navigation" name="catalog.category.home" as="homecategory" useCache="false" template="catalog/category/homecategory.phtml">
        <block type="catalog/navigation" name="catalog.category.home.randcats" as="random_categories"  useCache="false" template="catalog/category/random.phtml" />
    </block>
</reference>

So now I'm stuck, wondering why I can't prevent caching of that block, despite using the 'false' argument.

like image 569
Excalibur Avatar asked Feb 05 '12 05:02

Excalibur


2 Answers

I had this same problem. I beleive it has to do something with the block type of type="catalog/navigation". I've seen this disabling of caching work on other types of blocks. Here is a fix for this block type and this problem:

phtml file change: make sure the second param is false

echo $this->getChildHtml('topCategoriesList',false);

xml file change: Add these actions to the block

<block type="catalog/navigation" name="topCategoriesList" as="topCategoriesList"    template="catalog/navigation/categorylist.phtml">
   <action method="unsetData"><key>cache_lifetime</key></action>
   <action method="unsetData"><key>cache_tags</key></action>
</block>
like image 80
uffa Avatar answered Oct 05 '22 21:10

uffa


Have you tried forcing it by creating a new custom block type and overloading the caching functions? Extend the Mage_Catalog_Block_Product_List_Random class and create an empty pseudo-constructor:

protected function _construct() {}

This will prevent inheriting adding cache tags, lifetime, and other metadata to the block object. Then you can overload the cache key info as well so that it doesn't use any existing (or enabled) cache blocks. For example:

public function getCacheKeyInfo()
{
    return array(
        'MY_CACHE_TAG',
        Mage::app()->getStore()->getId(),
        (int)Mage::app()->getStore()->isCurrentlySecure(),
        Mage::getDesign()->getPackageName(),
        Mage::getDesign()->getTheme('template')
    );
}
like image 20
Jona Avatar answered Oct 05 '22 22:10

Jona