I am trying to disable cache for specific block,
Have used below code.
app\code\Vendor\Module\view\frontend\layout\default.xml
<referenceContainer name="header.panel">
<block class="Vendor\Module\Block\Index" name="custom_block" template="custom.phtml"/>
</referenceContainer>
app\code\Vendor\Module\Block\Index.php
<?php
namespace Vendor\Module\Block;
class Index extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context
array $data = []
) {
parent::__construct($context, $data);
$this->_isScopePrivate = false;
}
public function customHtml()
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
if (curl_exec($ch)) {
$contents = curl_exec($ch);
return $contents;
}
}
public function getCacheLifetime()
{
return null;
}
}
app\code\Vendor\Module\view\frontend/templates/custom.phtml
<?php echo $block->customHtml(); ?>
Used getCacheLifetime and _isScopePrivate but not working. Please someone guide me on this.
Note: cacheable="false" is not recommended
Thanks in advance!!