Symfony 2缓存Doctrine查询结果 [英] Symfony 2 cache Doctrine query results

查看:224
本文介绍了Symfony 2缓存Doctrine查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个使用教义的Symfony2项目。我想通过向查询添加缓存来优化API性能。



我已经看了几个选项,如:




  • Symfony注释缓存

  • Doctrine缓存

  • Memcache



不确定我应该去哪一个,但对我来说似乎是在Doctrine级别缓存数据是最合适的。



说我希望有人帮助我,或指导我如何设置Doctrine缓存,并解释它是如何工作的。



我有这个查询:

  class QueryFactory 

protected $ connect;

public function __construct(Connection $ connection)
{
$ this-> connect = $ connection;
}

私有函数myQuery()
{
return $ this-> connect-> createQueryBuilder()
- > select(' user_id')
- > from('users','u')
- > where('u.user_id = 2');
}
}

如何向此查询添加缓存?有没有任何教义图书馆需要注入任何我需要的东西使用

解决方案

doctrine 中缓存查询或结果,您可以执行以下操作:

  private function myQuery()
{
return $ this-> connect-> createQueryBuilder()
- > select('user_id')
- > ; from('users','u')
- > where('u.user_id = 2')
- > getQuery()
- > useQueryCache(true)/ / here
- > useResultCache(true); // and here
}

检查 doc 了解有关这些方法的更多信息。这将使您的缓存驱动程序正常工作 - 无论您正在使用什么驱动程序。



配置 Symfony 使用特定的查询驱动程序,您需要在 config.yml 中调整设置 - 检查查看完整的选项列表。您的缓存中重要的是(这是 apc 配置示例):

  entity_managers:
some_em:
query_cache_driver:apc
metadata_cache_driver:apc
result_cache_driver:apc

您可能还想查看这个博客条目


I am working on a Symfony2 project using Doctrine. I want to optimise the API performance by adding cache to queries.

I have looked at few options such as:

  • Symfony annotation cache
  • Doctrine cache
  • Memcache

Not to sure with which one I should go but to me it seems like caching data at Doctrine level would be most suitable.

Saying that I would like someone to help me or guide me how to set up Doctrine cache and explain how it exactly works.

I.e I have this query:

class QueryFactory

    protected $connect;

    public function __construct(Connection $connection)
    {
        $this->connect = $connection;
    }

    private function myQuery()
    {
        return $this->connect->createQueryBuilder()
            ->select('user_id')
            ->from('users', 'u')
            ->where('u.user_id = 2');
    }
}

How would I add a cache to this query? Is there any Doctrine library I need to inject any thing I need to use?

解决方案

In doctrine to cache queries or results you can do the following:

private function myQuery()
{
    return $this->connect->createQueryBuilder()
        ->select('user_id')
        ->from('users', 'u')
        ->where('u.user_id = 2')
        ->getQuery()
        ->useQueryCache(true)    // here
        ->useResultCache(true);  // and here
}

Check doc for more info about these methods. This will make your cache driver working - doesn't matter what driver you are using.

To configure Symfony to use specific query driver you need to adjust your settings in config.yml - check this to see complete list of options. What is important in your cache is (this is apc configuration example):

entity_managers:
        some_em:
            query_cache_driver: apc
            metadata_cache_driver: apc
            result_cache_driver: apc

You might also want to check this and this blog entries

这篇关于Symfony 2缓存Doctrine查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆