I have a module which includes block to each page. The block template prints javascript which displays notification to user.

First I defined observer in module config file:


Then model

class Company_Mymodule_Model_Observer
    public function addBlockAtEndOfMainContent(Varien_Event_Observer $observer)
        $layout = $observer->getEvent()->getLayout()->getUpdate();
        return $this;

And layout file

      <reference name="content">
          <block type="mymodule/mymodule" name="myblockname" after="-" template="mymodule/notification.phtml"/>

When user closes notification, cookie is stored. Inside mymodule.php I check with php if cookie exists. If exists, the notification with that cookie is not showed again.

$collection->addFieldToFilter('cookie_id',array('nin' => $deniedIds))


This module is installed on many stores and some of them have cache modules, such as Varnish cache and others. Some of cache modules cache my module block and therefore notification is showed every time, no matter if user closed it. It doesn't come to addFieldToFilter because the entire block was already cached.

Does anybody have any idea how I can avoid this? Is any better way to include the block to each page and not being cached?

Thank you!

2 Answers 2


I realized that Varnish caches entire html, so there is no way to partially exclude block to be cached. My solution in this case is to make ajax request with POST parameter. POST request is not cached.


You can set a cache key for your block, which then allows you to cache multiple instances of this block. Depending on what the triggers are if the block should be shown or what it should show you should add these variables to the cache key.

i.e. Mage_Checkout_Block_Cart_Sidebar::getCacheKeyInfo has infos about the items in the cart in the cache key:

public function getCacheKeyInfo()
    $cacheKeyInfo = parent::getCacheKeyInfo();
    $cacheKeyInfo['item_renders'] = $this->_serializeRenders();
    return $cacheKeyInfo;

Regards, Tobias.

  • Wouldn't just <action method="setCacheLifetime"><s>null</s></action> inside layout also do trick? I just found this, but not sure if it is safe for sure.
    – JohnyFree
    Commented May 14, 2014 at 19:04
  • Then that block would not be cached overall. If possible I would prefer having different cache variations than non caching for it.
    – Tobias
    Commented May 15, 2014 at 8:20
  • 2
    <s>null</s> will not work. To pass a null argument use <s/>
    – benmarks
    Commented Jun 15, 2014 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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