4
我在各種類中有幾個GET方法。計劃爲所有這些引入緩存。澤西島的緩存方法邏輯
邏輯會是這樣的。
@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;
}
}
我不想把這個邏輯放在所有的GET方法中。哪個是最好的地方掛。
我可以使用過濾器嗎?
public class CacheFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext req) throws IOException {
// frame cacheKey from url and params
if (cache.get(cacheKey) != null) {
// return response from here. How to do it ???
} else {
//forward the request How to do it ???
}
}
public void filter(ContainerRequestContext req, ContainerResponseContext res) {
// frame cachekey and put response
cache.put(cacheKey, response)
}
}
或者有沒有更好的方法來做到這一點。 基本上這個問題不是關於客戶端緩存或發送緩存頭給用戶。
基本上我試圖在路由方法內緩存內部服務調用。
你怎麼打算緩存失效? –
@CássioMazzochiMolin將在密鑰上設置緩存過期時間。所以一旦密鑰過期,我會再次調用內部服務並將其緩存爲固定的時間間隔。 – sanjeev