Score:0

How to return banned objects length in varnish purge?

in flag

i am Using Varnish 6.0 LTS in debian

iam using magento2 vcl 6 from here:

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/PageCache/etc/varnish6.vcl

when i issue a curl request with a tag pattern i would like the response to be:

"Found and purged X object hits"

the section from the vcl is here :

   if (req.http.X-Magento-Tags-Pattern) {
          ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
        }
        if (req.http.X-Pool) {
          ban("obj.http.X-Pool ~ " + req.http.X-Pool);
        }
 #Here i would like to return the Ban Purged Hits counter
        
return (synth(200, "You have Purged X items"));
Score:0
in flag

vmod_xkey

I suggest you use the vmod_xkey for that. It's an open source Varnish module that performs tag-based invalidation. You can download the source code from https://github.com/varnish/varnish-modules.

However, you have to compile this module from source.

One you have successfully installed the VMOD, you can find the API here: https://github.com/varnish/varnish-modules/blob/master/src/vmod_xkey.vcc.

There are some restrictions though: currently you're tagging responses with the X-Magento-Tags keyword. This will have to change to the xkey keyword instead. Hopefully you can make this change in Magento.

Once you've done that, you can call set req.http.n-gone = xkey.purge(req.http.X-Magento-Tags-Pattern);. The req.http.n-gone header contains the number of purged items.

vmod_ykey

If vmod_xkey is too restrictive, you can also use vmod_ykey, which is the Varnish Enterprise alternative. It offers more flexibility.

See https://docs.varnish-software.com/varnish-cache-plus/vmods/ykey/ for the documentation.

As mentioned in the documentation, you can tag content as follows:

sub vcl_backend_response {
    ykey.add_header(beresp.http.X-Magento-Tags);
    ykey.add_header(beresp.http.X-Pool);
}

Invalidation can be done through set req.http.n-gone = ykey.purge_header(req.http.X-Magento-Tags-Pattern);.

DISCLAIMER: Varnish Enterprise is commercial software that requires a license. However, you can use official machine images in the Cloud. This allows you to pay the license on a pay-as-you-go basis. There are Developer Edition images for AWS where lower fees are charged.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.