Google guava cache使用样例
2015-05-12 19:43
330 查看
package com.enniu.vdata.portal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import com.enniu.vdata.biz.monitor.dama.DamaSearchBizImpl;
import com.enniu.vdata.common.mongodb.dama.DamaSearchCond;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
public class DamaSearchServiceImplimplements DamaSearchService {
@Autowired
private DamaSearchBizImpl damaSearchBiz;
public static LoadingCache<Map, List<DamaSearchCond>>cache =
null;
static {
cache = CacheBuilder.newBuilder()
// 设置大小,条目数
.maximumSize(10000)
// 设置时效时间,最后一次被访问
.expireAfterAccess(2, TimeUnit.HOURS)
// 移除缓存的监听器
.removalListener(new RemovalListener<Map, List<DamaSearchCond>>() {
public void onRemoval(RemovalNotification<Map, List<DamaSearchCond>>notification) {
}
})
// 缓存构建的回调
.build(new CacheLoader<Map, List<DamaSearchCond>>() {// 加载缓存
@Override
public List<DamaSearchCond> load(Mapkey)
throws Exception {
List<DamaSearchCond> list =new ArrayList<DamaSearchCond>(1000);
return
list;
}
});
cache.invalidateAll();
}
public List<DamaSearchCond> search(Map<String, Object>content) {
List<DamaSearchCond> results =
null;
try {
if (cache.get(content) !=null &&
cache.get(content).size() > 0) {
results =
cache.get(content);
} else {
cache.cleanUp();
results =
damaSearchBiz.search(content);
cache.put(content,results);
}
} catch (ExecutionException
e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
}
}
这篇文章介绍的不错: http://outofmemory.cn/java/guava/cache/how-to-use-guava-cache
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import com.enniu.vdata.biz.monitor.dama.DamaSearchBizImpl;
import com.enniu.vdata.common.mongodb.dama.DamaSearchCond;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
public class DamaSearchServiceImplimplements DamaSearchService {
@Autowired
private DamaSearchBizImpl damaSearchBiz;
public static LoadingCache<Map, List<DamaSearchCond>>cache =
null;
static {
cache = CacheBuilder.newBuilder()
// 设置大小,条目数
.maximumSize(10000)
// 设置时效时间,最后一次被访问
.expireAfterAccess(2, TimeUnit.HOURS)
// 移除缓存的监听器
.removalListener(new RemovalListener<Map, List<DamaSearchCond>>() {
public void onRemoval(RemovalNotification<Map, List<DamaSearchCond>>notification) {
}
})
// 缓存构建的回调
.build(new CacheLoader<Map, List<DamaSearchCond>>() {// 加载缓存
@Override
public List<DamaSearchCond> load(Mapkey)
throws Exception {
List<DamaSearchCond> list =new ArrayList<DamaSearchCond>(1000);
return
list;
}
});
cache.invalidateAll();
}
public List<DamaSearchCond> search(Map<String, Object>content) {
List<DamaSearchCond> results =
null;
try {
if (cache.get(content) !=null &&
cache.get(content).size() > 0) {
results =
cache.get(content);
} else {
cache.cleanUp();
results =
damaSearchBiz.search(content);
cache.put(content,results);
}
} catch (ExecutionException
e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
}
}
这篇文章介绍的不错: http://outofmemory.cn/java/guava/cache/how-to-use-guava-cache
相关文章推荐
- Google Guava -缓存cache简单使用
- Google Guava 工具集__2__Cache 缓存使用
- google Guava Cache使用
- Google Guava之--cache 使用
- Google Guava之--cache
- Guava Cache使用笔记
- Google Guava Collections 使用介绍
- Guava Cache内存缓存使用实践-定时异步刷新及简单抽象封装
- com.google.common.cache.CacheLoader 本地緩存的使用方法
- 第二章 Google guava cache源码解析1--构建缓存器
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- [Google Guava] 1.1-使用和避免null
- Google Guava Collections 使用介绍
- 使用Google Guava来编写优雅的代码一Guava简介
- java命令执行jar包(里面的main函数,无web.xml文件)的方式(包括依赖其它的jar包),使用Google-Guava Concurrent包里的Service框架,maven工程
- Google Guava Collections 使用介绍
- google guava简单使用
- Guava Cache使用笔记
- Google网络框架Volley的使用,Cache-Control=no-cache时强制缓存的处理