Answers:
@denish,例如,使用cmd可以清除缓存。但是你在PHP命令行上的问题
为了在窗口中作为命令运行php client,您 需要将php设置为可用的环境。 如何为PHP设置env变量?
之后,您可以像这样从cmd运行任何magento 2 cli命令
php bin/magento cache:clean
php bin/magento cache:flush
Or
php bin/magento c:c
php bin/magento c:f
从cmd转到您的项目位置
下面的代码以编程方式刷新缓存。对我来说很好。
案例1:Magento外
use Magento\Framework\App\Bootstrap;
include('../app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
try{
$_cacheTypeList = $objectManager->create('Magento\Framework\App\Cache\TypeListInterface');
$_cacheFrontendPool = $objectManager->create('Magento\Framework\App\Cache\Frontend\Pool');
$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) {
$_cacheTypeList->cleanType($type);
}
foreach ($_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
}catch(Exception $e){
echo $msg = 'Error : '.$e->getMessage();die();
}
案例2:Magento内部
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();
}
对类型进行硬编码是一个坏主意。相反,您可以使用cache:flush
and cache:clean
命令使用的相同方法。缓存管理器类还可以为您提取所有缓存类型,如以下示例所示。
public function __construct(
\Magento\Framework\App\Cache\Manager $cacheManager
) {
$this->cacheManager = $cacheManager;
}
private function whereYouNeedToCleanCache()
{
$this->cacheManager->flush($this->cacheManager->getAvailableTypes());
// or this
$this->cacheManager->clean($this->cacheManager->getAvailableTypes());
}
为了增加denish的答案,您可以编写一些php脚本并将其放入您的magento根文件夹中:
<?php
$command = 'php bin/magento cache:clean && php bin/magento cache:flush';
echo '<pre>' . shell_exec($command) . '</pre>';
?>
这将为您提供如下输出:
Cleaned cache types:
config
layout
block_html
collections
reflection
db_ddl
eav
config_integration
config_integration_api
full_page
translate
config_webservice
Flushed cache types:
config
layout
block_html
collections
reflection
db_ddl
eav
config_integration
config_integration_api
full_page
translate
config_webservice
请确保您实际上可以从命令行执行php,否则这将毫无用处。对于Windows,必须确保已将php.exe添加到环境变量中的PATH中。请参阅http://willj.co/2012/10/run-wamp-php-windows-7-command-line/
您可以使用以下命令刷新或刷新所有缓存
php bin/magento cache:clean
php bin/magento cache:flush
我希望这能帮到您。
1.定义构造函数–通过
Magento \ Framework \ App \ Cache \ TypeListInterface
和
Magento \ Framework \ App \ Cache \ Frontend \ Pool
到您的文件的构造函数,如下所示:
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;
}
2.现在,将以下代码添加到要清除/刷新缓存的方法中:
$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();
}
我希望这对您有用。
创建一个名为cacheflush.php的文件,然后上传您的Magento根文件夹,例如httdocs文件夹的public_html。然后yoursite.com/cacheflush.php它将完美运行。如果您的主机中没有CLI mod没问题...请使用此代码..它将减少您的时间。
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$k[0]='bin/magento';
$k[1]='cache:flush'; // write your proper command like setup:upgrade,cache:enable etc...
$_SERVER['argv']=$k;
try {
$handler = new \Magento\Framework\App\ErrorHandler();
set_error_handler([$handler, 'handler']);
$application = new Magento\Framework\Console\Cli('Magento CLI');
$application->run();
} catch (\Exception $e) {
while ($e) {
echo $e->getMessage();
echo $e->getTraceAsString();
echo "\n\n";
$e = $e->getPrevious();
}
}
?>
这对我有用
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->create('Magento\Framework\App\Cache\Manager');
$cacheManager->flush($cacheManager->getAvailableTypes());