Symfony2 Caching Doctrine Results

I am working on a symfony2 project. My project uses a database to store data and Doctrine2 to retrieve this data.

As the data in the database grew, queries became very slow, and the entire web application took about 2 minutes to load or not load at all.

The only way I can make sure that I fix it myself is to cache some requests, but how can I do this. If there is no other way to solve this problem.

+8
php symfony doctrine2
source share
2 answers

You need your cache driver to be installed and configured in the configuration doctrine ( result_cache_driver important in your case). After that, you can do Doctrine to use the result cache by setting useResultCache(true)

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

Mark this blog post

NOTE : by default in the dev environment, the result cache will not be used.

EDIT : when using DBAL and not using ORM - SymfonyDoctrineBundle does not support this kind of cache code, but you can add this support yourself after this detailed guide

+16
source share

In developer mode, Symfony2 creates a new cache for each request. Therefore, suppose you have many requests and it will cache all requests one by one.

This takes longer than in production mode, because in production mode the cache will be stored only once.

0
source share

All Articles