【发布时间】:2013-07-17 22:25:23
【问题描述】:
为了在控制器中设置没有缓存的响应,您可以这样做:
$response = new Response();
$result = $this->renderView(
'AcmeDemoBundle:Default:index.html.twig',
array('products' => $products, 'form' => $form->createView()));
$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
$response->setContent($result);
return $response;
但是使用注解,保证每个方法的结果都一样,怎么办?
我试过了,但继续保存缓存,如果我使用浏览器的后退按钮保留缓存:
/**
* @Cache(maxage="0", vary="no-cache, must-revalidate, no-store", smaxage="0", expires="now", public="false")
*/
class DefaultController extends Controller
{
/**
* Homepage: show products
*
* @Route("/", name="homepage")
* @Template
*/
public function indexAction()
{
$sessionCart = $this->get('demo');
$filters = $sessionCart->getFilters($this->getDoctrine()->getEntityManager());
$products = $this->getDoctrine()->getRepository('AcmeDemoBundle:Product')->search($filters);
$form = $this->createForm(new FilterType, $filters);
return array('products' => $products, 'form' => $form->createView());
}
如果按照文档所述强加:
@Cache(vary=["no-cache", "must-revalidate", "no-store"]...
给了我一个不期望“[”的语法错误,所以我按照上面的方法尝试了。
【问题讨论】:
-
通常哈希图在注释中用大括号定义。试试看:
@Cache(vary={"..."}) -
我试过了,和使用一样:
vary="no-cache, must-revalidate, no-store"在response-header中我总能找到vary no-cache,must-revalidate,no-store,但是继续拿缓存 -
妈妈,你得到响应中的标题了吗?
-
我也面临这个问题。您可以在响应中看到变化的标头,但它实际上并没有强制对 Vary 标头逻辑进行缓存(数组中的第一个标头除外)。让
@Cache注释与变量一起使用的唯一方法是执行@Cache(vary = "Accept-Encoding, X-Foo, X-Foo2")
标签: symfony symfony-2.3