我正在使用新的 Symfony 缓存组件。我首先使用命令 (Command/AppCacheGenerateCommand.php) 生成缓存:
$cache = new FilesystemAdapter();
foreach ($domains as $domain){
if ($domain->getHost()){
$output->writeln('Generate cache for domain: ' . $domain->getHost());
$domainCache = $cache->getItem('domain.' . $domain->getHost());
$domainCache->set($domain->getId());
$cache->save($domainCache);
}
}
然后尝试在 onKernelRequest EventListener (EventListener/RequestListener.php) 中获取这些缓存的元素
$cache = new FileSystemAdapter();
$domainCache = $cache->getItem('domain.' . $host);
if (!$domainCache->isHit()){
die;
}
它总是死在这里,不再继续前进。任何人都可以给我一个解释吗? (我尝试过主机不匹配,但确实如此......)
最佳答案
我已经找到答案了:
首先我必须在 config.yml 中添加缓存配置:
framework:
cache:
pools:
my_cache_name:
adapter: cache.adapter.filesystem
default_lifetime: 0
比而不是
$cache = new FilesystemAdapter();
我必须使用新服务,例如:
$cache = $this->getContainer()->get('my_cache_name);
它已经开始工作了!希望对其他人有帮助!
关于php - Symfony 缓存组件 (3.1) 不保存缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743797/