Guava Cache与ConcurrentMap很相似,
guava cache 加载缓存主要有两种方式:
- cacheLoader
- callable callback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| LoadingCache<Key, Value> cache = CacheBuilder.newBuilder()
.build(
new CacheLoader<Key, Value>() {
public Value load(Key key) throws AnyException {
return createValue(key);
}
});
...
try {
return cache.get(key);
} catch (ExecutionException e) {
throw new OtherException(e.getCause());
}
|
这种方式不需要在创建的时候指定load方法,但是需要在get的时候实现一个Callable匿名内部类。
1
2
3
4
5
6
7
8
9
10
11
12
13
| Cache<Key, Value> cache = CacheBuilder.newBuilder()
.build(); // look Ma, no CacheLoader
...
try {
cache.get(key, new Callable<Value>() {
@Override
public Value call() throws AnyException {
return doThingsTheHardWay(key);
}
});
} catch (ExecutionException e) {
throw new OtherException(e.getCause());
}
|