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

时间:2014-11-05 16:33:37

标签: php caching orm doctrine-orm zend-framework2

因为我需要一些帮助,我已经骂了很多但没有结果:/ 我如何利用查询及其存储在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;
         },
        ),
    ),

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

1 个答案:

答案 0 :(得分: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;
        },
    ),
),

这是如何运作的?

在模块配置中,是每个支持的缓存适配器including memcache的预定义配置。使用此配置,您说“使用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',
    ),
),

更新

如何使用结果缓存(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
}