缓存操作
public static void main(String[] args) throws Exception {
LoadingCache<String,String> loadingCache = CacheBuilder
.newBuilder()
.build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return slowMethod(key);
}
});
while(true){
System.out.println(loadingCache.get("a"));
Thread.sleep(1000);
System.out.println("睡眠 1 秒 ");
}
}
static String slowMethod(String key) throws Exception {
Thread.sleep(5000);
System.out.println("睡眠 5 秒");
return key + ".result";
}