【问题标题】:Doctrine Second Level Cache w/ Redis带 Redis 的 Doctrine 二级缓存
【发布时间】:2015-11-24 05:18:42
【问题描述】:

在花了几天时间弄清楚为什么我的教义二级缓存配置不起作用之后,我希望有人能够支持。 目前没有二级缓存调用导致命中。

我的项目当前设置了以下包(+ 一些可能与此设置无关的其他包):

"symfony/symfony": "2.6.*",
"doctrine/orm": "2.*",
"doctrine/dbal": "2.*",
"doctrine/doctrine-bundle": "~1.2"
...
"snc/redis-bundle": "1.*"

Doctrine 缓存的设置方式如下:

orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    auto_mapping: true
    metadata_cache_driver:  redis
    query_cache_driver:     redis
    result_cache_driver:    redis
    second_level_cache:
        enabled:            true
        log_enabled:        true

元数据和查询缓存似乎工作正常,因为在 Redis 中创建了键,并且 SNC Redis Bundle 也正确记录了我的缓存命中。但是“2l Cache”只记录未命中和放置,而不是命中:

在调试过程中,我发现在来自 Doctrine/ORM/Query 的缓存请求中尝试访问 ArrayCache 而不是配置的缓存驱动程序。

如果有人有二级缓存的工作示例配置可能已经有所帮助,因为它既不适用于我的 Redis,也不适用于 APCu 或 memcached。

我希望有人有想法或者可以分享他的工作配置。

提前致谢并致以最诚挚的问候

【问题讨论】:

  • 似乎没有人知道如何将 SncRedisBundle 与学说的二级缓存一起使用,如果可能的话。

标签: symfony caching doctrine-orm redis second-level-cache


【解决方案1】:

好的,大约一个月后我终于得到了答案!

请注意,Doctrine 原生支持包括 redis 在内的许多缓存驱动程序,但就我而言,可能在 OP 的情况下也是如此,我需要使其与 SncRedisBundle 一起工作,以便利用 Redis 主从复制和/ 或聚类。

我在 Github 上得到了有用的反馈https://github.com/snc/SncRedisBundle/issues/216

基本上,您必须创建一个服务,该服务基本上是 services.yml 中的几行代码

....
services:
    snc_second_level_cache:
        class: %snc_redis.doctrine_cache.class%
        calls:
            - ["setRedis", ["@snc_redis.cache"]]
            - ["setNamespace", ["DoctrineSecondLevelCache"]] #Optional
....

然后在你的 config.yml 中

....
orm:
    entity_managers:
        default:
            second_level_cache:
                region_cache_driver:
                    type: service
                    id: snc_second_level_cache
                enabled: true
....

就是这样,享受吧!

更新 - 2016 年 1 月 19 日

截至今天,SncRedisBundle dev-master 分支现已兼容,并集成了对 Doctrine 二级缓存的支持

【讨论】:

  • 您能分享一个 SncRedisBundle 与 Doctrine 二级缓存集成的工作示例吗?谢谢!
【解决方案2】:

您还需要为二级缓存启用正确的cache_driver

second_level_cache:
      region_cache_driver:
          type:                 service
          id:                   doctrine_cache.providers.second_level
      enabled:              true
      regions:
          region_name:
              cache_driver:
                  type:                 service
                  id:                   doctrine_cache.providers.second_level

这是与DoctrineCacheBundle 结合使用的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 2013-09-30
    • 2013-02-11
    • 2014-11-13
    • 2014-06-28
    • 2014-08-27
    • 1970-01-01
    • 2014-03-01
    相关资源
    最近更新 更多