forked from morethink/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
53 lines (49 loc) · 1.55 KB
/
Demo.java
File metadata and controls
53 lines (49 loc) · 1.55 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
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
/**
* @author 李文浩
* @date 2018/12/28
*/
public class Demo {
/**
* CacheLoader
*/
public void loadingCache() {
LoadingCache<String, String> graphs = CacheBuilder.newBuilder()
.maximumSize(1000).build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
System.out.println("key:" + key);
if ("key".equals(key)) {
return "key return result";
} else {
return "get-if-absent-compute";
}
}
});
String resultVal = null;
try {
resultVal = graphs.get("key");
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println(resultVal);
}
/**
* Callable
*/
public void callablex() throws ExecutionException {
Cache<String, String> cache = CacheBuilder.newBuilder()
.maximumSize(1000).build();
String result = cache.get("key", new Callable<String>() {
public String call() {
return "result";
}
});
System.out.println(result);
}
}