How to use the cache in doctrine 2 and zend framew

2019-03-31 20:32发布

问题:

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.

回答1:

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
}