如何为 Catalyst 中的每个响应设置 Cache-Control 标头?
看来默认情况下 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}");
不过,在每个方法中这样做都会非常痛苦! 我更喜欢的是:
- 一组默认的标头(现在过期,最后修改,缓存控制:无缓存,编译指示:无缓存)
- 一种按方法覆盖默认值的方法。
有没有好的方法来实现这一点?
It seems that by default Catalyst does not output Cache-Control:
, etc. headers. I know I can output them in a given controller method like this:
$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}");
It'd get pretty painful doing that in each method, though! What I'd prefer is:
- A default set of headers (expires now, last modified now, cache-control: no-cache, pragma: no-cache)
- A way to, per-method, override the default.
Is there a good way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
德罗伯特:
很好的问题。 我在 Catalyst 的文章中详细介绍了这一点出现日历。
基本上,您创建一个存储变量来定义给定操作的缓存时间,然后在根端例程中处理它。 请参阅文章了解所有详细信息。
杰克
derobert:
Excellent question. I covered exactly this in an article for the Catalyst advent calendar.
Basically you create a stash variable that defines your cache time for the given action, and then you process it in your Root end routine. See the article for all the details.
JayK
更新:根据您对我之前建议的回复,我决定硬着头皮查看 Catalyst 文档。 在我看来,执行此操作的位置位于:
早期响应: 我不使用 Catalyst,但是您不能为您的应用程序编写一个子程序吗?
使用
set_default_response_headers( $c->response->headers )
进行调用。Update: Based on your response to my earlier suggestion, I decided to bite the bullet and look at the Catalyst docs. It seems to me, the place to do this is in:
Earlier response: I do not use Catalyst, but couldn't you just write a sub for your application?
Call with
set_default_response_headers( $c->response->headers )
.