Mitech Preloader
Magento2

How to flush cache programmatically in magento2?

magento2-flushcache

How to flush cache programmatically in magento2?  The answer is here, You just need to add this cache flush code to your magento site by following just some easy steps,

Define constructor – pass Magento\Framework\App\Cache\TypeListInterface and Magento\Framework\App\Cache\Frontend\Pool to your file’s constructor as defined below :

[php]

public function __construct(
Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
parent::__construct($context);
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
}

[/php]

Another thing is, add following code to the method where you want flush cache

[php]

$types = array(‘config’,’layout’,’block_html’,’collections’,’reflection’,’db_ddl’,’eav’,’config_integration’,’config_integration_api’,’full_page’,’translate’,’config_webservice’);
foreach ($types as $type) {
$this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}

[/php]

So using this method you can flush cache in magento2 .

I Hope this blog will help you.

blank