我想你正在使用教义模块 https://github.com/doctrine/DoctrineModule, 正确的?
将您的配置更改为:
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
),
),
/***** enabling the memcache ****/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'memcache',
'query_cache' => 'memcache',
'result_cache' => 'memcache',
)
),
/**** end ****/
'cache' => array(
'memcache' => array(
'instance' => 'doctrine.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;
},
),
),
这是如何运作的?
模块配置中是每个受支持的缓存适配器的预定义配置,包括内存缓存 https://github.com/doctrine/DoctrineModule/blob/master/config/module.config.php#L36。通过此配置,您可以说“使用 memcache 进行缓存”:
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'memcache',
'query_cache' => 'memcache',
'result_cache' => 'memcache',
)
),
此缓存需要配置 Memcache 实例,并且此配置显示“Memcache 实例在 ServiceManager 中可用,密钥为‘doctrine.cache.mycache’”
'cache' => array(
'memcache' => array(
'instance' => 'doctrine.cache.mycache',
),
),
Update:
如何使用结果缓存(文档 http://doctrine-orm.readthedocs.org/en/latest/reference/caching.html):
$cache = $entityManager->getConfiguration()->getResultCacheImpl();
$cacheItemKey = 'my-item';
// test if item exists in the cache
if ($cache->contains($cacheItemKey)) {
$item = $cache->fetch($cacheItemKey); // retrieve item from cache
} else {
$item = $repository->find($id); // retrieve item from repository
$cache->save($cacheItemKey, $item); // save item to cache
}