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

如何在Symfony应用程序中为条令配置Redis缓存,symfony,doctrine-orm,redis,Symfony,Doctrine Orm,Redis,在我的config\u prod.yml文件中,我有以下配置。我在哪里可以配置Redis驱动程序–unix套接字(或主机)、数据库号等选项 doctrine: orm: metadata_cache_driver: redis query_cache_driver: redis result_cache_driver: redis 这个配置应该可以工作 doctrine: orm: metadata_cache_d

在我的
config\u prod.yml
文件中,我有以下配置。我在哪里可以配置Redis驱动程序–unix套接字(或主机)、数据库号等选项

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

这个配置应该可以工作

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)>
原则:
orm:
元数据\u缓存\u驱动程序:
类型:redis
连接id:
主持人:
端口:
密码:
超时:
数据库:
持久的:
参数信息

更新

如果所有缓存都需要相同的缓存,可以定义一个服务并传递它

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
u缓存:
别名:
redis\u缓存:我的redis\u缓存
供应商:
my_redis_缓存:
类型:redis
连接id:
主持人:
端口:
密码:
超时:
数据库:
持久的:
教条:
...
orm:
实体管理人员:
违约:
...    
元数据\u缓存\u驱动程序:
类型:服务
id:redis_缓存
查询\u缓存\u驱动程序:
类型:服务
id:redis_缓存
结果\u缓存\u驱动程序:
类型:服务
id:redis_缓存

由@katona.abel给出的示例不起作用,但将我引向这个解决方案:

#services.yml
服务:
redis_缓存_服务:
类别:条令\Common\Cache\RedisCache
电话:
-方法:setRedis
论据:
-“@redis”
redis:
类别:Redis
公众:错
电话:
-方法:连接
论据:
-“/var/run/redis/redis.sock”或带有端口的主机/ip
-方法:选择
论据:
-13#按索引更改数据库
#config_prod.yml
教条:
orm:
元数据\u缓存\u驱动程序:
类型:服务
id:redis\u缓存\u服务
结果\u缓存\u驱动程序:
类型:服务
id:redis\u缓存\u服务
查询\u缓存\u驱动程序:
类型:服务
id:redis\u缓存\u服务

我注意到,作者询问的是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
现在发生了什么?对于条令元数据缓存,我们有内置的
doctor\Common\cache\PredisCache
包装的
Predis\Client
。结果和查询缓存驱动程序配置为元数据缓存驱动程序的别名(实际上是缓存提供程序的别名),因此它们都使用相同的数据库、主机和端口。如果您通过
bin/console
清除元数据缓存,或通过redis cli直接调用
flushdb
,则结果和查询的缓存也将被擦除


此解决方案不需要任何新的服务。

对于symfony 4.4/5,这里是它的实现方式,因为文档有点混乱

您需要首先设置缓存池:

#config/packages/cache.yaml

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

    pools:
      doctrine.result_cache_pool:
        adapter: cache.adapter.redis
然后将此池用于条令结果缓存,如下所示:

#config/packages/doctrine.yaml

doctrine:
  ...
  orm:
    result_cache_driver:
      type: pool
      pool: doctrine.result_cache_pool

我知道那个指令。我需要找到默认
redis
类型的配置在哪里。我的示例中的代码可以工作,但我需要在一个地方修改它——每个选项下都不能。这样的设置是否允许redis缓存成为可选的(以防特定redis服务器出现问题)?因此,如果redis服务器未安装或不可用,symfony将使用无缓存,但不会引发异常?