如何在doctrine 2和zend framework 2中使用缓存?

use*_*183 3 php orm caching doctrine-orm zend-framework2

PLZ我在这里需要一些帮助,我已经进行了很多但是没有结果:/我如何利用查询及其存储在memcache中的结果,我正在使用zend framework 2和doctrine 2?这是我在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;
         },
        ),
    ),
Run Code Online (Sandbox Code Playgroud)

任何想法或链接是指定的,谢谢.问候.

lku*_*lku 9

我想你正在使用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;
        },
    ),
),
Run Code Online (Sandbox Code Playgroud)

这是如何运作的?

在模块配置中是每个支持的缓存适配器的预定义配置,包括memcache.使用此配置,您说"使用memcache进行缓存":

'configuration' => array(
    'orm_default' => array(
        'metadata_cache'    => 'memcache',
        'query_cache'       => 'memcache',
        'result_cache'      => 'memcache',
    )
),
Run Code Online (Sandbox Code Playgroud)

此缓存需要配置Memcache实例,此配置说"Memcache实例在ServiceManager中可用,带有密钥'doctrine.cache.mycache'"

'cache' => array(
    'memcache' => array(
        'instance' => 'doctrine.cache.mycache',
    ),
),
Run Code Online (Sandbox Code Playgroud)

更新:

如何使用结果缓存(文档):

$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
}
Run Code Online (Sandbox Code Playgroud)