vertx-http-proxy
vertx-http-proxy copied to clipboard
Improve control of resource caching.
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.
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:
- I don't believe codegen supports
BiPredicate, so it would be better to have our own interface annotated with@VertxGen(and it could extendBiPredicate, to ease the implementation with lambdas) - Minor but I would prefer
skipCacheIfinstead ofnotCacheIf - I'm not sure about the use cases for
notCacheIfHeadersXXXmethods, only the method related to the path makes sense to me at this moment