php - Symfony 缓存组件 (3.1) 不保存缓存

标签 php caching symfony-3.1

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

相关文章:

php - 如何通过 Scalingo 的仪表板将 Symfony 的 env 设置为 prod?

PHP随机显示变量

php - 为什么 "104"排在 "0000105"之前?

php - 在数据库表中搜索大量关键字

android - Android WebView 缓存目录中的文件格式是什么(data_1、f_000001 等)?

asp.net - 禁用asp.net缓存

jquery - 在 jQuery 中缓存 $(this) 是最佳实践吗?

json - 将 Symfony 从 2.8 升级到 3.1 : "Your requirements could not be resolved to an installable set of packages."

symfony - assetic.ERROR "DoctrineBundle:Collector:db.html.twig"包含错误 : Unknown "profiler_dump" function

php - 我想创建简单投票而不点击按钮,但它不起作用