Magento offers several types of caching as per need. But after a modifying code or making changes that might affect HTML output on the store frontend, it’s highly recommended to keep Magento cache enabled. Working with Magento extension development, many times you need to flush cache programmatically to apply drastic changes on the store frontend. Recently, while developing an extension, we have implemented programmatically cache to get quick effects on store data. Today I would likely to share that code with you guys.
All you need to do is simply need to add the following code to your extension file.
public function __construct(TypeListInterface $typeListInterface,Pool $pool)
$this->typeListInterface = $typeListInterface;
$this->pool = $pool;
public function cachePrograme()
$_cacheTypeList = $this->typeListInterface;
$_cacheFrontendPool = $this->pool;
$types = array('full_page');
foreach ($types as $type)
foreach ($_cacheFrontendPool as $cacheFrontend)
Voila, now you successfully implemented programmatically cache. You can manipulate this code according to your requirements of caching.
Do comment if you are facing any issue while using this code.
I’ve tried this code and also using Magento\Framework\App\Cache\Manager method, but cache is not cleared, I’ve written code in Block file. What could be the issue?
Manually flushing the cache works fine.
Confirm your code is called, and put code in try…..catch block, so if any error occurs so you get idea.