Last active
August 23, 2018 05:58
-
-
Save seveniu/85aa6c3f81b8f8d6997c2d922b01e34a to your computer and use it in GitHub Desktop.
guava cache test refresh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void testRefresh() throws ExecutionException, InterruptedException { | |
LogTime logTime = new LogTime(); | |
AtomicInteger value = new AtomicInteger(); | |
LoadingCache<String, String> cb = CacheBuilder.newBuilder() | |
.maximumSize(1000) | |
.refreshAfterWrite(6, TimeUnit.SECONDS) | |
.removalListener((RemovalListener<String, String>) notification -> { | |
logTime.log("remove key : " + notification.getKey() + ", value : " + notification.getValue() + ", cause " + notification.getCause()); | |
}) | |
.build(new CacheLoader<String, String>() { | |
public String load(String key) { // no checked exception | |
String newValue = String.valueOf(value.getAndIncrement()); | |
logTime.log("load "); | |
try { | |
TimeUnit.SECONDS.sleep(1); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
logTime.log("load end "); | |
return newValue; | |
} | |
@Override | |
public ListenableFuture<String> reload(String key, String oldValue) throws Exception { | |
// asynchronous! | |
logTime.log("refresh "); | |
ExecutorService executor = Executors.newFixedThreadPool(1); | |
ListenableFutureTask<String> task = ListenableFutureTask.create(() -> load(key)); | |
executor.execute(task); | |
return task; | |
} | |
}); | |
logTime.start(); | |
// ------ | |
logTime.log("value : " + cb.get("name")); | |
TimeUnit.SECONDS.sleep(10); | |
logTime.log("value : " + cb.get("name")); | |
TimeUnit.SECONDS.sleep(2); | |
logTime.log("value : " + cb.get("name")); | |
// logTime.log(" test refresh " + "value : " + cb.get("name")); | |
// TimeUnit.SECONDS.sleep(2); | |
// logTime.log(" test refresh result : " + "value : " + cb.get("name")); | |
// ------ | |
} | |
public static void test() throws ExecutionException, InterruptedException { | |
LogTime logTime = new LogTime(); | |
AtomicInteger value = new AtomicInteger(); | |
LoadingCache<String, String> cb = CacheBuilder.newBuilder() | |
.maximumSize(1000) | |
.refreshAfterWrite(6, TimeUnit.SECONDS) | |
.expireAfterAccess(10, TimeUnit.SECONDS) | |
.removalListener((RemovalListener<String, String>) notification -> { | |
logTime.log("remove key : " + notification.getKey() + ", value : " + notification.getValue() + ", cause " + notification.getCause()); | |
}) | |
.build(new CacheLoader<String, String>() { | |
public String load(String key) { // no checked exception | |
String newValue = String.valueOf(value.getAndIncrement()); | |
return newValue; | |
} | |
@Override | |
public ListenableFuture<String> reload(String key, String oldValue) throws Exception { | |
// asynchronous! | |
logTime.log("refresh "); | |
ExecutorService executor = Executors.newFixedThreadPool(1); | |
ListenableFutureTask<String> task = ListenableFutureTask.create(() -> load(key)); | |
executor.execute(task); | |
return task; | |
} | |
}); | |
logTime.start(); | |
// 测试 expire 和 refresh 的优先级 | |
logTime.log("value : " + cb.get("name")); | |
TimeUnit.SECONDS.sleep(7); | |
cb.cleanUp(); | |
logTime.log("size : " + cb.size()); | |
// logTime.log("value : " + cb.get("name")); | |
TimeUnit.SECONDS.sleep(7); | |
cb.cleanUp(); | |
logTime.log("size : " + cb.size()); | |
} | |
private static class LogTime { | |
long start = System.currentTimeMillis(); | |
private void start() { | |
this.start = System.currentTimeMillis(); | |
} | |
private void log(String s) { | |
System.out.println((int) (System.currentTimeMillis() - start) / 1000 + "s -- " + Thread.currentThread().getName() + " ---- " + s); | |
} | |
} | |
public static void main(String[] args) throws ExecutionException, InterruptedException { | |
// testRefresh(); | |
test(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment