2

我正在使用新的 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;
    }

它总是死在这里,不会更进一步。谁能给我一个解释?(我试过如果主机不匹配,但它确实......)

4

1 回答 1

3

我想出了答案:

首先我必须在 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);

它开始工作了!希望对其他人有所帮助!

于 2016-11-22T14:49:08.937 回答