Search code examples
perlcachinghttp-headerscatalyst

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?


Solution

  • 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