使用Symfony2,为什么忽略了缓存的响应中的ESI标签? [英] With Symfony2 why are ESI tags inside cached responses ignored?

查看:103
本文介绍了使用Symfony2,为什么忽略了缓存的响应中的ESI标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子商务应用程序,我试图将其设置为缓存-最初是通过Symfony2反向代理,但最终是通过生产中的Varnish。我在Apache2上使用Symfony 2.1.8。

I have an ecommerce application that I'm try to set up for caching - initially via the Symfony2 Reverse Proxy, but then ultimately via Varnish in production. I'm using Symfony 2.1.8 on Apache2.

我的问题是,当缓存了主控制器操作时,无法为每个请求重新检查ESI标签(对于私有请求很重要)内容,例如购物篮内容),但我不明白为什么。

My problem is I cannot get the ESI tags to be re-checked for each request when the primary controller action is cached (important for private content like the basket contents), but I don't understand why.

例如,我用以下代码缓存主页:

For example, I cache the homepage with the following code:

public function indexAction(Request $request)
{
    // check cache
    $homepage = $this->getHomepage();

    $response = new Response();
    $response->setPublic();
    $etag = md5('homepage'.$homepage->getUpdated()->getTimestamp());
    $response->setETag($etag);
    $response->setLastModified($homepage->getUpdated());

    if ($response->isNotModified($request))
    {
        // use cached version
        return $response;
    }
    else
    {
        return $this->render(
            'StoreBundle:Store:index.html.twig',
            array(
                'page' => $homepage
            ),
            $response
        );
    }
}

渲染的模板扩展了基本布局模板,其中包括以下ESI来显示篮子:

The rendered template extends the base layout template which includes the following ESI to show the basket:

{% render 'PurchaseBundle:Basket:summary' with {}, { 'standalone': true } %}

(编辑:在阅读了迭戈的回答后,我还使用了推荐的语法:

( After reading Diego's answer, I have also used the recommended syntax:

{% render url('basket_summary') with {}, {'standalone': true} %}

不幸的是,这没有什么区别。)

Unfortunately this had not made any difference.)

我一直在研究篮子摘要的代码,但这是我目前的想法。 / p>

I've been playing with the code for the basket summary quite a bit, but this is what I have at present.

public function summaryAction()
{
    $response = new Response();
    $response->setPrivate();
    $response->setVary(array('Accept-Encoding', 'Cookie'));

    if ($this->basket->getId())
    {
        $etag = md5($this->getUniqueEtag());
        $response->setLastModified($this->basket->getUpdated());
    }
    else
    {
        $etag = md5('basket_summary_empty');
    }

    $response->setETag($etag);

    if ($response->isNotModified($this->request))
    {
        // use cached version
        return $response;
    }
    else
    {
        return $this->render(
            'PurchaseBundle:Basket:summary.html.twig',
            array(
                'basket' => $this->basket
            ),
            $response
        );
    }
}

在首页以外的页面(尚未缓存)上的购物篮摘要缓存工作正常,它始终显示正确的数据。只有返回首页后,您才能看到过时的信息。日志记录确认 summaryAction 不会在首页上调用,除非 indexAction 实际上呈现。

On pages other than the homepage (which are not cached yet) the basket summary caching works just fine, it always displays the correct data. It's only when you return to the homepage that you'd see outdated information. Logging confirms that summaryAction is not called on the homepage unless indexAction actually renders.

在每个页面请求后使用 error_log($ kernel-> getLog())我得到了一个非缓存的页面:

Using error_log($kernel->getLog()) after each page request I get this for a non-cached page:

GET /categories/collections: miss; GET /_internal/secure/PurchaseBundle:Basket:summary/none.html: stale, valid, store; GET /_internal/secure/CatalogBundle:Search:form/none.html: miss; GET /esi/menu/main: fresh

这用于缓存的首页:

GET /: fresh

我肯定会遗漏一些明显的东西,但是文档似乎并没有涵盖这个问题,但这意味着这只是ESI应该用于的事情。

I must be missing something obvious, but the documentation doesn't appear to cover this, yet it implies it's just the sort of thing ESI is supposed to be used for.

推荐答案

看来Symfony2中的ESI无法与您使用的Latmodified / Etag缓存验证结构一起使用。请参阅此处: https://groups.google.com/forum/ ?fromgroups =#!topic / symfony2 / V4BItzpLbOs

It appears that ESI in Symfony2 doesnt work with the Latmodified/Etag cache validation structure that you used. See here: https://groups.google.com/forum/?fromgroups=#!topic/symfony2/V4BItzpLbOs

此处类似的问题:
Symfony 2中的Edge Side Includes和验证快取

我一直在尝试与您相同的方法,但是只能使用过期缓存来使ESI正常工作。

I've been trying to do the same as you, but can only get ESI to work by using expiration cache.

这篇关于使用Symfony2,为什么忽略了缓存的响应中的ESI标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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