在Symfony2中如何使容器缓存失效?

4

我的Symfony应用程序配置的一部分是从遗留数据库加载的,因此有时需要使容器缓存失效以利用更新的数据。

是否有任何API可以以编程方式使Symfony容器缓存失效?

1个回答

10
  1. As per CacheClearCommand:

    $filesystem   = $this->container->get('filesystem');
    $realCacheDir = $this->container->getParameter('kernel.cache_dir');
    $this->container->get('cache_clearer')->clear($realCacheDir);
    $filesystem->remove($realCacheDir);
    
  2. Directly call CacheClearCommand from code:

    services.yml

    clear_cache_command_service:
        class: Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand
        calls:
           - [setContainer, ["@service_container"] ]
    

    Than it's possible to do something like this (note that this will warmup the cache):

    $clearCacheCommand = $this->container->get('clear_cache_command_service');
    $clearCacheCommand->run(new ArgvInput(), new ConsoleOutput());
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接