在我的 symfony 项目中,我有一个看起来像这样的“复杂”查询:
$d = Doctrine_Core::getTable('MAIN_TABLE')
// Create the base query with some keywords
->luceneSearch($keywords)
->innerJoin('w.T1 ws')
->innerJoin('ws.T2 s')
->innerJoin('w.T3 piv')
->innerJoin('piv.T4 per')
->innerJoin('w.T5 st')
...
->innerJoin('doc.T12 docT')
->innerJoin('w.Lang lng')
->execute();
由于我的数据模型,我添加了所有这些 innerJoin 以减少查询的数量。实际上,所有数据都通过这个唯一的查询恢复了……但是查询花费了 2 到 20 秒。取决于关键字。
我决定使用内存缓存,因为数据不会一直在变化。
我所做的是配置内存缓存并添加
...
->useResultCache(true)
->execute();
我的查询。
奇怪的是:
- 第一次(当缓存为空/刷新时),只执行一个查询
- 第二次执行了约 130 ares,比第一次花费的时间更多...
那些"new"查询正在为每条记录从“内部联接”中检索数据。
我不明白的是为什么“innerjoined”数据没有保存在缓存中?
我试着改变水合物模式,但似乎没有影响。
有人有想法吗?
最佳答案
经过一整天的谷歌搜索、分析 Doctrine 并变得绝望后,我找到了一篇解释解决方案的文章:
class User extends BaseUser{
public function serializeReferences($bool=null)
{
return true;
}
}
The problem was the profile object was not getting stored in the result cache and thus causing a query each time it was called from the user object. After much hunting around, a long time in #doctrine, and a few leads from a couple of people, it turns out, by default, Doctrine will only serialize the immediate relation to the main object. However, you can make it so that it will serialize objects further down the line by overriding the function serializeReferences to return true in the class you want to serialize references from. In my example this is the User class. Since our application will never only need the ‘User’ class to be serialized on a result cache I completely overrode the function and made it always return true
http://shout.setfive.com/2010/04/28/using-doctrine-result-cache-with-two-deep-relations/
关于php - 使用 Symfony/Doctrine 的 Memcache 正在重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166447/