5

似乎默认情况下 Catalyst 不输出Cache-Control:, etc. 标头。我知道我可以像这样在给定的控制器方法中输出它们:

$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}");

不过,在每种方法中这样做都会很痛苦!我更喜欢的是:

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

有没有什么好方法可以做到这一点?

4

2 回答 2

6

德罗贝尔:

很好的问题。我在 Catalyst 降临日历 的一篇文章中详细介绍了这一点。

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

周杰伦

于 2009-07-24T17:11:03.787 回答
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 )

于 2009-07-24T13:24:32.757 回答