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

时间:2009-07-24 12:56:44

标签: perl caching http-headers catalyst

默认情况下,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}");
但是,在每种方法中,这样做会非常痛苦!我更喜欢的是:

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

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

2 个答案:

答案 0 :(得分:6)

derobert:

很好的问题。我在article中为Catalyst出现日历详细介绍了这一点。

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

JayK

答案 1 :(得分:3)

更新:根据您对我之前建议的回应,我决定咬紧牙关并查看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 )拨打电话。

相关问题