如何使Symfony2中的容器缓存无效? [英] How to invalidate container cache in Symfony2?
本文介绍了如何使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?
推荐答案
-
根据
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());
这篇关于如何使Symfony2中的容器缓存无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文