在开发,开发人员或来自商家的请求的情况下,他/她需要以编程方式清除/刷新缓存。所以今天我们将向您展示如何以编程方式清除缓存。在Helper中实现这些代码行:
<?php
use Magento\Framework\App\PageCache\Version;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Cache\Frontend\Pool;
protected $cacheTypeList;
protected $cacheFrontendPool;
public function __construct(
TypeListInterface $cacheTypeList,
Pool $cacheFrontendPool
){
$this->cacheTypeList = $cacheTypeList;
$this->cacheFrontendPool = $cacheFrontendPool;
}
public function flushCache(Version $subject)
{
$_types = [
'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();
}
}
现在flushCache()
在控制器或模型中调用函数。