I have several GET methods in different classes. Planning for caching implementations for all of these.
The logic will be something like this.
@GET
@Path("/route1") {
String cacheKey = routePathWithParams;
if (cache.get(cacheKey) != null) {
return cache.get(cacheKey);
} else {
// call servcie and get response
cache.put(cacheKey, response)
return response;
}
}
I do not want to put this logic in all GET methods. Which place is best suited.
Can I use filters?
public class CacheFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext req) throws IOException {
if (cache.get(cacheKey) != null) {
} else {
}
}
public void filter(ContainerRequestContext req, ContainerResponseContext res) {
cache.put(cacheKey, response)
}
}
Or is there a better way to do this. Basically this question is not about client-side caching or sending cache headers to the user.
I am mainly trying to cache internal service calls in route methods.
source
share