【问题标题】:Configuring which Redis adapter to use in Symfony Cache配置在 Symfony 缓存中使用的 Redis 适配器
【发布时间】:2020-03-13 21:08:18
【问题描述】:

我想为所有 Redis 连接使用 Predis\Client 而不是 \Redis

Symfony 文档 on cache adapters 描述了您可以为 createConnection 方法提供其他选项。 但是,这都是在服务容器中自动装配的。我唯一要声明的是我想使用 Redis 进行缓存:

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

有什么方法可以配置 RedisAdapter 的默认选项?或者有没有其他方法可以让 Symfony 始终使用 Predis\Client 用于 Redis?

使用?class=\Predis\Client 配置DSN 可行,这是最佳解决方案吗?

【问题讨论】:

    标签: php symfony caching redis symfony4


    【解决方案1】:

    向 DSN 添加其他选项没有任何问题。能够只用一个字符串配置您的提供程序就是它存在的原因。但是,您可以定义自定义提供程序服务并使用您喜欢的任何配置。

    来自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 }
    

    在您的情况下,您需要将类更改为 Client\Predis 并更改适用的设置。

    【讨论】:

    • 酷!正是我想要的。谢谢:)
    猜你喜欢
    • 2019-03-24
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多