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

发布于 2024-07-29 16:37:35 字数 475 浏览 4 评论 0原文

看来默认情况下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-08-05 16:37:36

德罗伯特:

很好的问题。 我在 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

无力看清 2024-08-05 16:37:36

更新:根据您对我之前建议的回复,我决定硬着头皮查看 Catalyst 文档。 在我看来,执行此操作的位置位于:

  sub end : Private {
    my ( $self, $c ) = @_;

    # handle errors etc.

    if ( $c->res->body ) {
        if ( "some condition" ) {
            set_default_response_headers( $c->response->headers );
            return;
        }
        else {
            do_something_else();
            return;
        }
    }
    $c->forward( 'MyApp::View::TT' ); # render template
}

早期响应: 我不使用 Catalyst,但是您不能为您的应用程序编写一个子程序吗?

sub set_default_response_headers {
    my ($h) = @_;
    $h->last_modified(time);
    $h->expires(time + $self->{cache_time});
    $h->header(cache_control => "public, max-age=$self->{cache_time}");
    return $h;    
}

使用 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:

  sub end : Private {
    my ( $self, $c ) = @_;

    # handle errors etc.

    if ( $c->res->body ) {
        if ( "some condition" ) {
            set_default_response_headers( $c->response->headers );
            return;
        }
        else {
            do_something_else();
            return;
        }
    }
    $c->forward( 'MyApp::View::TT' ); # render template
}

Earlier response: I do not use Catalyst, but couldn't you just write a sub for your application?

sub set_default_response_headers {
    my ($h) = @_;
    $h->last_modified(time);
    $h->expires(time + $self->{cache_time});
    $h->header(cache_control => "public, max-age=$self->{cache_time}");
    return $h;    
}

Call with set_default_response_headers( $c->response->headers ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文