Magento – Clear Magento 2 theme cache from module

cachemagento2PHP

Is it possible to clear cache directly from module?

After some actions in my module and I'm saving data into database. Basing on the values from the database I am displaying some informations (or not if they don't exists) in frontend. The problem is that, that those informations are caching in frontend and in order to see any changes I need to refresh cache. It's pretty bad for users of my app because they will be forced to clear cache manually.

So I'm wondering is there any possibility to force magento to refresh cache directly from module in order to see changes in frontend?

I need to refresh "Page Cache : Full page caching "

Best Answer

You could programmatically clear cache within your module using

/**
 * @var \Magento\Framework\App\Cache\Frontend\Pool
 */
protected $_cacheFrontendPool;


public function __construct(
    ....
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
) {
    ...
    $this->_cacheFrontendPool = $cacheFrontendPool;
}

Then

    /** @var $cacheFrontend \Magento\Framework\Cache\FrontendInterface */
    foreach ($this->_cacheFrontendPool as $cacheFrontend) {
        $cacheFrontend->clean();
    }

See magento2/app/code/Magento/Backend/Controller/Adminhtml/Cache/FlushSystem.php

Related Topic