Set ttl etc. explicitly using a request header
I’m building a fastly proxy to a backend service I don’t own because said service doesn’t offer great caching. The vcl does very little other than read a few properties in the url query string to activate various caching presets
e.g
if (req.url ~ "interval=weekly") {
beresp.ttl = 7d;
}
What I would love to do is something like sending a Cache-Control
header with the original request, setting this to Surrogate-Control
on the response, and thus allowing the caching of the response to be completely configurable via the request. But I can’t figure out where and how I should do this, or even if it’s possible.
Is it possible?
-
Strictly speaking within VCL, your proposed solution doesn't work straight up. This is because
Surrogate-Control
is evaluated beforevcl_fetch
is run.However, you can set the TTL based on any header, using a couple of functions. It would look something like:
if (req.http.Cache-Control ~ "max-age=([0-9]+)") { set beresp.ttl = std.integer2time(std.atoi(re.group.1)); }
Note, I didn't test this code, but I think it should work.
-
For reference, here is a working solution https://github.com/Financial-Times/keen-proxy/pull/1
One thing that held me up for ages - and I'm not sure where this is/should be documented - but req.http.Cache-Control could not be read within vcl_fetch, hence the proprietary
Cache-Strategy
header
Please sign in to leave a comment.
Comments
5 comments