2013-04-10 102 views
0

我正在開發一個symfony2網站,我需要緩存幾個頁面,但有些部分必須保持未緩存(如用戶菜單等)。我已經檢查過這個文檔,而且ESI似乎正是爲此而構建的。Symfony2 ESI緩存+最後修改日期

我開始在我的博客文章頁面的項目中實現緩存。我按照Symfony2文檔中的建議設置了最後修改和Etag的緩存驗證。

我在我的所有頁面的標題中有一個用戶菜單。它使用ESI進行渲染,並確保它不緩存。據我所知,它不工作。每次我的整個博客文章頁面都會與用戶菜單一起被緩存。它被保存到瀏覽器緩存中,並且只在我實際更新博客文章時更新(這是正確的)。

這裏是我的博客文章控制器代碼:

public function showAction($slug) 
{ 
$response = new Response(); 
$response->setETag(md5($response->getContent())); 
$date = $article->getModifiedAt(); 
$response->setLastModified($date); 
$response->setPublic(); 
$response->headers->addCacheControlDirective('must-revalidate', true); 
// Check that the Response is not modified for the given Request 
if ($response->isNotModified($this->getRequest())) { 
    // return the 304 Response immediately 
     return $response; 
    } else { 
    //do stuff 
    return $this->render('NewsBundle:News:show.html.twig', array(
      'article' => $article, 
     ),$response); 
} 

我的用戶菜單控制:

public function userMenuAction() 
{ 
$response = new Response(); 
$response->setSharedMaxAge(0); 

return $this->render('MainBundle:Views:userMenu.html.twig', array(
      'user' => $user, 
    ),$response); 
} 

MY ESI路由

ESI_userMenu: 
pattern: user-menu 
defaults: { _locale: en, _controller: MainBundle:Default:userMenu } 

ESI渲染:

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

當我加載我的博客文章頁面時,我注意到用戶菜單也被緩存。 我測試了更多,並發現如果我不使用「isNotModified」,但設置了過期壽命時間,ESI確實有效。

有什麼方法可以讓ESI與我用於博客文章的「isNotModified」結構一起工作?

感謝

編輯:

ESI似乎並不與驗證緩存工作...

在這裏看到: With Symfony2 why are ESI tags inside cached responses ignored?

回答