Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the Cache-Control header for every response in Catalyst?

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?

like image 203
derobert Avatar asked Jul 24 '09 12:07

derobert


People also ask

Is Cache-Control a request or response header?

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).

What should you add to a Cache-Control response header to specify that a response?

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.

What happens if you don't set Cache-Control header?

Without the cache control header the browser requests the resource every time it loads a new(?) page.

How do I setup a cache header?

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'.


2 Answers

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

like image 164
jayk Avatar answered Sep 25 '22 20:09

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 ).

like image 29
Sinan Ünür Avatar answered Sep 25 '22 20:09

Sinan Ünür