VMOD_PURGE(3)
π VMOD_PURGE(3) β the Library Functions Manual glow-up π
okay bestie, pull up a chair, because we're reading the vmod_purge page from the Library Functions Manual together and honestly? it's giving cache-eviction main character energy β¨
β¨ NAME β¨
vmod_purge β the Varnish Purge Module π§Ήπ
π¦ SYNOPSIS π¦
import purge [as name] [from "path"]
INT hard()
INT soft(DURATION ttl, DURATION grace, DURATION keep)
like, that's the whole vibe. two little functions. both absolute icons. π
πΈ DESCRIPTION πΈ
so vmod_purge is a lil module that contains functions offering finer-grained control than just return(purge) from vcl_recv{}. that's the whole point, that's the tea. π«
β οΈ the functions can only be called fromvcl_hit{}orvcl_miss{}β nowhere else, bestie, that's a hard boundary
and also, like, you should in general use them in both vcl_hit{} and vcl_miss{}, because that's the only way to make sure all variants of a same object are taken care of. no leaving stragglers behind, that's not the vibe. π
π EXAMPLE π
here's the whole setup, verbatim, no notes, she ate:
sub vcl_recv {
if (req.method == "PURGE") {
if (client.ip !~ purge_acl) {
return (synth(405));
}
return (hash);
}
}
sub my_purge {
set req.http.purged = purge.hard();
if (req.http.purged == "0") {
return (synth(404));
}
else {
return (synth(200));
}
}
sub vcl_hit {
if (req.method == "PURGE") {
call my_purge;
}
}
sub vcl_miss {
if (req.method == "PURGE") {
call my_purge;
}
}
sub vcl_synth {
if (req.method == "PURGE") {
if (req.http.purged) {
set resp.http.purged = req.http.purged;
}
return (deliver);
}
}
π INT hard() π
okay so this one is equivalent to return(purge) β same energy, same outcome β but you call it explicitly from vcl_hit{} and vcl_miss{}. that's the difference. that's it. π
it returns the number of purged objects. a whole count. a receipt. π§Ύ
Example:
set req.http.purged = purge.hard();
Restricted to: vcl_hit, vcl_miss.
π« literally do not call it anywhere else, the varnish gods will simply not allow it
π
INT soft(DURATION ttl, DURATION grace, DURATION keep) π
INT soft(DURATION ttl=0, DURATION grace=-1, DURATION keep=-1)
look at those defaults β that's the signature, exactly as written, don't @ me. π
so what it does is it sets the ttl, grace and keep. by default, ttl is set to 0 with the grace and keep periods left untouched. and here's the cute part: setting grace or keep to a negative value or to something higher than the object's current value leaves them untouched. so you can't accidentally inflate a lifetime, bestie, the module simply refuses. π
setting all three parameters to 0 is equivalent to a hard purge. yes, fr fr, they merge like that.
it returns the number of soft-purged objects. π
β οΈ a soft-purge can only decrease the lifetime of an object. it cannot extend it. ever. that's the rule and she does not bend.
let's consider an object in cache created with ttl, grace, and keep of 60 seconds each:
purge.soft(ttl = 0s, grace = -1s, keep = -1s);
- if the object is fresh, the
ttlis reduced to0seconds and the object expires after 120 seconds. β³ - if the object is stale, the expiry time is not changed. untouched, unbothered. π
purge.soft(ttl = 0s, grace = 10s, keep = 10s);
- if the object is fresh, the
ttlis reduced to0seconds,graceandkeepare reduced to10seconds. the object expires after 20 seconds. - if the object has been stale for 5 seconds,
graceis reduced to5seconds andkeepis reduced to10seconds. the object expires after 15 seconds. - if the object has been stale for 15 seconds,
graceis reduced to0seconds andkeepis reduced to5seconds. the object expires after 5 seconds. - if the object has been stale for 20 seconds or more, the object immediately expires. π gone. poof.
purge.soft(ttl = 10s, grace = -1s, keep = -1s);
- if the object has been fresh for 5 seconds, the
ttlis reduced to10seconds. the object expires after 130 seconds. - if the object has been fresh for 55 seconds, the
ttlis not changed. the object expires after 125 seconds. - if the object is stale, the expiry time is not changed.
π the logging bit (iykyk) π
when the expiry time of an object is reduced due to a softpurge, an EXP_Reduce entry is logged under the ExpKill VSL tag. π
if a softpurge does not reduce the expiry time of an object, an EXP_Unchanged entry is logged instead. so you always get a little paper trail, queen. π
Restricted to: vcl_hit, vcl_miss.
π« same deal as hard() β those two subs and nowhere else
β SEE ALSO β
go read that one next bestie, it's the whole world this module lives in π
π COPYRIGHT π
okay legal moment, but make it cute β everything below is verbatim, protect it with your life:
Copyright (c) 2017 Varnish Software AS
All rights reserved.
Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
SPDX-License-Identifier: BSD-2-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
and that's the whole page bestie!! π«Ά you just read VMOD_PURGE(3) from the Library Functions Manual β that footer line is literally the same VMOD_PURGE(3) you saw at the top, so consider this your lil sign-off: go forth, purge responsibly, and never call these functions outside vcl_hit or vcl_miss. mwah. πβ¨
π¦ where this page comes from
- alpine 3.22: varnish-doc 7.7.3-r0 Β· BSD-2-Clause
- alpine 3.23: varnish-doc 7.7.3-r0 Β· BSD-2-Clause
- alpine 3.24: varnish-doc 7.7.3-r0 Β· BSD-2-Clause
- debian trixie: varnish 7.7.0-3+deb13u1 Β· BSD-2-Clause
- debian trixie: varnish 7.7.0-3+deb13u2 Β· BSD-2-Clause
- fedora 44: varnish 8.0.2-1.fc44 Β· BSD-2-Clause AND (BSD-2-Clause-FreeBSD AND BSD-3-Clause AND LicenseRef-Fedora-Public-Domain AND Zlib)
- fedora 44: varnish 8.0.2-1.fc44 Β· BSD-2-Clause AND (BSD-2-Clause-FreeBSD AND BSD-3-Clause AND LicenseRef-Fedora-Public-Domain AND Zlib)
- opensuse-leap 16.0: varnish 7.7.3-bp160.1.1 Β· BSD-2-Clause
- opensuse-leap 16.0: varnish 7.7.3-bp160.1.1 Β· BSD-2-Clause
- opensuse-leap 16.0: varnish 7.7.3-bp160.1.1 Β· BSD-2-Clause
- opensuse-leap 16.0: varnish 7.7.3-bp160.1.1 Β· BSD-2-Clause
- ubuntu questing: varnish 7.7.3-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms