33

I have developed a top bar cart functionality for store front that displays latest three products added to the cart this works great when cache is disabled but when i enable the cache it showing weird subtotal in top cart and weird products in cart.

This make me sad :'(

I know this is happening because of cache. Is there anything that I can disable the cache for this section only? I have tried disabling the block html cache and check now this was working great but I found the category flat is having unspecified issues when block HTML is disabled.

7ochem
  • 7,532
  • 14
  • 51
  • 80
Ravi Soni
  • 1,515
  • 6
  • 20
  • 40

3 Answers3

34

You can try

    <reference name="needed block">
        <action method="setCacheLifetime"><s>null</s></action>
    </reference>

or

    <reference name="needed block">
        <action method="setCacheLifetime" />
    </reference>

to set it to null.

But probably it is better to use ajax.

There is a good article about null or 0 meaning http://blog.kyp.fr/make-a-clean-sweep-of-commons-magento-cache_lifetime-workarounds-usage-in-cache-block-policy/ (link is broken)

7ochem
  • 7,532
  • 14
  • 51
  • 80
Dmytro Zavalkin
  • 1,535
  • 9
  • 13
  • hmm use of ajax could solve the problem. I will try this thanks for your help. – Ravi Soni Apr 20 '13 at 05:49
  • 2
    I thought null = never cache and 0 = cache permanently? – benmarks Apr 20 '13 at 11:02
  • 1
    null = never cache, right. However, I'm not sure it is possible to argument value to null with <action> directive. – Dmytro Zavalkin Apr 21 '13 at 11:02
  • 2
    <action method="setCacheLifetime"><s>null</s></action> worked for me. TNX! – ruuter Apr 26 '13 at 21:21
  • 6
    Setting the Cache Lifetime to "0" does not disable the cache. Only "null" will disable it! – Anna Völkl Nov 14 '13 at 14:10
  • I tried with editing this answer to a correction, but it was not possible, so I can only downvote it for "you can try" - answer or not. But you can try sounds harsh for a Q&A site. Especially as others have commented that the answer is wrong in part - and it would have been easy to correct it. – hakre Nov 10 '15 at 09:43
  • So the correct method is? null and none of the other solutions? – snh_nl Nov 14 '17 at 12:45
11

You can disable cache in getChildHtml(). This is what the signature looks like:

public function getChildHtml($name = '', $useCache = true, $sorted = false)

This should do the trick:

echo $this->getChildHtml('block', false);
Rick Kuipers
  • 4,260
  • 2
  • 20
  • 24
  • i have tried this.. actually is have coded in top.phtml this is not working in that case – Ravi Soni Apr 19 '13 at 14:49
  • That might be cached as well so I guess it wouldn't have much effect. Is there a chance you can pull it outside of that part? I don't think there are any other options... – Rick Kuipers Apr 19 '13 at 14:53
9

In the particular xml file. Say catalog.xml

In that for particular reference block In that block set the action of cache limit to null

Example of catalog.xml

<reference name="right">
    <block type="catalog/navigation" name="catalog.product.cat.related" as="related_cat" template="catalog/navigation/list_cat.phtml" after="-" >
        <action method="unsetData"><key>cache_lifetime</key></action>
        <action method="unsetData"><key>cache_tags</key></action>
    </block>
</reference>
Vinay Shah
  • 91
  • 1
  • 1