如何在Symfony应用程序中为Doctrine配置Redis缓存

4
在我的config_prod.yml文件中,我有以下配置。在哪里可以配置Redis驱动程序的选项,例如unix套接字(或主机),数据库编号等等?
doctrine:
    orm:
        metadata_cache_driver: redis
        query_cache_driver: redis
        result_cache_driver: redis
4个回答

13

对于symfony 4.4/5版本,以下是操作步骤,因为官方文档有些混乱。你需要先设置缓存池:

#config/packages/cache.yaml

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

    pools:
      custom_cache_pool:
        adapter: cache.adapter.redis

然后像这样将此池用于Doctrine结果缓存:

#config/packages/doctrine.yaml

doctrine:
  ...
  orm:
    result_cache_driver:
      type: pool
      pool: custom_cache_pool

7

@katona.abel提供的示例不起作用,但是启发了我找到了这个解决方案:

#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

此设置是否可以使Redis缓存变成可选的(以防特定的Redis服务器出现故障或错误)?因此,如果Redis服务器未安装或不可用,则Symfony将无缓存运行,但不会抛出异常。 - userfuser

2
这个配置应该可以正常工作。
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)>

参数信息请点击这里

更新

如果您需要相同的缓存,可以定义一个服务并将其传递

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

我知道那个指令。我需要找到默认的redis类型的配置在哪里。我的示例代码可以工作,但我需要在一个地方进行修改 - 而不是在每个选项下面。 - kuboslav

1

我注意到作者询问的是Redis,而不是Predis,但我发现对于3.4.*版本,最简单的“开箱即用”方式,如果你只是想快速开始:

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

这需要使用 predis 客户端:
$ composer require predis/predis

现在会发生什么?对于Doctrine元数据缓存,我们有内置的Doctrine\Common\Cache\PredisCache包装的Predis\Client。结果和查询缓存驱动程序被配置为元数据缓存驱动程序的别名(实际上是缓存提供程序的别名),因此它们都使用相同的数据库、主机和端口。如果您通过bin/console清除元数据缓存或直接通过redis-cli调用flushdb,则结果和查询的缓存也将被删除。这种解决方案不需要任何新的服务。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接