Memcache 与 Symfony/Doctrine 正在重新加载数据

发布于 2024-10-20 04:40:58 字数 859 浏览 1 评论 0原文

在我的 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个查询,并且比第一次花费更多的时间......

那些“新”查询正在检索每个记录的“内部连接”数据。

我不明白的是为什么“内连接”数据没有保存在缓存中?

我尝试更改水合物模式,但似乎不受影响。

有人有主意吗?

In my symfony project, I have a "complex" query that looks like:

$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();

I added all those innerJoin to reduce the number of query due to my data model. Actually all data are recovered with this only query.... but the query took from 2 to 20 sec. depends on keywords.

I decided to use memcache because data are not changing all the time.

What I've done is configuring memcache and adding

...
->useResultCache(true)
->execute();

to my query.

What is strange is that :

  • The first time (when the cache is empty/flushed), only one query is execute
  • The second time, ~130 ares executed and it take more time than the first...

Those "new" queries are retrieving data from "inner join" for each record.

What I don't undestand is why "innerjoined" data are not saved in the cache?

I tried to change the hydrate mode but it seems not to be influent.

Someone has an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-10-27 04:40:58

经过一整天的谷歌搜索、分析教义和绝望之后,我找到了一篇解释解决方案的文章:

class User extends BaseUser{
    public function serializeReferences($bool=null)
    {
        return true;
    }
}

问题是配置文件对象没有存储在结果缓存中,因此每次从用户对象调用它时都会导致查询。经过大量的搜索、在 #doctrine 中呆了很长时间以及一些人的一些线索,事实证明,默认情况下,Doctrine 只会序列化与主要对象的直接关系。但是,您可以通过重写函数serializeReferences 以在要序列化引用的类中返回 true 来进一步序列化对象。在我的示例中,这是 User 类。由于我们的应用程序永远不需要在结果缓存上序列化“User”类,因此我完全重写了该函数并使其始终返回 true

http://shout.set Five.com/2010/04/28/using-doctrine-result-cache-with-两个深层关系/

After a whole day to googlise, to analyse doctrine and become desperate, I found an article that explain the solution:

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文