Stillmatic1985
Stillmatic1985

Reputation: 1912

ZF2 Doctrine2 Entity Cache

Does somebody know how to cache doctrine2 entities in a zf2 project. I cant find a tutorial or website where this is explained. I cant find any information to start with defining a entity filecache.

Somebody of you got working links or examples.

Thanks

Upvotes: 2

Views: 3942

Answers (2)

v.babak
v.babak

Reputation: 906

To activate file cache you just need to add in your module.config.php

'doctrine' => array(
    'configuration' => array(
            'orm_default' => array(
                'metadata_cache' => 'filesystem',
                'query_cache' => 'filesystem',
            )
        ),
    )

and it will create cache automatically in data/DoctrineModule/cache folder

here is my full doctrine config for ZF 2.2.4 + Doctrine 2

'doctrine' => array(
        'driver' => array(
            'application_entities' => array(
                'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/Modulename/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    'Modulename\Entity' => 'application_entities'
                ),
            )
        ),
        'configuration' => array(
            'orm_default' => array(
                'metadata_cache' => 'filesystem',
                'query_cache' => 'filesystem',
            )
        ),
    ),

Upvotes: 3

El Yobo
El Yobo

Reputation: 14946

You have two options

  1. Use doctrine's native caching, e.g. using memcache (in the memcache block you can use any kind of doctrine supported cache, a full list of cache drivers is available).
  2. Use doctrine's adapter for Zend/Cache/Storage to use another cache that you're using elsewhere; the adapter is described in the DoctrineModule docs.

As an example of version two, I have something like the following configuration in a module (actually spread across various config files, so I can't guarantee that copy-pasting verbatim will work).

'services' => array(
  'factories' => array(
    // Wraps a ZF2 cache storage in a Doctrine compatible way
    'doctrine.cache.zend.static.local' => function ($services) {
      return new ZendStorageCache($services->get('cache.static.local'));
    },
),

'caches' => array(
  // A ZF2 cache, can be configured as you like
  'cache.static.local' => array(
    'adapter' => 'xcache',
    'plugins' => array(
      'exception_handler' => array(
        'throw_exceptions' => false,
       ),  
       'serializer',
    ),          
  ),
),

'doctrine' => array(
  'configuration' => array(
    'orm_default' => array(
      'metadata_cache' => 'zend.static.local',
      'query_cache'    => 'zend.static.local',
    ),
  ),
),

Note that Doctrine annoyingly automatically prefixes "doctrine.cache." to the name of the cache service that you configure, so while we configure "metadata_cache" to "zend.static.local", the actual cache service must be named "doctrine.cache.zend.static.local". Obviously you can call them what you want, but you'll need to add that prefix to whatever you call them.

Upvotes: 3

Related Questions