Doctrine and Symphony: magic methods and caching

I am currently using a doctrine result cache, so I installed

result_cache_driver: apc 

into my configuration.

Then I got the request cache working inside the repository correctly using, for example,

 ->setResultCacheId(sprintf('posts_for_user_%d', $userId)) 

The first problem arises when I used these things in the doctrine:

 $repository->findOneBy(array) $repository->findBy(array) 

which can be easily overridden in the repository.

The problem that I cannot get around is using ParamConverter to use doctrine caching, as well as to combine objects.

For example, if I have a Team object with a OneToMany relation to Player, I usually do

 $team->getPlayers() 

I have no control over the caching of this request. Is this possible in some way?

+4
source share
1 answer

When you run methods like find / findBy or process PersistentCollection by doing $ team-> getPlayers (), there is UoW that loads data using EntityPersister and ObjectHydrator to hydrate the object. These objects do not support the result cache driver.

On the other hand, when you use DQL or QueryBuilder, your code object invokes a Query object that extends AbstractQuery. If you look inside AbstractQuery :: execute, you will see this pretty snippet of code that will make it possible to use the result cache driver.

 $cache = $queryCacheProfile->getResultCacheDriver(); $result = $cache->fetch($cacheKey); if (isset($result[$realCacheKey])) { return $result[$realCacheKey]; } 

So my suggestion is - try loading your objects with QueryBuilder and leftJoins in child collections.

 $this->createQueryBuilder('x')->select('x, p')->leftJoin('x.players', 'p')->....; 

This will create the ability to use the result cache driver.

+2
source

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


All Articles