【发布时间】:2020-09-10 18:30:02
【问题描述】:
我正在使用 Guava 来处理我的 Web 应用程序中的缓存;我想每 10 分钟自动刷新一次缓存中的现有元素。
这是我的 sn-p 代码:
private Cache<String, Integer> cache = CacheBuilder.newBuilder.build();
//my main method
public Integer load(String key){
Integer value = cache.getIfPresent(key)
if(value == null){
value = getKeyFromServer(key);
//insert in my cache
cache.put(key, value);
}
return value;
}
我想增强上面的代码,以便刷新缓存映射中收集的元素,如下所示:
//1. iterate over cache map
for(e in cache){
//2. get new element value from the server
value = getKeyFromServer(e.getKey());
//3. update the cache
cache.put(key, value);
}
【问题讨论】:
标签: java spring-boot guava