Symfony 2缓存Doctrine查询结果

编程入门 行业动态 更新时间:2024-10-21 20:31:17
本文介绍了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查询结果

本文发布于:2023-10-13 10:17:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   查询结果   Symfony   Doctrine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!