Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve control of resource caching. #69

Open
tsegismont opened this issue Mar 15, 2024 · 2 comments
Open

Improve control of resource caching. #69

tsegismont opened this issue Mar 15, 2024 · 2 comments
Labels
enhancement New feature or request gsoc2024

Comments

@tsegismont
Copy link
Contributor

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.

@tsegismont tsegismont added enhancement New feature or request gsoc2024 labels Mar 15, 2024
@wzy1935
Copy link
Contributor

wzy1935 commented Jul 22, 2024

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
Copy link
Contributor Author

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 extend BiPredicate, to ease the implementation with lambdas)
  • Minor but I would prefer skipCacheIf instead of notCacheIf
  • I'm not sure about the use cases for notCacheIfHeadersXXX methods, only the method related to the path makes sense to me at this moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gsoc2024
Projects
None yet
Development

No branches or pull requests

2 participants