2016-12-27 44 views
2

我需要实现一种方法来在部署发生后清除composer.json文件中的redis缓存。 SNC的Redis的bundle命令是这样的:一旦部署发生,我该如何自动化Redis缓存清除?

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文件的源代码的根文件?我知道这可能是一个非常简单的方法,但我无法弄清楚。

回答

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注册它:

$ composer run-script post-install-cmd 

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

您可以使用下面的命令试试吧

希望它的作品!

+1

是的,它的工作完全非常感谢你的帮助我真的很感激它! – user6405037

+0

你能接受答案吗?它需要点击绿色支票 – chalasr

+0

对不起,我再次点击它,再次感谢! – user6405037