Symfony2:ESI setMaxAge缓存 [英] Symfony2: ESI setMaxAge Cache
问题描述
我有一个控制器,其控制器动作用
I have a Controller whose Action is rendered in twig with
{{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
每次都会更改。
When I reload my Browser, the "myTime"
changes everytime.
如何使用 setShardeMaxAge()
,以便仅在MaxAge过期后才渲染Twig? / p>
How can I use setShardeMaxAge()
, so that the Twig is only renderd after the MaxAge is expired?
推荐答案
在Symfony2中,您需要执行一些操作才能激活esi缓存。
In Symfony2 there's a few things you need to do in order to activate esi caching.
1)在 app / config / config.yml
中,确保您激活了带片段路径的esi。
1) In app/config/config.yml
make sure you activated esi, with a fragments path.
framework:
esi: { enabled: true }
fragments: { path: /_proxy }
2)用AppCache对象包装内核
2) Wrap the kernel with the AppCache object
// web/app.php
$kernel = new AppCache($kernel);
3)设置AppCache配置
3) Set up the AppCache configuration
// 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,
);
}
}
关于您的问题是否正在缓存您的回复和唯一的问题是,每次刷新页面时都会重新加载。确保配置 allow_reload
属性设置为false。
About your issue if it is caching your response and the only problem is that it's reloading every time you refresh the page. make sure the configuration allow_reload
property is set to false.
您可以在此处了解更多信息:
http://symfony.com/doc/current/book/http_cache.html
You can read more about it here: http://symfony.com/doc/current/book/http_cache.html
这篇关于Symfony2:ESI setMaxAge缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!