如何在使用Zend Framework 2和Doctrine 2的项目中启用缓存?到底应该启用什么缓存呢?教义缓存或zend缓存?
这是我尝试过的,但是看不到添加在时间执行上的任何差异
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',
)
)
),
任何帮助或想法或解释表示赞赏。谢谢。
为了减少不必要的麻烦,一定要使用array
高速缓存的开发时间和memcached
,redis
或apc
在生产环境中运行应用程序时。
您应该将工厂定义放在service_manager
>factories
键下,而不是直接放在模块配置数组中。
在您的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;
},
],
],
];
我也强烈建议始终将工厂移至单个工厂类别。这样,在合并配置缓存的帮助下,您将在生产环境上拥有一个更具可读性,可维护性和效率的应用程序。
例如:
'service_manager' => [
'factories' => [
'doctrine.cache.mycache' => \App\Memcache\Factory::class // implement FactoryInterface
],
],
];
几年后为将来的读者更新:
我强烈建议您在为各种准则组件(例如实体管理器,配置或缓存)编写定制专用工厂之前,先研究roave / psr-container-doctrine。作为库的贡献者,我可以说它很好地满足了我到目前为止所需的大多数用例。当您确实需要专门的工厂时,可以扩展或组成或装饰所提供的工厂,并在上面添加自己的逻辑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句