douluan4644 2014-05-28 09:36
浏览 20

Memcached在Zend 2中没有使用Doctrine 2

I can't seem to get memcached working with doctrine 2 in zend 2. Having memcached in place seems to have no effect on performance at all. I looks like it is not working. This is my setup:

module.config.php

 'doctrine' => array(
        'configuration' => array(
            'orm_default' => array(
                'metadata_cache'    => 'my_memcache',
                'query_cache'       => 'my_memcache',
                'result_cache'      => 'my_memcache'
            )
    ),
    'service_manager' => array(
        'factories'          => array(
            'doctrine.cache.my_memcache' => function ($sm) {
                    $cache = new \Doctrine\Common\Cache\MemcachedCache();
                    $memcached = new \Memcached();
                    $memcached->addServer('127.0.0.1', 22222);
                    $cache->setMemcached($memcached);
                    return $cache;
            },
        ),
    )

controller.php

public function get($id)
{
    $product = $this->getEntityManager()->createQuery('
        SELECT p,pc,fs,f
        FROM ProductMod\Entity\Product p
        LEFT JOIN p.prices pc
        LEFT JOIN p.formats fs
        LEFT JOIN fs.format f
        WHERE p.id = :id
        AND p.hide = false
        ')->setParameters(array('id' => $id))->useResultCache(true, 500, 'product'.$id);

    return $product->getArrayResult();
}

I have tested if memcached was working in php with a test script that stored some test data and that worked fine.

Update, this does work for me, though I would still like to know how to use the useResultCache.

    $mcache = $this->getServiceLocator()->get('doctrine.cache.my_memcache');
    if($mcache->contains('product'.$id))
    {
        echo'cached';
        $result = $mcache->fetch( 'product'.$id );
    }
    else
    {

        $product = $this->getEntityManager()->createQuery('
        SELECT p,pc,fs,f
        FROM ProductMod\Entity\Product p
        LEFT JOIN p.prices pc
        LEFT JOIN p.formats fs
        LEFT JOIN fs.format f
        WHERE p.id = :id
        AND p.hide = false
            ')->setParameters(array('id' => $id));

        $result = $product->getArrayResult();
        $mcache->save('product'.$id, $result, 500);

    }

    return $result;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数