Open tsegismont opened 6 months ago
Possible custom cache control if using interceptors:
public interface CacheFilterInterceptor {
// the most general one - sometimes cache decision depends on both requests and responses
static ProxyInterceptor cacheIf(BiPredicate<ProxyRequest, ProxyResponse> condition) { // or Predicate<ProxyContext> ?
return null;
}
static ProxyInterceptor notCacheIf(BiPredicate<ProxyRequest, ProxyResponse> condition) {
return null;
}
// some "shortcuts": (necessary?)
static ProxyInterceptor cacheIfHeadersContainOne(List<CharSequence> keys) {
return null;
}
static ProxyInterceptor cacheIfHeadersContainOne(MultiMap headers) {
return null;
}
static ProxyInterceptor cacheIfHeadersContainAll(List<CharSequence> keys) {
return null;
}
static ProxyInterceptor cacheIfHeadersContainAll(MultiMap headers) {
return null;
}
static ProxyInterceptor notCacheIfHeadersContainOne(List<CharSequence> keys) {
return null;
}
static ProxyInterceptor notCacheIfHeadersContainOne(MultiMap headers) {
return null;
}
static ProxyInterceptor notCacheIfHeadersContainAll(List<CharSequence> keys) {
return null;
}
static ProxyInterceptor notCacheIfHeadersContainAll(MultiMap headers) {
return null;
}
static ProxyInterceptor cacheIfPathStartsWith(List<String> paths) {
return null;
}
static ProxyInterceptor notCacheIfPathStartsWith(List<String> paths) {
return null;
}
}
Usage:
HttpClient client = vertx.createHttpClient();
HttpProxy proxy = HttpProxy.reverseProxy(new ProxyOptions().setCacheOptions(new CacheOptions()), client)
.addInterceptor(CacheFilterInterceptor.cacheIf((req, resp) -> {
return resp.getStatusCode() == 200; // only cache responses if status code is 200
}));
proxy.origin(8081, "localhost");
Thanks for sharing findings @wzy1935
Here are a few comments:
BiPredicate
, so it would be better to have our own interface annotated with @VertxGen
(and it could extend BiPredicate
, to ease the implementation with lambdas)skipCacheIf
instead of notCacheIf
notCacheIfHeadersXXX
methods, only the method related to the path makes sense to me at this moment
We should let users decided (e.g. by providing a set of paths in configuration) which resources shall be cached instead of enabling caching for all resources.
Besides, it should be possible to decide in an interceptor whether a resource is a candidate for caching.