3

I want to make use of Predis\Client instead of \Redis for all the Redis connections.

The Symfony docs on cache adapters describe that you can give additional options to the createConnection method. However, this is all autowired in the service container. The only thing I'm declaring is that I want to use Redis for caching:

framework:
  cache:
    app: cache.adapter.redis
    default_redis_provider: '%redis_dsn%'

Is there any way I can configure the default options for the RedisAdapter? Or is there another way that I can set Symfony always to use Predis\Client for Redis?

Configuring the DSN with ?class=\Predis\Client works, is this the optimal solution?

1 Answer 1

4

There's nothing wrong with adding additional options to the DSN. Being able to configure your provider with just a string is why it exists. However you can define a custom provider service and use whatever configuration you'd like.

From https://symfony.com/doc/current/cache.html#custom-provider-options:

# config/packages/cache.yaml
framework:
    cache:
        pools:
            cache.my_redis:
                adapter: cache.adapter.redis
                provider: app.my_custom_redis_provider

services:
    app.my_custom_redis_provider:
        class: \Redis
        factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
        arguments:
            - 'redis://localhost'
            - { retry_interval: 2, timeout: 10 }

In your case you'd change the class to Client\Predis and change the applicable settings.

Sign up to request clarification or add additional context in comments.

2 Comments

Cool! Exactly what I was looking for. Thanks :)
@Will how to configure it for multiples nodes (using a redis cluster for example) ?

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.