【发布时间】:2019-12-09 14:56:54
【问题描述】:
我有一个弹簧缓存需求:
我需要向服务器请求获取一些数据并将结果存储在 Spring 缓存中。每次相同的请求都会给我不同的结果,所以我决定使用@cachePut,这样每次我可以进入我的函数并更新缓存。
@CachePut(value="mycache", key="#url")
public String getData(String url){
try{
// get the data from server
// update the cache
// return data
} catch(){
// return data from cache
}
}
现在有一个转折点。如果服务器关闭并且我无法得到响应;我想要缓存中的数据(存储在以前的请求中)。
如果我使用@Cacheable,我无法获取更新的数据。这样做的干净方法是什么?类似于捕获异常并从缓存中返回数据。
【问题讨论】:
标签: spring spring-boot spring-cache