Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Doctrine 2 cache in a ZF2 project

How to enable cache in a project working with Zend Framework 2 and Doctrine 2? and what cache exactly should be enabled the doctrine cache or the zend cache?

Here what i've tried but can't see any diference in the time execution added in the

module\Application\config\module.config.php

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


    'doctrine.cache.apc' => function ($sm){
            $apc = new \Doctrine\Common\Cache\ApcCache();
            return $apc;
    },



    // Doctrine config
    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver'   => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'),
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ),
            )
        ),
        'configuration' => array(
            'orm_defaults' => array(
                'metadata_cache'    => 'apc',
                'query_cache'       => 'apc',
                'result_cache'      => 'my_memcache',
            )
        )
    ),

any help or idea or explication is appreciated. thanks.

like image 326
user3911183 Avatar asked Jan 09 '23 15:01

user3911183


1 Answers

To reduce unnecessary headaches, always use array cache on development time and memcached, redis or apc when your application running on production environment.

You should put your factory definitions under the service_manager > factories key, not directly in the module configuration array.

Try this in your module.config.php:

return [
    'doctrine' => [
        'configuration' => [
            'orm_default' => [
                'metadata_cache' => 'mycache',
                'query_cache' => 'mycache',
                'result_cache' => 'mycache',
                'hydration_cache' => 'mycache',
            ]
        ],
    ],

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

                return $cache;
            },
        ],
    ],
];

Also I strongly recommend moving factories to individual factory classes, always. This way, you'll have a more readable, maintainable and efficient application on production environment with the help of merged configuration cache.

For example:

'service_manager' => [
    'factories' => [
        'doctrine.cache.mycache' => \App\Memcache\Factory::class // implement FactoryInterface
        ],
    ],
];

Update for future readers after several years:

I would highly recommend looking into roave/psr-container-doctrine before writing custom dedicated factories for various doctrine components such as entity manager, config or cache. As a contributor of the library I can say that it serves the purpose nicely for most of the use cases I needed so far. When you really need a specialized factory, you can either extend or compose or decorate factories provided and add your own logic on top it.

like image 79
edigu Avatar answered Jan 17 '23 04:01

edigu