i'm a little stuck how to handle "NOT-MODIFIED" answers from a remote resource. Using Spring WebClient i fetch a remote resource (json) and want to prevent unneeded calls or better: working with conditional requests.
Details: Spring Boot 3 MVC WebApp with Thymeleaf FrontEnd, using WebClient from Webflux to retrieve external content (blocking mode) and putting this to the frontend as an Object to display it within the template.
What happens: I call the remote site and on Http.OK i will store the "etag" in a local map to the called url, also returning the Body (Mono) - normal case. On the next call i'll call the remote with "If-None-Match" header, using the etag as value (if exists for this url). On 304 Status, i'll get an empty body - as expected.
Question: Where/How do i store/cache the remote content and return it, if i get the 304 answer ?
Before i used Caffeine-Caching with "expireAfterWrite" to the objects to expire the cache after some time, than retrieving the external content. Now i want to update the content only if needed, not just after a cache is expired.
I'm open to any answer, so if you suggest using simply the expiring-cache - i'm fine. But my favorite solution is to use the possibilities given (conditional requests).
Here's some code i use:
public Mono<PageObject> getPageObject(final String callingUri) {
return webClient.get().uri(callingUri).ifNoneMatch(retrieveEtag(callingUri)).exchangeToMono(clientResponse -> {
if (clientResponse.statusCode().isSameCodeAs(HttpStatus.NOT_MODIFIED)) {
return Mono.empty();
}
clientResponse.headers().header("etag").stream().findFirst().ifPresent(etagString -> storeEtag(callingUri, etagString));
return clientResponse.bodyToMono(PageObject.class);
});
}
What if a local cache is expires and does not contain the content but remote-site still returns 304 - should I check manually if the content is still cached and force retrieving the content regardless of etag and rewrite it to the cache ?
Thanks for your ideas and suggestions.