Melvin Koopmans
Melvin Koopmans

Reputation: 3060

Configuring which Redis adapter to use in Symfony Cache

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?

Upvotes: 3

Views: 11457

Answers (1)

Will
Will

Reputation: 121

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.

Upvotes: 4

Related Questions