[Varnish 4] Respecting client's Cache-Control: max-age= as TTL

Martynas Jusevičius martynas at atomgraph.com
Tue Aug 1 00:01:12 CEST 2017


Thanks Guillaume.

First I tried

sub vcl_hit {
    if (req.http.Cache-Control ~ "max-age=[0-9]*") {
        set req.http.Max-Age = regsub(req.http.Cache-Control,
"max-age=([0-9]*)", "\1");
        if (obj.age > std.duration(req.http.Max-Age + "s", 1000000s)) {
            std.log("obj.age: " + obj.age + " req.http.Max-Age: " +
req.http.Max-Age);
            return(fetch);
        }
    }
    ...

but I got an error:

-   VCL_call       HIT
-   ReqHeader      Max-Age: 69
-   VCL_Log        obj.age: 102.306 req.http.Max-Age: 69
-   VCL_return     fetch
-   VCL_Error      change return(fetch) to return(miss) in vcl_hit{}
-   VCL_Error      vcl_hit{} returns miss without busy object.  Doing pass.
-   VCL_call       PASS
-   VCL_return     fetch

I did as told and I tried


On Mon, Jul 31, 2017 at 9:11 PM, Guillaume Quintard <
guillaume at varnish-software.com> wrote:

> man vcl
>
> bereq is filtered to avoid side effects of the client forcing the ttl to
> the backed.
>
> Anyway, by the time you have access to bereq, it's too late for you since
> the decision to go to the backend has already been been made.
>
> --
> Guillaume Quintard
>
>
> On Jul 31, 2017 19:56, "Martynas Jusevičius" <martynas at atomgraph.com>
> wrote:
>
> Thanks. What was mostly unclear to me is passing the req header value all
> the way to where it's used to set TTL.
>
> Why doesn't bereq contain the req headers? At least Cache-Control is gone.
>
> But I guess that can be done using obj.ttl, which I didn't know about. Any
> documentation on that?
>
> On Mon, 31 Jul 2017 at 18.38, Guillaume Quintard <
> guillaume at varnish-software.com> wrote:
>
>> On github I pointed to the doc explaining how you can return(fetch) to
>> ignore a cached object, possibly based on ttl, so you already have half the
>> answer.
>>
>> The other part of the equation is just converting req.http.cache-control
>> to a duration and comparing that to obj.ttl. It will be similar to what you
>> have done on v3.
>>
>> --
>> Guillaume Quintard
>>
>> On Jul 31, 2017 18:25, "Martynas Jusevičius" <martynas at atomgraph.com>
>> wrote:
>>
>>> Hi,
>>>
>>> I have been reading quite a bit about Varnish and VCL but found almost
>>> no examples with Cache-Control coming from the client request [1].
>>>
>>> What I want to achieve: if the client sends Cache-Control: max-age=60,
>>> TTL becomes 60 s. If the cache hit is fresher than 60 s, deliver it,
>>> otherwise fetch a new response from backend (I hope I'm not misusing the
>>> VCL terms here) *and* cache it.
>>>
>>> I had hacked this together in the vcl_fetch section in Varnish 3.x by
>>> setting the req.http.Cache-Control max-age value as beresp.ttl, but
>>> vcl_fetch is gone in Varnish 4.x.
>>>
>>> I have received a suggestion to use vcl_hit and/or grace [2], but again
>>> -- no examples...
>>>
>>> Could anyone provide some VCL pseudo-code that
>>> uses req.http.Cache-Control value to override TTL? max-age number parsing
>>> not necessary, I have figure that out.
>>>
>>> Thanks,
>>>
>>> Martynas
>>>
>>> [1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Header
>>> s/Cache-Control#Cache_request_directives
>>> [2] https://github.com/varnishcache/varnish-cache/issues/
>>> 2014#issuecomment-319096566
>>>
>>> _______________________________________________
>>> varnish-misc mailing list
>>> varnish-misc at varnish-cache.org
>>> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20170801/00016d92/attachment.html>


More information about the varnish-misc mailing list