我的实现非常简单,但它没有做应该做的事情。我正在使用这个缓存库:https ://github.com/ben-manes/caffeine
当我部署应用程序时:
Calls Controller Endpoint -> First time loads data to cache (works fine) -> Wait 10 seconds -> refresh data to cache -> wait 10 seconds -> ... over and over
第一次加载完美。问题是 10 秒后,数据的重新加载没有运行。
控制器
MyData myData = MyData.getInstance();
Map<String, List<String>> tableOne = myData.cache.get("tableOne");
MyData 类
public LoadingCache<String, Map<String, List<String>>> cache;
public static MyData getInstance() {
if (instance == null) {
synchronized (MyData.class) {
if (instance == null) {
instance = new MyData();
}
}
}
return instance;
}
private MyData() {
this.cache = Caffeine.newBuilder()
.refreshAfterWrite(10, TimeUnit.SECONDS)
.build(key -> MyData.getInstance().loadData(key));
}
private Map<String, List<String>> loadData(String key) {
// Loads Data. This is only called once and never again!
}
任何线索为什么refreshAfterWrite
不每 10 秒运行一次?
谢谢