2015-06-03 46 views
2

我有一個控制器,其動作呈現在樹枝的Symfony2:ESI setMaxAge緩存

{{ render_esi(controller('MyWebsiteBundle:Element:header')) }}

行動本身看起來是這樣的:

/** 
    * @return Response 
    */ 
    public function headerAction() 
    { 
     $currentLocale = $this->getCurrentLocale(); 

     $response = $this->render('MyWebsiteBundle:Element:header.html.twig', array(
      'currentLocale' => $currentLocale, 
      'myTime' => time() 
     )); 
     $response->setPublic(); 
     $response->setSharedMaxAge(3600); 

     return $response; 
    } 

當我重裝我的瀏覽器中,"myTime"每次更換。

我怎樣才能使用setShardeMaxAge(),這樣Twig只會在MaxAge過期後呈現?

+0

你使用'app_dev.php'或'app.php'訪問頁面? – nifr

+0

目前我使用app_dev.php – Zwen2012

+0

您是否在app_dev.php中啓用了symfony的內部緩存代理AppCache([howto?](http://symfony.com/doc/current/book/http_cache.html) #symfony-reverse-proxy))? – nifr

回答

4

在Symfony2中,爲了激活esi緩存,你需要做一些事情。

1)在app/config/config.yml請確保您使用片段路徑激活esi。

framework: 
    esi: { enabled: true } 
    fragments: { path: /_proxy } 

2)包裹內核設置的應用程序緩存配置

// app/AppCache.php 
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; 

class AppCache extends HttpCache 
{ 
    protected function getOptions() 
    { 
     return array(
      'debug'     => false, 
      'default_ttl'   => 0, 
      'private_headers'  => array('Authorization', 'Cookie'), 
      'allow_reload'   => false, 
      'allow_revalidate'  => false, 
      'stale_while_revalidate' => 2, 
      'stale_if_error'   => 60, 
     ); 
    } 
} 

關於你的問題,如果它是緩存的響應應用程序緩存對象

// web/app.php 
$kernel = new AppCache($kernel); 

3)和唯一的問題每次刷新頁面時都會重新加載。確保配置allow_reload屬性設置爲false。

你可以閱讀更多關於它在這裏: http://symfony.com/doc/current/book/http_cache.html