读书人

ehcache 缓存范例

发布时间: 2012-10-23 12:12:22 作者: rapoo

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

读书人网 >软件架构设计

热点推荐