I want to invalidate a request in Varnish from a java backend with HTTP headers. till now I am able to achieve cache arequest which does not have query param in it. Let's say I have a request: localhost:8090/api/data/abc?fields=test,test1 what headers do I need to set in this case for varnish to cache it.
I am able to ban a request which is like : localhost:8090/api/data/abc by using this headers for this request: request: localhost:8090/api/data/abc headers: responseHeaders.set("x-host", "localhost:8080"); responseHeaders.set("x-url", "/api/data/abc");
VCL code
You can use the VCL example from the banning tutorial on the Varnish Developer Portal, which looks like this:
Removing objects that match a specific query string pattern
Let's say the cache contains 4 objects identified by the following URL:
/?a=1
/?a=2
/?a=3
/?a=4
Imagine we want to remove the first 3. Assuming the value of the
a
query string parameter is a number, we can create the following HTTP request:As a result
/?a=1
,/?a=2
and/?a=3
will be removed from the cache, whereas/?a=4
is still stored in the cache.Conclusion
The query string parameter is part of the URL. As long as you can match it in a regular expression, you can remove specific objects from the cache.