没有带注释的缓存头 [英] No cache header with annotation

查看:22
本文介绍了没有带注释的缓存头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在控制器中设置没有缓存的响应,您可以这样做:

In order to set the response without the cache in the controller you can do this:

$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;

但是使用注解,保证每个方法的结果都一样,怎么办?

But using annotations, to ensure that each method has the same result, how can you do?

我试过了,但继续保存缓存,如果我使用浏览器的后退按钮保留缓存:

I tried so but continues to save the cache and if I use the browser's Back button keeps the cache:

/**
 * @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());
}

如果按照文档说明强制执行:

If imposed as the documentation says:

@Cache(vary=["no-cache", "must-revalidate", "no-store"]...

给了我一个不需要["的语法错误,所以我尝试了上面的方法.

gives me a syntax error which does not expect "[", so I tried as above.

推荐答案

您将两件事混为一谈.在您的第一个代码段中,您正在设置缓存控制标头,但是您希望通过注释设置 Vary 标头.但是 VaryCache-Control 标头完全不同,no-cache, must-revalidate, no-store 应该在其中.Vary 意味着对请求(即 Cookies)的思考可以改变.请参阅此答案以了解:https://stackoverflow.com/a/1975677/2084176

You are mixing two things. In your first snippet you are setting cache control headers, but with the annotation you want to set the Vary header. But Vary is complete different than the Cache-Control header, in which no-cache, must-revalidate, no-store should stand. Vary means on which thinks of the request (i.e. Cookies) the Response can vary. See this answer for understanding: https://stackoverflow.com/a/1975677/2084176

在您的情况下(无缓存),您可以依赖 默认值,symfony设置的,如果没有缓存头:

In your case (no-cache) you can rely on the defaults, which symfony sets, if no cache headers are present:

Symfony2 会根据以下规则在开发人员未设置任何内容时自动设置合理且保守的 Cache-Control 标头:

Symfony2 automatically sets a sensible and conservative Cache-Control header when none is set by the developer by following these rules:

  • 如果没有定义缓存头(Cache-ControlExpiresETagLast-Modified),Cache-Control设置为no-cache,表示不会缓存响应;
  • If no cache header is defined (Cache-Control, Expires, ETag or Last-Modified), Cache-Control is set to no-cache, meaning that the response will not be cached;

如果您需要为每个控制器操作设置缓存头,您可以使用 kernel.response 事件.创建一个对这个事件做出反应的监听器,并使用适当的缓存控制修改响应.

if you need to set the cache header for every controller action, you can work with the kernel.response event. Create a listener which reacts on this event and modify the response with appropriate cache control.

namespace Acme\DemoBundle\EventListener;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

class AcmeCacheListener
{
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $response = $event->getResponse();

        $response->headers->addCacheControlDirective('no-cache', true);
        $response->headers->addCacheControlDirective('max-age', 0);
        $response->headers->addCacheControlDirective('must-revalidate', true);
        $response->headers->addCacheControlDirective('no-store', true);
    }
}

并在您的 services.yml

services:
    kernel.listener.your_listener_name:
        class: Acme\DemoBundle\EventListener\AcmeCacheListener
        tags:
            - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

这篇关于没有带注释的缓存头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆