25
protected function _construct()
    {
        $this->addData(
            [
                'cache_lifetime' => false,
                'cache_tags' => array('MY_BLOCK'),
            ]
        );
    }

    public function getCacheKeyInfo()
    {
        return [];
    }

does not work. Why? How to disable the cache for block?

  • I suspect your block is inside another block that is also cached. For example, anything you put inside the footer block is cached because the footer block output is cached. – Smartie Feb 24 '16 at 12:24
  • @Smartie how to turn it off me? <referenceContainer name="content"> <block class="class" template="block.phtml" before="product.info.main"/> </referenceContainer> – gebuket Feb 24 '16 at 12:38
29

Blocks can be set as non-cacheable by setting the cacheable attribute false in layout XML files. For example

<block class="Block\Class" name="blockname" cacheable="false" />

Pages containing such blocks are not cached.

Also check How do disable caching of custom block

Edit: A single cacheable="false" will disable Full Page Caching for the whole page, making the pages sourcing from that layout file extremely slow! Check https://inviqa.com/blog/how-full-page-cache-works-magento-2

15

It's important to understand why you don't want to cache block. If this is meant to show some session specific information then you should be looking into

One non-recommended option could be also custom controller what returns some data over ajax-call (with POST-method so it wouldn't be cached).

(!) cacheable="false" must not be used. Here will follow, WHY not:

A block with cacheable="false" will make the whole page uncached. It is not used for cache hole-punching. Also following page says this (To create an uncacheable page, mark any block on that page as uncacheable in the layout using cacheable="false"):

What it does is that Varnish/Fastly modules will be sending due this attribute value always non-cacheable headers.

When we enable cachable="false" and while using Varnish/Fastly then browser side following headers are sent :

X-Magento-Cache-Debug:MISS
X-Magento-Cache-Control:max-age=0, must-revalidate, no-cache, no-store
Age: 0

For that can be debugged the Magento's page caching code on

vendor/magento/module-page-cache/Model/Layout/LayoutPlugin.php::afterGenerateXml
vendor/magento/module-page-cache/Model/Layout/LayoutPlugin.php::afterGetOutput

where first one should send public Cache-Control with TTL and second should send X-Magento-Tags for Varnish/Fastly.

Both are using isCacheable() check where this always returns FALSE due following check (check if in current layout there are any attributes:cacheable="false"):

$cacheableXml = !(bool)count($this->getXml()->xpath('//' . Element::TYPE_BLOCK . '[@cacheable="false"]'));

When we remove cacheable="false" then we start to get isCacheable() checks as TRUE and also get headers correctly on start-/category-/productpages.

X-Magento-Cache-Control:max-age=86400, public, s-maxage=86400
X-Magento-Cache-Debug:HIT
X-Magento-Cache-Hits:1
Age:32
  • So... what is the alternative solution? – Black Feb 13 at 19:02
  • alternative must be taken case by case - what you need. if it's customer related then you need to define additional item-argument for Magento\Customer\CustomerData\SectionPoolInterface in etc/frontend/di.xml (ex. devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/… ). if it should be block with dynamic data depending from product, then maybe it should be javascript component or widget. It really depends all on the case. – Elvin Risti Mar 5 at 6:42
9

I have made non-cacheable for Pricing block

<?php

namespace Custom\Module\Block\Pricing\Render;

class ExtendFinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox {

    public function getCacheLifetime()
    {
        return null;
    }

}
  • How did you replace FinalPriceBox with ExtendPriceBox using di.xml? – siddhesh Jul 14 '17 at 14:54
  • Yes using di.xml – Lead Developer Jul 18 '17 at 16:39
  • @LeadDeveloper, he wants to know what you wrote into your di.xml – Black Feb 13 at 19:02
0

You can add non-cacheable block only to custom pages. In other case, magento Page Cache will not work for these pages OR update information in block via ajax (magento section requests).

Some things described here https://mirasvit.com/blog/magento-2-page-cache-coverage-rate.html

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.