我已经看了几个选项,如:
- 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: apcYou might also want to check this and this blog entries
更多推荐
Symfony 2缓存Doctrine查询结果
发布评论