部署发生后,如何自动执行Redis缓存清除?

时间:2016-12-27 19:13:21

标签: php composer-php symfony

我需要实现一种方法,在部署发生后从composer.json文件中清除redis缓存。 snc redis bundle命令是这样的:

namespace Snc\RedisBundle\Command;
/**
* Symfony command to execute redis flushall
*
*/
class RedisFlushallCommand extends RedisBaseCommand
{
/**
 * {@inheritDoc}
 */
protected function configure()
{
    parent::configure();
    $this->setName('redis:flushall')
        ->setDescription('Flushes the redis database using the redis flushall command');
}
/**
 * {@inheritDoc}
 */
protected function executeRedisCommand()
{
    if ($this->proceedingAllowed()) {
        $this->flushAll();
    } else {
        $this->output->writeln('<error>Flushing cancelled</error>');
    }
}
/**
 * Flushing all redis databases
 */
private function flushAll()
{
    $this->redisClient->flushall();
    $this->output->writeln('<info>All redis databases flushed</info>');
}
}

如何将其包含在源代码根文件的composer.json文件中?我知道这可能是一种非常简单的方法,但我无法弄明白。

1 个答案:

答案 0 :(得分:2)

您可以创建自己的作曲家脚本处理程序:

// src/AppBundle/ScriptHandler.php
namespace AppBundle;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;

class ScriptHandler
{
    public static function clearRedisCache()
    {    
        (new Application(new \AppKernel('dev', true)))
            ->get('redis:flushall')
            ->run(new ArrayInput(['--client' => 'YOURCLIENT']), new ConsoleOutput());
    }
}

然后在你的composer.json中注册它:

"scripts": {
    "post-install-cmd": [
        "AppBundle\\ScriptHandler::clearRedisCache"
        // ...
     ]
}

您可以使用以下命令尝试:

$ composer run-script post-install-cmd

希望它有效!