Symfony2:私有ESI片段

Tho*_*s K 3 caching reverse-proxy symfony esi

我想知道是否存在类似私有ESI片段的东西.在我读到的文档中:

  1. "设置共享的最大年龄 - 这也将响应标记为公共 "
  2. "一旦开始使用ESI,请记住始终使用s-maxage指令而不是max-age.由于浏览器只接收聚合资源,因此它不知道子组件,因此它将遵循最大值 -年龄指令并缓存整个页面.你不希望这样."

我不完全理解我是否能够或不能按用户缓存页面的某些部分.有人可以解释一下吗?

提前致谢!

Gav*_*ove 5

您可以基于每个用户缓存页面的一部分.

它们的关键是清漆配置,您为ttl设置正常的共享最大年龄,然后将为该用户缓存esi请求.

然后看一下这个用于登录用户Varnish cookbook缓存,关键是你需要一个带有哈希用户ID的唯一cookie,并myapp_unique_user_id在示例中用你的cookie名称替换.

这是一个示例控制器,其中包含缓存和非缓存操作.

<?php

namespace MyTest\Bundle\HomepageBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class TestController extends Controller
{
    /**
     * UnCached html content
     *
     * @Route("/test_cache", name="homepage")
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function homepageAction()
    {
        return $this->render('MyTestHomepageBundle::index.html.twig');
    }

    /**
     * Cached user specific content
     *
     * @param integer $myTestUserId
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @Route("/test_user_cached/{myTestUserId}", name="homepage_user_specific_content")
     */
    public function userSpecificContentAction($myTestUserId)
    {
        $response = $this->render('MyTestHomepageBundle::userSpecificContent.html.twig', array('userId' => $myTestUserId));
        $response->setPublic();
        $response->setSharedMaxAge(3600);

        return $response;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是你的index.html

<!DOCTYPE html>
<head></head>
<body>

<h1>My Test homepage - {{ "now"|date("F jS \\a\\t g:i:s") }}</h1>
{{ render_esi(url('homepage_user_specific_content')) }}
</body>
Run Code Online (Sandbox Code Playgroud)

和userSpecificContent.html.twig

UserId: {{ userId }}  - {{ "now"|date("F jS \\a\\t g:i:s") }}
Run Code Online (Sandbox Code Playgroud)