【问题标题】:How to invalidate container cache in Symfony2?如何使 Symfony2 中的容器缓存失效?
【发布时间】:2013-11-28 11:43:30
【问题描述】:

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

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

【问题讨论】:

    标签: php symfony


    【解决方案1】:
    1. 根据CacheClearCommand

      $filesystem   = $this->container->get('filesystem');
      $realCacheDir = $this->container->getParameter('kernel.cache_dir');
      $this->container->get('cache_clearer')->clear($realCacheDir);
      $filesystem->remove($realCacheDir);
      
    2. 直接从代码中调用CacheClearCommand:

      services.yml

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

      可以做这样的事情(注意这会预热缓存):

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

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 2020-09-25
      • 2012-02-26
      • 1970-01-01
      • 2011-01-13
      • 2017-09-08
      • 2011-01-11
      • 1970-01-01
      • 2018-01-03
      相关资源
      最近更新 更多