Come svuotare la cache via codice in Magento 2
Potrebbe capire, durante i nostri sviluppi, di avere la necessità di dover pulire la cache.
Questo è possibile farlo in questa maniera:
protected $_cacheTypeList;
Dopo aver iniettato le classi \Magento\Framework\App\Cache\TypeListInterface e \Magento\Framework\App\Cache\Frontend\Pool possiamo utilizzare i loro metodi all'interno della nostra classe:
protected $_cacheFrontendPool;
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;
}
$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();
}
dove $types sono tutti i tipi di cache disponibili.