Search code examples
symfonycachingdoctrine-ormredissecond-level-cache

Doctrine Second Level Cache w/ Redis


after investing a few days now in figuring out why my second level cache config for doctrine is not working, I hope someone might be able to support. At the moment no second level cache call result in a hit.

My project is currently set up with the following packages (+ some other which are probably not relevant for this setup):

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

The Doctrine cache is set up the following way:

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

The metadata & query cache seems to work properly, as there are keys created within Redis and the SNC Redis Bundle also properly logs my cache hits. But the "2l Cache" just logs misses and puts, instead of hits:

no 2l cache hits

During my debugging, I found out that within the cache requests from the Doctrine/ORM/Query try to access the ArrayCache instead of the configured cache driver.

It might already help if someone has a working example configuration for the second level cache as it neither works with Redis for me, nor for APCu or memcached.

I hope someone has an idea or can just share his working config.

Thanks in advance & best regards


Solution

  • Ok so I finally got the answer to this after about a month!

    Please note that Doctrine has native support for many cache drivers including redis but, in my case, probably in the OP's case as well, I needed to make it work with SncRedisBundle so as to take advantage of Redis Master-Slave replication and/or Clustering.

    I got my answer with helpful feedback on Github here https://github.com/snc/SncRedisBundle/issues/216

    Basically, you must create a service which is basically a few lines of code in services.yml

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

    then in your config.yml

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

    That's it, Enjoy!

    UPDATE - 19th Jan, 2016

    As of today, SncRedisBundle dev-master branch is now compatible and comes with integrated support for Doctrine Second Level Cache