By default, all pages in Magento are cacheable, but you can disable caching if necessary (e.g., payment method return page, debug page, or AJAX data source).
Disable or enable caching
Add a cacheable="false" attribute to any block in your layout to disable caching:
<block class="Magento\Paypal\Block\Payflow\Link\Iframe" template="payflowlink/redirect.phtml" cacheable="false"/>
Magento disables page caching if at least one non-cacheable block is present in the layout.
Using cacheable="false" inside the default.xml file disables caching for all pages on the site.
You can also disable caching with HTTP headers. Use a controller to return an object that contains methods for manipulating the cache.
Define caching behavior
You can use the Admin to define caching policies or you can define them programmatically in a controller:
class DynamicController extends \Magento\Framework\App\Action\Action
{
protected $pageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->pageFactory = $resultPageFactory;
}
/**
* This action render random number for each request
*/
public function execute()
{
$page = $this->pageFactory->create();
//We are using HTTP headers to control various page caches (varnish, fastly, built-in php cache)
$page->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0', true);
return $page;
}
}
You can implement the HTTP class and call the method setNoCacheHeaders, as you can see below.
use Magento\Framework\App\Response\Http;
public function myCustomMethod(){
$this->response->setNoCacheHeaders();
...
}
Source Click Here