Doctrine结果缓存不会使用连接缓存查询 [英] Doctrine result cache not caching a query with a join

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

问题描述

我正在使用Doctrine 2.2.2试图为用户实体查询一个用户实体。我想要这个查询被缓存,因为里面的数据不会经常改变,如果有的话。以下是我试过的:

I'm using Doctrine 2.2.2 trying to do a query for a user entity along with the user's company. I want this query to be cached since the data inside won't change often, if at all. Below is what I've tried:

$user = $em->createQuery('
    SELECT u, c
    FROM Entities\User u
    LEFT JOIN u.company c
    WHERE u.id = :id
')->setParameter('id', $identity)->useResultCache(true, 21600, 'user_' . $identity)->getResult();

看起来,用户实体被缓存,但它仍然对公司进行查询。有没有一种方法可以同时获取两个并将它们放在结果缓存中?

Seemingly, the user entity is cached, but it still does a query for the company. Is there a way I can fetch both at the same time and have them be placed into the result cache?

我确实发现这个,但它是非常老的,他的解决方案对我来说不起作用。

I did find this, but it's very old and his solution did not work for me.

对我来说,我做了这个,但是那种打败了结果缓存的目的。

It was suggested to me that I do this, but that kind of defeats the purpose of the result cache.

推荐答案

我已经使用Doctrine 2.3进行了测试:

I've tested this with Doctrine 2.3 using:

$q->setResultCacheDriver( new \Doctrine\Common\Cache\ApcCache() )
  ->setResultCacheLifetime( 21600 )
  ->setResultCacheId( 'user_' . $identity );

当我第一次运行它时,我看到我的sql-logger中的实际查询。


当我运行第二个,第三个等时间时,我的sql-logger中没有任何东西。

When I run it the first time, I see the actual query in my sql-logger.
When I run it the second, third, etc time, I don't see anything in my sql-logger.

我的结论是它的工作正常。

My conclusion is it's working fine.

我想你没有配置结果缓存,或者你有一个非永久的结果缓存(如ArrayCache)配置,或者它是Dontrine 2.2.2中的一个错误(尽管我在jira上找不到任何信息)。

I guess you either haven't got a result-cache configured, or you have a non-permanent result-cache (like ArrayCache) configured, or it's a bug in Dontrine 2.2.2 (although I can't find anything about it on jira).

PS:我从另一个问题(原则分离,缓存和合并)你还有这个问题吗?

PS: I gather from another question (Doctrine detaching, caching, and merging) that you've upgraded to Doctrine 2.3. Do you still have this problem?

PPS:Doctrine 2.0之后的某个地方(我认为是2.2),结果缓存改变了。代替缓存水合结果,sql结果被缓存(每次运行都会发生水合)。如果要缓存水合效果,则必须使用水化缓存(但是将这些结果合并到EntityManager中)。

PPS: Somewhere after Doctrine 2.0 (I think it was 2.2), the result-cache changed. In stead of caching the hydrated result, the sql-result is cached (and hydration takes place every run). If you want to cache the hydrated result, you'll have to use the hydration-cache (but be ware that those results are not merged into the EntityManager).

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

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