【问题标题】:How to store queries and their results in memcache using cache enabled for doctrine 2 (using zend framework 2 )?如何使用为学说 2 启用的缓存(使用 zend 框架 2)将查询及其结果存储在 memcache 中?
【发布时间】:2014-11-07 17:15:23
【问题描述】:

我想标题中的所有内容,我已经寻找了很多解决方案,但不知道该怎么做?这是我在module.config.php 中对memcache 的配置:

// 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;
         },
        ),
    ),

我正在使用 zend 框架 2 和教义 2 。 谢谢。

【问题讨论】:

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


    【解决方案1】:

    如果一切配置正确,您不需要为Query CacheMetadata Cache 进行任何其他配置,但是,要启用Result Cache,您必须在每个查询上显式调用useResultCache

    例子:

    <?php
    $query = $em->createQuery('select u from \Entities\User u');
    $query->useResultCache(true);
    

    【讨论】:

    • 感谢您的回答,它适用于结果缓存,但查询缓存呢?我的意思是我已经启用了内存缓存来存储 DQL 查询到 SQL 查询的转换?另外,如果我有一些复杂的查询,例如,我不想获取对象而是求和或平均值,我该如何存储它并在之后获取它?
    • 是的,这就是Query Cache 的目的,它只将DQL 解析为SQL 一次。回答您的第二个问题:Result Cache 存储已经水合的对象,因此,它将存储您希望从数据库中检索的任何内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    相关资源
    最近更新 更多