在窗口系统的Magento 2中以编程方式刷新缓存


12

我正在寻找可以通过脚本刷新或刷新Magento2缓存的代码。

Magento 1.x非常简单。

我在WAMP服务器(窗口)上运行Magento2。

Answers:


2

@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转到您的项目位置


就像相同的是magento 1的步骤是什么。–
zus

23

下面的代码以编程方式刷新缓存。对我来说很好。

案例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();
}

如果只需要清理特定产品的缓存stackoverflow.com/a/42636405/3733214
Gediminas

16

对类型进行硬编码是一个主意。相反,您可以使用cache:flushand 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());
}

2

为了增加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/


它什么也没显示
Arunendra '02

1
对于Windows,必须确保已将php.exe添加到环境变量中的PATH中。请参阅willj.co/2012/10/run-wamp-php-windows-7-command-line
tecjam

如果您能够将shell_exec()用于PHP,则您的安装不安全。在现场环境中应禁用此功能。
frustratedtech


1

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();
}

我希望这对您有用。


0

创建一个名为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();
            }
        }
    ?>

-1

这对我有用

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->create('Magento\Framework\App\Cache\Manager');
$cacheManager->flush($cacheManager->getAvailableTypes());
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.