home

Search A2Z 24

Programmatically refresh or clean or flush the cache in Magento 2

Using magento 2 Cache Manager class to pull all the cache types and clean and flush the cache.

<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Cache\Manager as CacheManager;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\App\Cache\TypeListInterface;
class Cache extends Template implements IdentityInterface
{
    /**
     * Cache types list
     * @var TypeListInterface
     */
    private $cacheTypeList;
    
    /**
     * @var CacheManager
     */
    protected $_cacheManager;

    /**
     * @param Template\Context $context
     * @param CacheManager $cacheManager
     * @param TypeListInterface $cacheTypeList
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        CacheManager $cacheManager,
        TypeListInterface $cacheTypeList,
        array $data = []
    ) {       
        parent::__construct($context, $data);
        $this->_cacheManager = $cacheManager;
        $this->cacheTypeList = $cacheTypeList;
    }
    /**
     * @return string
     */     
    public function toHtml() {
        $response = $this->whereYouNeedToCleanCache();
        return $response;
    }

    /**
     * @return string
     */
    private function whereYouNeedToCleanCache()   {   
        $response = null;
        $inc  = 1;
        if($getStat = count($this->_cacheManager->getStatus())) {
            $response .= "<strong>".__("Available Cache's")." ($getStat)</strong><br/><sub>1 = Enable & 0 = Disable</sub><br/><br/>";
            
            foreach($this->_cacheManager->getStatus() as $st_key => $st_val) {
                $response .= $inc++."). ".$st_key." =        ".$st_val."<br/>";
            }
        }
        
        $getInv = count($this->cacheTypeList->getInvalidated());
        $response .= "<br/><strong>".__("Invalid Cache's")." ($getInv)</strong><br/>";
        
        if($getInv) {            
            $in_inc  = 1;
            foreach($this->cacheTypeList->getInvalidated() as $inv) {
                foreach($inv as $in) {
                     $response .= $in_inc++."). ".$in['id'].' = '.$in['description']."<br/>";
                }
            }  
            
            $this->_cacheManager->flush($this->_cacheManager->getAvailableTypes());
            $this->_cacheManager->clean($this->_cacheManager->getAvailableTypes());
            
            $response .= "<br/><hr><br/><strong>".__("($getInv) - Invalid cache(s) flush & clean Done")."</strong><br/>";
        } else {
            $response .= "<br/><hr><br/><strong>".__("Nothing to flush or clean")."</strong><br/>";
        }        
        return $response;
    }  

    /**
     * Get key pieces for caching block content | Get cache key informative items
     * @return array
     */
    public function getCacheKeyInfo() {
        return [
            'Vendor_Module_Cache',
            $this->_storeManager->getStore()->getId(),
            $this->_design->getDesignTheme()->getId(),
            'base_url' => $this->getBaseUrl(),
            'template' => $this->getTemplate(),
            date('Y-m-d :: H:i:s')
        ];
    }
    
    /**
     * Return identifiers for produced content
     * @return array
     */
    public function getIdentities()   {
        return ['vendor_custom_cache'];
    }
}

?>

About Author

loading...

Share your thoughts!

Login as a member to access comment posting block !! click-here

Thoughts From Other Users (0)

No Comments

Reveal Modal Goodness

This is a default modal in all its glory, but any of the styles here can easily be changed in the CSS.

This is just a simple modal with the default styles, but any type of content can live in here. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi quis sem vel enim eleifend tristique. Etiam tincidunt faucibus pharetra.

×