-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsynRefresh.java
More file actions
70 lines (57 loc) · 1.79 KB
/
Copy pathAsynRefresh.java
File metadata and controls
70 lines (57 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
public class AsynRefresh {
public static void main(String[] args) {
//
final ExecutorService executor= Executors.newFixedThreadPool(1);
CacheLoader<String,String> loader;
loader= new CacheLoader<String,String>(){
@Override
public String load(String key) throws Exception {
//
return key+" _load_make";
}
// 重写reload方法 -- 实现异步刷新
@Override
public ListenableFuture<String> reload(final String key, String oldValue) throws Exception {
//
//创建一个futureTask
ListenableFutureTask<String> task=ListenableFutureTask.create(new Callable<String>() {
public String call() throws Exception {
//执行异步刷新操作
String newValue=getNewValueFromDB(key);
return newValue;
}
private String getNewValueFromDB(String key) {
//
return "newValue";
}
});
executor.execute(task);
return task;
}
};
//
LoadingCache<String, String> cache = CacheBuilder.newBuilder().refreshAfterWrite(1, TimeUnit.SECONDS).build(loader);
try {
for (int i = 0; i < 10; i++) {
String value = cache.getUnchecked("hello");
System.out.println(" 取出的值是 "+value);
Thread.sleep(1000);
}
//executor.
executor.shutdown();
}catch(Exception e) {
e.printStackTrace();
}
}
}