Symfony 2 Cache Request Results

I am working on a Symfony2 project using Doctrine. I want to optimize the performance of the API by adding a cache to the requests.

I examined several options, for example:

  • symfony annotation cache
  • Doctrine Cache
  • Memcache

Not sure which one I should go with, but it seems to me that caching data at the Doctrine level would be most appropriate.

Saying that I want someone to help me or give me guidance on setting up the Doctrine cache and explain how it works.

Ie I have this request:

class QueryFactory protected $connect; public function __construct(Connection $connection) { $this->connect = $connection; } private function myQuery() { return $this->connect->createQueryBuilder() ->select('user_id') ->from('users', 'u') ->where('u.user_id = 2'); } } 

How to add cache to this request? Is there any Doctrine library that I need to add any thing I need to use ?

+5
source share
1 answer

In doctrine for caching queries or results, you can do the following:

 private function myQuery() { return $this->connect->createQueryBuilder() ->select('user_id') ->from('users', 'u') ->where('u.user_id = 2') ->getQuery() ->useQueryCache(true) // here ->useResultCache(true); // and here } 

Check out the document for more information on these methods. This will make your cache driver work - no matter which driver you use.

To configure Symfony to use a specific request driver, you need to configure your settings in config.yml - check this to see the full list of options. What is important in your cache (this is an example apc configuration):

 entity_managers: some_em: query_cache_driver: apc metadata_cache_driver: apc result_cache_driver: apc 

You can also check this and this blog post.

+19
source

Source: https://habr.com/ru/post/1213312/


All Articles