Created
August 1, 2017 14:00
-
-
Save rezan/1dbecb4814b03a69fdb5c6d3e7e916e2 to your computer and use it in GitHub Desktop.
Updating the Cache-Control to reflect the smallest TTL included (Edgestash)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
varnishtest "Updating the Cache-Control to reflect the smallest TTL included (Edgestash)" | |
server s1 { | |
rxreq | |
txresp -hdr "Cache-Control: max-age=3600" -body { | |
This is a response with 2 partials | |
{{ > /include/p1 }} | |
{{ > /include/p2 }} | |
} | |
expect req.url == "/page" | |
rxreq | |
txresp -hdr "Cache-Control: max-age=120" -body "Partial #1" | |
expect req.url == "/include/p1" | |
rxreq | |
txresp -hdr "Cache-Control: max-age=30" -body "Partial #2" | |
expect req.url == "/include/p2" | |
} -start | |
varnish v1 -vcl+backend { | |
import edgestash; | |
import kvstore; | |
import std; | |
sub vcl_init { | |
# Init kvstore(0) for storing the smallest max-age | |
kvstore.init(0, 25000); | |
} | |
sub vcl_recv { | |
set req.http.X-top-url = req_top.url; | |
} | |
sub vcl_backend_response { | |
edgestash.parse_response(); | |
if (kvstore.get(0, bereq.http.X-top-url, "") == "" || | |
beresp.ttl < std.duration(kvstore.get(0, bereq.http.X-top-url, "0s"), 0s)) { | |
kvstore.set(0, bereq.http.X-top-url, beresp.ttl + "s"); | |
} | |
} | |
sub vcl_deliver { | |
edgestash.execute(); | |
set resp.http.X-smallest-ttl = kvstore.get(0, req.http.X-top-url, "NONE"); | |
} | |
} -start | |
client c1 { | |
txreq -url "/page" | |
rxresp | |
expect resp.status == 200 | |
expect resp.body ~ "This is a response with 2 partials" | |
expect resp.body ~ "Partial #1" | |
expect resp.body ~ "Partial #2" | |
expect resp.http.Cache-Control == "max-age=3600" | |
expect resp.http.X-smallest-ttl == "3600.000s" | |
txreq -url "/page" | |
rxresp | |
expect resp.status == 200 | |
expect resp.body ~ "This is a response with 2 partials" | |
expect resp.body ~ "Partial #1" | |
expect resp.body ~ "Partial #2" | |
expect resp.http.Cache-Control == "max-age=3600" | |
expect resp.http.X-smallest-ttl == "30.000s" | |
} -run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment