1
votes

I am using Doctrine2 with ZF2 (Zend Framework2). As per http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/
I am using annotations within Doctrine, I need to know whether be default the annotations are cached and if so where, and if not how I can cache them.

I know Symfony2/Doctrine2 caches annotation data, how can I do it with Zend2?

1

1 Answers

2
votes

An example to use memcache for annotation caching would be (module.config.php):

'service_manager' => array(

    'factories' => array(
        'doctrine.cache.my_memcache' => function(\Zend\ServiceManager\ServiceManager $sm) {
            $cache = new \Doctrine\Common\Cache\MemcacheCache();
            $memcache = new \Memcache();
            $memcache->connect('localhost', 11211);
            $cache->setMemcache($memcache);

            return $cache;
        }
    )

This creates the basic memcache object for doctrine. Afterwards, you have to tell doctrine to use this cache for annotations like this (again, module.config.php):

'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'my_memcache', // <- this points to the doctrine.cache.my_memcache factory we created above
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
            ),
        ),
    ),
),

Alternatively, you can use the array cache of doctrine. simply change 'cache' => 'my_memcache' to 'cache' => 'array' (which should be the default anyway, as far as I know. Hope that helps!