一、使用
1)简单初始化:
LoadingCache<Long, Long> loadCache = CacheBuilder.newBuilder()
.expireAfterWrite(6, TimeUnit.SECONDS)
.maximumSize(100)
.build(new CacheLoader<Long, Long>() {
@Override
public Long load(Long userId) throws Exception {
return getNum(userId);
}
});
2)异步更新的初始化:
LoadingCache<Long, Long> loadCache = CacheBuilder.newBuilder()
.expireAfterWrite(6, TimeUnit.SECONDS)
.refreshAfterWrite(2, TimeUnit.SECONDS)
.maximumSize(100)
.build(CacheLoader.asyncReloading(new CacheLoader<Long, Long>() {
@Override
public Long load(Long userId) throws Exception {
return getNum(userId);