问题描述
我需要一些帮助,我已经去了很多,但没有结果:/我如何利用查询和他们的结果存储在memcache,我正在使用zend框架2和原则2 ?这是我在module.config.php中的配置:
// Doctrine config
'doctrine'=> array(
'driver'=> array(
__NAMESPACE__。'_driver'=> array(
'class'=>'Doctrine\ORM\Mapping\Driver\\ \\ AnnotationDriver',
'paths'=>数组(__ DIR__。'/../src/'。__NAMESPACE__。'/ Entity')
),
'orm_default'=>数组(
'drivers'=>数组(
__NAMESPACE__。'\Entity'=> __NAMESPACE__'_driver'
),
)
)
/ *****启用memcache **** /
'configuration'=>数组(
'orm_default'=>数组(
'metadata_cache'= >'mycache',
'query_cache'=>'mycache',
'result_cache'=>'mycache',
)
/ ** ** end **** /
)
),
'service_manager'=>数组(
'工厂'=>数组(
'translator'=>'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation'=>'Zend\\ \\ Navigation\Service\DefaultNavigationFactory',
'doctrine.cache.mycache'=> function($ sm){
$ cache = new \Doctrine\Common\Cache\MemcacheCache ();
$ memcache = new \Memcache();
$ memcache-> connect('localhost',11211);
$ cache-> setMemcache($ memcache);
return $ cache;
},
),
),
$ b谢谢你的任何想法或链接。
问候。
我想你使用,对吧?
将您的配置更改为:
// Doctrine config
'教义'=> array(
'driver'=> array(
__NAMESPACE__。'_driver'=> array(
'class'=>'Doctrine\ORM\Mapping\Driver\\ \\ AnnotationDriver',
'paths'=>数组(__ DIR__。'/../src/'。__NAMESPACE__。'/ Entity')
),
'orm_default'=>数组(
'drivers'=>数组(
__NAMESPACE__。'\Entity'=> __NAMESPACE__'_driver'
),
),
) ,
/ *****启用memcache **** /
'configuration'=>数组(
'orm_default'=>数组(
'metadata_cache' ='memcache',
'query_cache'=>'memcache',
'result_cache'=>'memcache',
)
),
/ **** end **** /
'cache'=>数组(
'memcache'=>数组(
'instance'=>'doctrine.cache.mycache',
),
),
),
'service_manager'=> array(
'factoryories'=> array(
'doctrine.cache.mycache'=> function($ sm){
$ cache = new \Doctrine\Common\ cache \MemcacheCache();
$ memcache = new \Memcache();
$ memcache-> connect('localhost',11211);
$ cache-> setMemcache $ memcache);
return $ cache;
},
),
),
这是如何工作的?
在模块配置中,每个支持的缓存适配器都有预定义的配置,。有了这个配置,你在说使用memcache进行缓存:
'configuration'=> ;数组(
'orm_default'=>数组(
'metadata_cache'=>'memcache',
'query_cache'=>'memcache',
'result_cache'= >'memcache',
)
),
此缓存需要配置Memcache实例和此配置说Memcache实例在ServiceManager中使用key'doctrine.cache.mycache'
'cache'=>数组(
'memcache'=>数组(
'instance'=>'doctrine.cache.mycache',
),
),
更新:
如何使用结果缓存():
$ cache = $ entityManager-> getConfiguration() - > getResultCacheImpl();
$ cacheItemKey ='my-item';
//测试缓存中是否存在项
if($ cache-> contains($ cacheItemKey)){
$ item = $ cache-> fetch($ cacheItemKey); //从缓存中检索项目
} else {
$ item = $ repository-> find($ id); //从存储库检索项目
$ cache-> save($ cacheItemKey,$ item); //保存项缓存
}
plz i need some help here , i've goolged a lot but without result :/how can i exploit the query and their result stored in the memcache , i'm working with zend framework 2 and doctrine 2 ? and here is my configuration in module.config.php :
// 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' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
)
/**** end ****/
)
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
any idea or link is appeciated , thanks.Regards.
解决方案 I suppose You are using DoctrineModule, right?Change your configuration to this:
// 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;
},
),
),
How does this work?
In module configuration are predefined configurations for every supported cache adapter, including memcache. With this configuration, you are saying "use memcache for caching":
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'memcache',
'query_cache' => 'memcache',
'result_cache' => 'memcache',
)
),
This cache needs configured Memcache instance and this config saying "Memcache instance is available in ServiceManager with key 'doctrine.cache.mycache'"
'cache' => array(
'memcache' => array(
'instance' => 'doctrine.cache.mycache',
),
),
Update:
How to use result cache (documentation):
$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
}
这篇关于如何在doctrine 2和zend framework 2中使用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!