Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine ORM Caching in ZF2 Application

I use Doctrine 2 ORM in a Zend Framework 2 project and I wanted to clarify some details about caching.

Doctrine config looks like

return array(
    'doctrine' => array(
        'driver' => array(
            'application_entities' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'doctrine_cache_entities', // 1
                'paths' => array(__DIR__ . '/../src/Application/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    'Application\Entity' => 'application_entities',
                ),
            ),
        ),
        'configuration' => array(
            'orm_default' => array(
                'metadata_cache' => 'doctrine_cache_metadata', // 2
                'query_cache' => 'doctrine_cache_query', // 3
                'result_cache' => 'doctrine_cache_result', // 4
                'hydration_cache' => 'doctrine_cache_hydration', // 5
            )
        ),
    ),
);

Here we can see 5 different types of cache:

  • Entities cache
  • Metadata cache
  • Query cache
  • Result cache
  • Hydration cache

But there are only 3 options in Doctrine console tool to clear the cache:

  • orm:clear-cache:metadata Clear all metadata cache of the various cache drivers.
  • orm:clear-cache:query Clear all query cache of the various cache drivers.
  • orm:clear-cache:result Clear all result cache of the various cache drivers.

So how can I clear the rest cache (especially entities cache) considering that it can be stored in different places, not necessarily in the filesystem.


And the second question:

Should all these caches be enabled in production together (the question is mainly about Entities and Metadata cache since they seem to contain similar data)?

**N.B.* The chache info for the driver configuration comes from here

like image 471
Alexey Kosov Avatar asked Jun 04 '15 21:06

Alexey Kosov


1 Answers

The 1st cache you described (Entity Cache) is actually the metadata cache and allows you to override the type of caching used on entities in a paticulate module, therefore you clear the entity cache by running orm:clear-cache:metadata.

With the hydration cache, I am inclined to say that it is linked to the result cache.

From the documentation, the hydration cache is used when merging entities back into the Unit of Work, and given how expensive that is, it should be avoided, however if you were to use the hydration cache, intuition would suggest that by running orm:clear-cache:result you would clear the hydration cache.

Hydration cache: https://groups.google.com/forum/#!topic/doctrine-user/V4G4rRF7Ls4

Merging Entities into UofW: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#merging-entities

like image 187
Rob Spick Avatar answered Nov 07 '22 12:11

Rob Spick