Closed dima-dencep closed 2 weeks ago
If you're really sure you want to bypass server's preferences, you can install a backend interceptor that overwrites response's Cache-Control
.
var client = Methanol.newBuilder()
.backendInterceptor(new Interceptor() {
@Override
public <T> HttpResponse<T> intercept(HttpRequest request, Chain<T> chain) throws IOException, InterruptedException {
return forceCache(chain.forward(request));
}
@Override
public <T> CompletableFuture<HttpResponse<T>> interceptAsync(HttpRequest request, Chain<T> chain) {
return chain.forwardAsync(request).thenApply(this::forceCache);
}
private <T> HttpResponse<T> forceCache(HttpResponse<T> response) {
return ResponseBuilder.newBuilder(response)
.setHeader("Cache-Control", "max-age=5") // Cache for 5 seconds.
.build();
}
})
.cache(HttpCache.newBuilder().cacheOnMemory(100 * 1024 * 1024).build())
.build();
Note that ResponseBuilder
was internal API in 1.7.0
(it will be public in 1.8.0
). So you'll need some --add-exports
trickery if you're using modules.
Let me know how it goes.
Everything works great, thank you so much!
I need to cache the response, however it has
cache-control
which disables cache completely, but I need to cache otherwise everything will take a very long time to complete Response hascache-control
:no-cache, no-store, must-revalidate, private
It would be great if there was an option to turn on the cache if it's turned off or something like that If anything, I'm using version 1.7.0, before that I had a cache built on guava, but it was difficult to work with it