Cache
refresh vs expire
public static void main(String[] args) throws InterruptedException {
LoadingCache<String, String> ca = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofMillis(1000)) // Set to 800 to trigger load()
.refreshAfterWrite(Duration.ofMillis(800))
.build(new CacheLoader<String, String>() {
@Override
public @Nullable String load(String key) throws Exception {
return "load_" + key;
}
@Override
public @Nullable String reload(String key, String oldValue) throws Exception {
return "reload_" + key;
}
});
ca.put("a", "A");
int millisSleep = 300;
System.out.println("ca.get(\"a\") = " + ca.get("a"));
for (int i = 1; i <= 5; i++) {
System.out.println("millis: " + i * millisSleep);
Thread.sleep(millisSleep);
System.out.println("ca.get(\"a\") = " + ca.get("a"));
System.out.println("ca.get(\"a\") = " + ca.get("a"));
}
}Last updated