6

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

  • Did you try <action method="setCacheLifetime"><time>null</time></action>? – pspahn Jun 30 '14 at 14:59
  • I will try that – Memes Jul 1 '14 at 14:43
  • tried it at no avail, I as well tried <time>0</time>, no luck neither... – Memes Jul 2 '14 at 15:50
3

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).

| improve this answer | |
  • Thanks for your help, It is not a block per se that I am trying not to cache but a template phtml. I do not see how to implement the 3 functions. I also tried the XML solution, that is not giving me any result. – Memes Jul 2 '14 at 16:09
1

The usual way to go here is using for example https://github.com/AOEpeople/Aoe_TemplateHints to identify the cached block/template. Sometimes its not the one Block, but one of the parent Blocks, which cache the containing blocks together.

The linked tool can show you if a block is cached or not and also gives some infos about it like block name, type, template, cache key and similar.

| improve this answer | |
0

This may not be the exact answer you're looking for but it's what I did in a similar situation. I had an issue before requiring this block (and a few more) to be removed from cache when using Varnish. Due to various problems I found it easier to just remove the block from the layout completely and display it via an Ajax call after the page had loaded.

To do this I created a mini module (controller, layout and template file). In the layout I referenced the block and called it in the template. Using ajax then I called the url and appended it to an appropriate div.

Also when logged in it may need to be uncached as there could be a risk of displaying incorrect duplicate data to multiple users.

| improve this answer | |
  • Thanks for the answer, that is indeed not what I am looking for as I want to keep our existing code (running rather well except that). – Memes Jul 1 '14 at 14:40
  • Also, one question, how come the HTML served by AJAX is not cached? is that because you request a different "page" each time? – Memes Jul 1 '14 at 14:40
  • Basically (as far as I understand) the page is cached, not the actual code. So while the html from the ajax load appears on the page, the actual markup is not part of the page. – develophper Jul 1 '14 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.