简体   繁体   中英

Can I invalidate an arbitrary cache entry with Varnish?

I am investigating if I can use Varnish to speed up a REST API.

Basically, I want to cache the GET requests for a potentially long time. But when a PUT / POST / DELETE request is received, I want to parse the URL and, based on the information I find, I want to purge a cache entry.

For example:

GET /documents/:docType // return document list for specified docType
DELETE /document/:docType/:docId // delete a document

GET /documents/A0  <-- cached
GET /documents/A1  <-- cached
DELETE /document/A0/333  <-- first entry is purged

Can I achieve this with VCL?

I suggest this varnish tutorial entry where purge and ban are explained.

You should be careful when purging as you shouldn't allow everyone to purge an url.

In order to do that you should do something like:

# IPs allowed to purge
acl purgeIps {
    "localhost";
    "192.168.55.0"/24;
}

Then in your vcl_recv you have to decide when to purge/ban:

if (req.request == "PUT" ||
    req.request == "POST" ||
    req.request == "DELETE"){
    if (!client.ip ~ purgeIps) {
        error 405 "Not allowed.";
    }
    purge; #I'm not sure if purge can be done here, if it doesn't work you should use it in vcl_hit and vcl_miss
    # Also, if purge does not work here you should change this line for return(lookup);
    error 200 "Purged";
}

Here you can find more examples about banning and purging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM