如何为Catalyst中的每个响应设置Cache-Control标头?

der*_*ert 5 perl caching catalyst http-headers

似乎默认情况下Catalyst不输出Cache-Control:等标题.我知道我可以在给定的控制器方法中输出它们,如下所示:

$c->response->headers->last_modified(time);
$c->response->headers->expires(time + $self->{cache_time});
$c->response->headers->header(cache_control => "public, max-age=$self->{cache_time}");
Run Code Online (Sandbox Code Playgroud)

不过,在每种方法中做到这一点都会非常痛苦!我更喜欢的是:

  • 一组默认标头(现在过期,最后修改,缓存控制:无缓存,编译指示:无缓存)
  • 一种方法,per-methods,覆盖默认值.

有没有一个很好的方法来实现这一目标?

小智 6

derobert:

好问题.我在一篇关于Catalyst advent日历的文章中详细介绍了这一点.

基本上,您创建一个存储变量来定义给定操作的缓存时间,然后在根端例程中处理它.有关所有详细信息,请参阅文章.

JayK

  • 您可能希望在此答案中包含一个简短的代码段. (2认同)