I'm using memcahe
for Symfony Doctrine cache drivers as below:
doctrine:
orm:
metadata_cache_driver: memcache
result_cache_driver: memcache
query_cache_driver: memcache
I have Article
and Tag
entities which are manyToMany
relationship. I have a controller which fetches all articles (with pagination) and renders them to twig. The below code is in Article
repository that uses useQueryCache()
and useResultCache()
:
$articles = $this->createQueryBuilder('a')
->orderBy('a.created_at', 'DESC')
->getQuery()
->useQueryCache(true)
->useResultCache(true)
->getResult();
That DQL is perfectly cached for query and result as I did not see the query executed in Symfony Profiler Doctrine section. The problem is the result of the associated entity Tag
not being cached. I have the following code in twig:
{% for article in articles %}
// ...
{% if article.tags|length %}
<div class="tag-wrapper clearfix">
{% for tag in article.tags %}
// ...
{% endfor %}
</div>
{% endif %}
// ...
{% endfor %}
The call article.tags
seems fetching the tags of the related article all the time without caching. I see all queries are executing on every page load in the Symfony Profiler Doctrine section. Is it possible to cache this?
You're missing a couple of things:
tags
relation and you're not selecting from it in your query Take a look here for some pointers: Doctrine2 (Doctrine 2.1) eager loading in Symfony2 .
On another note, I'd be careful doing this if you anticipate a lot of articles with a lot of tags on that one page. Even with caching, you might run into time and/or memory problems down the road.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.