F
F
ff0xff2019-01-18 15:06:40
symfony
ff0xff, 2019-01-18 15:06:40

How to cache sql query execution data in symfony 4 + doctrine?

Good day, I’m interested in the following question:
The database has table A and table B
The data in table A is updated very often, their selection is not resource-intensive and you always need to show the current table to the user, in this case, Doctrine by default copes well by caching only the structure to the file tables.
There are no questions - everything works out of the box.
But suppose in table B, data selection is a very resource-intensive process, such a table is updated extremely rarely, and the user can be shown not the most up-to-date data.
It would be desirable without reinventing the wheel - that is, by standard means, to cache table B into a file.
Not only its structure, but also the result of the sql query.
That would not have to do a sample every time.
What are the standard solutions for table B - note that table A should not be cached in this way.
I use a bunch of symfony 4 + doctrine
PS: If you like answering off topic, you don’t need to write about the fact that the table can be optimized, I’m not stupid, I know all this, the question is specifically about managing the cache, this is the essence of the question, I want to hear about the cache and not about the need to optimize the table!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2019-01-18
@ff0xff

https://www.doctrine-project.org/projects/doctrine...

D
dosim86, 2019-01-24
@dosim86

You can preconfigure caching in Symfony for Doctrine:

framework:
    cache:
        pools:
            doctrine.result_cache_pool:
                adapter: cache.app
services:
    doctrine.result_cache_provider:
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
        - '@doctrine.result_cache_pool'
doctrine:
    orm:
        result_cache_driver:
            type: service
            id: doctrine.result_cache_provider

By default, Symfony uses file caching for user data, so the results of a Doctrine query will also be stored in a file. However, if, for example, you use Redis, then you can specify it as the default cache store:
framework:
    cache:
        # Redis
        app: cache.adapter.redis
        default_redis_provider: '%env(REDIS_URL)%'

Further, in the necessary queries, simply indicate to Doctrine that the result of the query should be cached:
...
->getQuery()
->useResultCache(true) // кешировать результат данного запроса
//->setResultCacheLifetime(3600) // время жизни кеша при необходимости
->getResult()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question