2015-06-03 45 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