jan.bogutzki.de

Set TTL in Varnish 4

You can set the Varnish ttl in many ways. Some set it with hard values in the vcl some prefer to give the ttl via a header setting with the requested site. To set the ttl in varnish 2 and 3 you could use inline C code like the one below:

# this on the beginning of the vcl
C{
    #include <stdlib.h>
}C
...
sub vcl_fetch
{
    ...
    if (beresp.http.X-CacheTime)
    {
        C{
            char *ttl;
            ttl = VRT_GetHdr(sp, HDR_BERESP, "\023X-CacheTime:");
            VRT_l_beresp_ttl(sp, atoi(ttl));
        }C
    }
    ...
}

With Varnish 4 the code must be changed a little bit, because the function declaration of VRT_GetHdr has changed. To migrate your Varnish 2 or 3 vcl you have to use this code in your vcl_fetch:

C{
    char *ttl;
    const struct gethdr_s hdr = { HDR_BERESP, "\023X-CacheTime:" };
    ttl = VRT_GetHdr(ctx, &hdr); // number of chars
    VRT_l_beresp_ttl(ctx, atoi(ttl));
}C