简体   繁体   English

缓存Doctrine结果Symfony2

[英]Caching Doctrine results Symfony2

I am working on a Symfony2 project. 我正在研究Symfony2项目。 My project uses database to store the data and Doctrine2 to retrieve that data. 我的项目使用数据库来存储数据,使用Doctrine2来检索数据。

As the data within the database has grown the queries became very slow and the whole web app takes about 2 mins to load or does not load at all. 随着数据库中的数据增长,查询变得非常缓慢,整个Web应用程序大约需要2分钟才能加载或根本不加载。

The only way I can see my self fixing this is to cache some queries results but how can I do that. 我能看到自我修复的唯一方法是缓存一些查询结果,但我该怎么做呢。 Unless there is different way of dealing with such issue. 除非有不同的方式来处理这样的问题。

You need to have your cache driver installed and configured in doctrine configuration ( result_cache_driver is important in your case). 您需要在doctrine 配置中安装和配置缓存驱动程序(在您的情况下, result_cache_driver很重要)。 Once you have this done you can make Doctrine to use result cache by setting useResultCache(true) 完成此操作后,您可以通过设置useResultCache(true)使Doctrine使用结果缓存

$cachedResult = $doctrine->getManager()
    ->createQueryBuilder()
    ->(...)
    ->useResultCache(true)
    ->(...)

Check this blog post 查看此博客文章

NOTE : by default, in dev environment, result cache won't be used 注意 :默认情况下,在dev环境中,不会使用结果缓存

EDIT : as you're using DBAL and not using ORM - SymfonyDoctrineBundle doesn't support this kind of cache out of the box, but you can add this support by yourself by following this detailed guide 编辑 :你使用DBAL并没有使用ORM - SymfonyDoctrineBundle不支持这种高速缓存的开箱即用,但你可以自己按照添加该支持详细指南

In developer mode Symfony2 creates a new cache on every query. 在开发人员模式下,Symfony2会在每个查询上创建一个新缓存。 So suppose you have many queries then it will cache all queries one by one. 因此,假设您有许多查询,那么它将逐个缓存所有查询。

This takes more time than in production mode, because in production mode the cache will only be stored once. 这比生产模式花费的时间更多,因为在生产模式下,缓存只会存储一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM