eclipse-vertx / vertx-http-proxy

vertx http proxy
Eclipse Public License 2.0
55 stars 36 forks source link

Improve control of resource caching. #69

Open tsegismont opened 6 months ago

tsegismont commented 6 months ago

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.

wzy1935 commented 2 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");
tsegismont commented 2 months ago

Thanks for sharing findings @wzy1935

Here are a few comments: