我有一个控制器,它的动作是在树枝上呈现的
{{ 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过期后才能呈现?
发布于 2015-06-03 08:48:43
在Symfony2中,为了激活esi缓存,您需要做一些事情。
1)在app/config/config.yml中,确保使用片段路径激活了esi。
framework:
esi: { enabled: true }
fragments: { path: /_proxy }2)用AppCache对象封装内核
// web/app.php
$kernel = new AppCache($kernel); 3)设置AppCache配置
// 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,
);
}
}关于您的问题,如果它缓存您的响应,唯一的问题是每次刷新页面时都会重新加载。确保configuration allow_reload属性设置为false。
你可以在这里读到更多关于它的信息:cache.html
https://stackoverflow.com/questions/30614573
复制相似问题