【问题标题】:How do I automate Redis cache clearing once deployment occurs?部署后如何自动清除 Redis 缓存?
【发布时间】:2016-12-27 19:13:21
【问题描述】:

一旦部署发生,我需要实现一种从 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 文件中?我知道可能有一种非常简单的方法可以做到这一点,但我就是想不通。

【问题讨论】:

    标签: php composer-php symfony


    【解决方案1】:

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

    // 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
    

    希望它有效!

    【讨论】:

    • 是的,它工作得很好,非常感谢你的帮助,我真的很感激!!!
    • 你能接受这个答案吗?它需要点击绿色检查
    猜你喜欢
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2015-04-16
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多