在学说2和zend框架2中如何使用缓存?

Posted

技术标签:

【中文标题】在学说2和zend框架2中如何使用缓存?【英文标题】:How to use the cache in doctrine 2 and zend framework 2? 【发布时间】:2014-11-05 16:33:36 【问题描述】:

请我在这里需要一些帮助,我已经搜索了很多但没有结果:/ 我如何利用存储在内存缓存中的查询及其结果,我正在使用 zend 框架 2 和教义 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;
         ,
        ),
    ),

任何想法或链接都可以接受,谢谢。 问候。

【问题讨论】:

“mycache”是您创建的自定义缓存吗? 不,我正在启用 memcache 来存储查询和结果的学说查询,但我仍然可以使用它吗?我正在寻找如何保存查询以及如何获得结果... 在示例的“启用内存缓存”中,您使用 mycache 作为 Doctrine 的 3 种缓存类型的值。这个 mycache 在你的代码中做了什么,它调用了哪些代码? “mycache”是memcache 的一个对象,我已将其用于三种类型... 好的,这是重要信息。请显示 mycache 代码、显示任何调试或错误信息的原则查询的日志文件,并查看最终是否存在 php 错误。其他 Doctrina 缓存类是否有效?缓存的配置如何? 【参考方案1】:

我想你正在使用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

【讨论】:

感谢 Iku 的回答,我真正需要的是:如何在我的控制器中启用缓存以从内存缓存中获取结果?我想我没有很好地解释我的问题。 @user3911183 没问题,如果它帮助您解决问题,请考虑接受答案 返回一个问题请教,启用缓存中的缓存自动将每个查询和每个结果存储在缓存中?在此先感谢! @user3911183 对于查询缓存,这是真的,一旦激活查询缓存,它会自动缓存所有 DQL 转换。相反,结果缓存需要手动激活 - 见文档:doctrine-orm.readthedocs.org/en/latest/reference/…

以上是关于在学说2和zend框架2中如何使用缓存?的主要内容,如果未能解决你的问题,请参考以下文章

如何创建队列系统以在zend框架2.2中发送电子邮件。

学说 2 和 ORM:如何缓存某个实体的每个查询?

如何使用 zend from 在 zend 框架 2 中添加自定义属性

markdown Zend框架3:学说迁移

如何在 zend 框架 2 或 AjaxContext 中使用 ajax?

如何在 zend 框架 2 中呈现页面?