2016-11-22 117 views
2

我正在使用新的Symfony缓存组件。我第一次产生我的缓存的命令(指令/ AppCacheGenerateCommand.php):Symfony缓存组件(3.1)不保存缓存

$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(事件监听/ RequestListener.php)这些缓存的元素

$cache = new FileSystemAdapter(); 
    $domainCache = $cache->getItem('domain.' . $host); 
    if (!$domainCache->isHit()){ 
     die; 
    } 

它总是在这里死亡,而不是进一步。任何人都可以给我一个解释? (我试过,如果主机不匹配,但它确实...)

+0

你必须在解决方案中添加一个答案(然后接受它),而不是在你的问题中更新。查看帮助中心! –

回答

2

我已经想通了答案:

首先,我不得不添加缓存配置在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); 

它开始工作!希望它对别人有帮助!

+1

你必须在这里添加答案,而不是在你的问题内更新。看看[帮助中心](http://stackoverflow.com/help)部分,或者在StackOverflow上的一个被接受的答案的上千个问题来获得它。 –

+0

请注意@ gp_sflover的建议。使用答案来回答问题并提出问题。就这么简单。我现在已经为你做了,但下次要节省时间。干杯 –