首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ehcache 缓存范例

2012-10-23 
ehcache 缓存实例1.先在src创建ehcache.xml配置文件?2.在context中引入cacheManage相关配置,以便在bean中

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

热点排行