爲了設置,而不在控制器中的高速緩存可以做到這一點的響應:無緩存標頭註釋
$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"]...
給了我一個語法錯誤,不期望「[」,所以我試着如上。
Normaly包含HashMap用大括號定義在註釋中。試試看:'@Cache(vary = {「...」})' –
我試過了, 和使用:'''vary =「no-cache,must-revalidate,no-store」'' 在響應頭中,我總是發現'''改變了no-cache,must-revalidate,no-store''',但是繼續採用緩存 – Lughino
媽媽,你會在響應中獲得頭文件? –