ehcache 缓存实例
1.先在src创建ehcache.xml配置文件
?2.在context中引入cacheManage相关配置,以便在bean中引用cache,这里我在applicationContext-dao.xml中加入
?3.CacheRandomDaoImpl 中使用
private RandomDao target;private Cache cache;private final static String cacheKey = "randomObjCache"; public void setCache(Cache cache) {this.cache = cache;}public List<RandomObj> getCacheRandomObj(Integer flag) {// TODO Auto-generated method stubElement element = cache.get(cacheKey);if (element == null) {// 当cache中没有数据的时候,从target中取数据,target为实体DAOList<RandomObj> result = target.getCacheRandomObj(flag);System.out.println("set into cache");// cache method resultelement = new Element(cacheKey, (Serializable) result);cache.put(element); //测试CACHE//for(int i=0;i<10000;i++){//element = new Element(cacheKey+i, new RandomObj());//cache.put(element);//}}System.out.println("out cache");return (List<RandomObj>)element.getValue();}?ehcache.xml中的相关属性可参照
http://catastiger.iteye.com/blog/656653