Please see this excellent article:
http://fbrnc.net/blog/2015/06/cache-and-layout-xml-tricks
For completeness:
create a helper class:
class My_Module_Helper_Data extends Mage_Core_Helper_Abstract {
public function returnNull()
{
return null;
}
}
and set the value in your layout:
<reference name="footer">
<action method="setCacheLifetime"><lifetime helper="mymodule/returnNull" /></action>
</reference>
an alternative, but not so elegant a solution is to use your own derived block class.
class My_Module_Block_NoCache extends Mage_Core_Block_Template {
protected function _construct()
{
$this->addData(array('cache_lifetime' => null));
}
}
and then derive your block from this, not core/template
{{block type="my_module/nocache" name="supplier.delivery" template="myphp/delivery.phtml"}}
you can define your own cache tags in the construct:
$this->addCacheTag(array(
Mage_Core_Model_Store::CACHE_TAG,
"MY_OWN_CACHE_TAG"
));
Note that there can be a potential that the parent block is also cached, which would then in turn cache your block there. Subsequent page requests would not even reach your block, as the parent(s) cached output would simply be used.
Best Answer
You can create a preference or a plugin on the block class to override the block cache functions:
getCacheLifetime(), hasCacheLifetime(), get/hasCacheTags(), has/getCacheKeyInfo()
, getData().If
getCacheLifetime
returnsnull
than it will disable current block's cache.Take a look at the block class to see which cache methods it uses and search in parent block classes (Abstract, Template, ..) to see which function you need to override in your plugin/preference class.
cacheable = "false" has only one meaning. It will disable full page cache type on that page type / action.
If there is something wrong with the parameters that form cache key info, than you can vary block cache by overriding getCacheKeyInfo function.
If there is customer specific data inside that block than you can't use either block cache or have it in full page cache. The only viable solution is to use AJAX. Magento 2 has private content: http://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/private-content.html
Other solution is to use AJAX requests to fill parts of pages like in product page reviews and other. https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Review/view/frontend/web/js/process-reviews.js
The last and the least is the XML layout declaration ttl="3600". When Magento 2 is configured to work with Varnish, it will create am ESI policy for the output of the block. Something similar does when it uses builtin page cache instead of Varnish. Magento uses this type of cache hole punching in one place: in top menu. I personally don't recommend using "ttl" cache hole punching because it adds an extra request to the server. If you use it too many times on a page than it may decrease performance.