1

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 "

1

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

  • Thanks for your answer. I found different solution which let's me clean only that cache that I need. – Walter White Feb 24 '16 at 16:01
1

I will answer my own question because I was found a nice solution.

use Magento\PageCache\Model\Cache\Type as Cache;

class Index extends \Magento\Backend\App\Action
{
    protected $cache;
    public function __construct(
        Cache $cache
    ) {
        $this->cache = $cache;
    }

    private function cleanMageCache(){
        return $this->cache->clean(\Zend_Cache::CLEANING_MODE_ALL, array('FPC'));
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.