How to set a default cache lifetime for doctrine in Symfony 4.4?

2.9k views Asked by At

I would like to set a default cache lifetime / timeout for doctrine caches in my different environments.

php bin/console config:dump-reference doctrine

shows only options to set the lifetime quite deep down in the advanced configuration in the entity manager regions, where I rather would not mess around. I cannot believe that there is no place to set a general value for db result caching.

Is there really no option to set the default timeout for the cache in a config file?

currrent config/test/doctrine.yaml:

doctrine:
    orm:
        auto_generate_proxy_classes: false
        metadata_cache_driver:
            type: service
            id: doctrine.system_cache_provider
        query_cache_driver:
            type: service
            id: doctrine.system_cache_provider
        result_cache_driver:
            type: service
            id: doctrine.result_cache_provider

services:
    doctrine.result_cache_provider:
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.result_cache_pool'
    doctrine.system_cache_provider:
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.system_cache_pool'

framework:
    cache:
        pools:
            doctrine.result_cache_pool:
                adapter: cache.app
            doctrine.system_cache_pool:
                adapter: cache.system
2

There are 2 answers

0
Pouya Amani On

In config/packages/prod/doctrine.yaml Need to change to:

doctrine:
    orm:
        auto_generate_proxy_classes: false
        metadata_cache_driver:
            type: service
            id: doctrine.system_cache_pool
        query_cache_driver:
            type: pool
            pool: doctrine.system_cache_pool
        result_cache_driver:
            type: pool
            pool: doctrine.result_cache_pool

framework:
    cache:
        pools:
            doctrine.result_cache_pool:
                adapter: cache.app
            doctrine.system_cache_pool:
                adapter: cache.system
0
ninsky On

I've did it like this...

framework:
    cache:
        default_memcached_provider: 'memcached://localhost:...'
        pools:
            doctrine.result_cache_pool:
                adapter: cache.adapter.memcached
                public: false
                default_lifetime: 86400