我可以使用Varnish使任意缓存条目无效吗?

时间:2015-07-03 19:41:53

标签: rest caching varnish varnish-vcl

我正在研究是否可以使用Varnish来加速REST API。

基本上,我想在很长一段时间内缓存GET个请求。但是,当收到PUT / POST / DELETE请求时,我想解析URL,并根据我找到的信息,我想清除缓存条目。

例如:

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

我可以用VCL实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

我建议用varnish tutorial entry解释清除和禁令。

清除时应该小心,因为不应该让每个人都清除网址。

为了做到这一点,你应该做类似的事情:

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

然后在你的vcl_recv中你必须决定何时清除/禁止:

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您可以找到有关禁止和清除的更多示例