Goran Jurić

Using APC cache with Doctrine & Symfony? Check again!

Published on

If you have read the documentation you probably know that having a Metadata cache for Doctrine is something you really want to have in production.

The setup in Symfony is pretty easy, you just have to edit the config_prod.yml:

    metadata_cache_driver: apc
    query_cache_driver: apc

The problem is that the Dependency Injection Container is generated through the Command Line Interface, and if you do not have apc.enable_cli=1 in your php.ini DIC will use FileCacheReader instead.

You can check your /app/cache/prod/appProdProjectContainer.php to see if you are using APC cache.

    protected function getDoctrine_Orm_DefaultEntityManagerService()
        $a = $this->get('annotation_reader');
        $b = new \Doctrine\Common\Cache\ApcCache();
        $c = new \Doctrine\Common\Cache\ApcCache();
// ...
        $g = new \Doctrine\ORM\Configuration();
// ...

If you can not find \Doctrine\Common\Cache\ApcCache you are not using APC to cache your metadata.

For a simple page with one query the difference between APC and File based caching can be quite big. Take a look at the numbers I got from XHProf for a simple page:

Cache type Wall time CPU time Memory usage
APC 254,972 µs 242,020 µs 10,352,536 bytes
File 355,617 µs 325,320 µs 11,579,568 bytes
Difference 39.47 % 34.42 % 11.85 %