如何使Symfony2中的容器缓存无效?

问题描述:

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

Part of my Symfony app configuration is being loaded from legacy database, so sometimes I need to invalidate container cache to make use of updated data.

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

Is there any API to invalidate Symfony container cache programmatically?

  1. 根据CacheClearCommand:

$filesystem   = $this->container->get('filesystem');
$realCacheDir = $this->container->getParameter('kernel.cache_dir');
$this->container->get('cache_clearer')->clear($realCacheDir);
$filesystem->remove($realCacheDir);

  • 直接从代码中调用CacheClearCommand:

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