Enable Doctrine 2 Cache in ZF2 Project
How do I enable cache in a project running Zend Framework 2 and Doctrine 2? and in which cache should doctrine cache or zend cache be enabled?
Here is what I tried but don't see any difference in runtime added in
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. thank.
source to share
To reduce unnecessary headaches, always use the cache array
during development and memcached
, redis
or apc
when your application is running in production.
You should put your factory definitions under the service_manager
> key factories
, not directly in the module config array.
Try the following:
// module.config.php
return array(
'doctrine' => array(
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
'hydration_cache' => 'mycache',
)
),
),
'service_manager' => array(
'factories' => array(
'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 highly recommend moving factories into separate factory classes, always. This way, you will have a more readable, maintainable, and efficient application in production with a unified configuration cache .
For example:
'service_manager' => array(
'factories' => array(
'doctrine.cache.mycache' => 'Your\Memcache\Factory' // implement FactoryInterface
),
),
);
source to share