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:
Is there a good way to accomplish this?
What is the Cache-Control Header. Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).
private. The private response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers). You should add the private directive for user-personalized content, especially for responses received after login and for sessions managed via cookies.
Without the cache control header the browser requests the resource every time it loads a new(?) page.
To use Cache-Control headers, choose Content Management | Cache Control Directives in the administration server. Then, using the Resource Picker, choose the directory where you want to set the headers. After setting the headers, click 'OK'.
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
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 )
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With