Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 285
gstoert Avatar asked Aug 29 '15 19:08

gstoert


People also ask

What is second level cache?

A second-level cache is a local store of entity data managed by the persistence provider to improve application performance. A second-level cache helps improve performance by avoiding expensive database calls, keeping the entity data local to the application.

What is JPA second level cache?

Second level shared cache is an auxiliary technique, mainly used in JPA, to enhance performance; it is used especially during large inflow, outflow of data between the database and the application.

What is doctrine cache?

PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.

What are cache regions?

Caching Regions are specific region into the cache provider that might store entities, collection or queries.


2 Answers

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

like image 161
Don Omondi Avatar answered Sep 30 '22 06:09

Don Omondi


You also need to enable the correct cache_driver for the second level cache:

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

this is an example in conjunction with DoctrineCacheBundle.

like image 40
Robert Schönthal Avatar answered Oct 01 '22 06:10

Robert Schönthal