4

In my config_prod.yml file I have following configuration. Where can I configure Redis driver – options like unix socket (or host), database number, etc. ?

doctrine:
    orm:
        metadata_cache_driver: redis
        query_cache_driver: redis
        result_cache_driver: redis
0

4 Answers 4

13

For symfony 4.4/5 here is how it's done, because the docs are a bit confusing.

You need to first set up the cache pool:

#config/packages/cache.yaml

framework:
  cache:
    default_redis_provider: 'redis://localhost' # or '%env(resolve:REDIS_URL)%'

    pools:
      custom_cache_pool:
        adapter: cache.adapter.redis

And then use this pool for the doctrine result cache like so:

#config/packages/doctrine.yaml

doctrine:
  ...
  orm:
    result_cache_driver:
      type: pool
      pool: custom_cache_pool
Sign up to request clarification or add additional context in comments.

Comments

8

Example given by @katona.abel not works, but lead me to this solution:

#services.yml
services:
    redis_cache_service:
        class: Doctrine\Common\Cache\RedisCache
        calls:
            - method: setRedis
              arguments:
                - '@redis'

    redis:
        class: Redis
        public: false
        calls:
            - method: connect
              arguments:
                - '/var/run/redis/redis.sock' # or host/ip with port
            - method: select
              arguments:
                - 13 # change database by index
#config_prod.yml
doctrine:
    orm:
        metadata_cache_driver:
            type: service
            id: redis_cache_service
        result_cache_driver:
            type: service
            id: redis_cache_service
        query_cache_driver:
            type: service
            id: redis_cache_service

1 Comment

Could this set up possibly also allow redis caching to be optional (in case something goes or is wrong with the specific redis server)? So, if redis server is not installed or is unavailable, the symfony goes on cacheless, but without throwing exceptions?
2

This config should work

doctrine:
    orm:
        metadata_cache_driver: 
            type: redis
            connection_id: <Redis connection service id>
            host:<Redis host>
            port:<Redis port>
            password:<Redis password>
            timeout:<Redis connection timeout>
            database:<Redis database selection (integer)>
            persistent:<Whether to use persistent connection or not (bool)>

parameter info here

UPDATE

if you need the same cache for all cache you could define a service and pass it

doctrine_cache:
    aliases:
        redis_cache: my_redis_cache
    providers:
        my_redis_cache:
            type: redis
            connection_id: <Redis connection service id>
            host:<Redis host>
            port:<Redis port>
            password:<Redis password>
            timeout:<Redis connection timeout>
            database:<Redis database selection (integer)>
            persistent:<Whether to use persistent connection or not (bool)>


doctrine:
    ...
    orm:
        entity_managers:
            default:
               ...    
               metadata_cache_driver:
                    type: service
                    id: redis_cache
                query_cache_driver:
                    type: service
                    id: redis_cache
                result_cache_driver:
                    type: service
                    id: redis_cache

1 Comment

I know that directive. I need to find where are configuration for default redis type. Code in my example works, but I need to modify it in one place – no under each option.
1

I've noticed that author asks about redis, not predis, but I found simplest "out-of-box" way for 3.4.*, if you just want a quick start:

doctrine:
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                metadata_cache_driver:
                    type: predis
                    host: '%app.redis.host%'
                    port: '%app.redis.port%'
                    database: 1
                result_cache_driver:
                    cache_provider: doctrine.orm.default_metadata_cache
                query_cache_driver:
                    cache_provider: doctrine.orm.default_metadata_cache

This requires predis client:

$ composer require predis/predis

What happens now? For doctrine metadata cache we have Predis\Client wrapped by built-in Doctrine\Common\Cache\PredisCache. Result and query cache drivers configured as aliases for metadata cache driver (aliases to cache provider, actually), so they all use same database, host and port. If you clear metadata cache by bin/console or call flushdb directly by redis-cli, cache for results and queries also will be erased.

This solution doesn't require any new services.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.