我的实现非常简单,但它并没有做应该做的事情。我正在使用这个缓存库: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秒运行一次吗?
谢谢
发布于 2020-05-20 21:12:05
自上次写入后,在条目过期超过该阈值后,将在读取时触发刷新。
自动刷新是在出现对条目的第一个过时请求时执行的。触发刷新的请求将对CacheLoader.reload进行异步调用,并立即返回旧值。
然后,可以将其与过期相结合,以便重新加载流行的条目,而不活动的条目将过期。在这种情况下,刷新避免了调用方定期阻塞以重新加载条目的代价,因为这个延迟是隐藏的,同时也确保它不会变得太陈旧。
缓存的定期重新加载最好由您自己的线程来完成。例如,使用ScheduledExecutorService可以批量重新加载所有内容。
https://stackoverflow.com/questions/61911986
复制相似问题