Ehcache的一个完整例子
2015-04-04 16:59
302 查看
Ehcache的一个完整例子
ouyida3的csdn blog
2015.4.4
测试类
测试驱动开发,先写测试类[code]package com.company.mapp.cache; import org.junit.Test; import com.company.component.PageData; public class CacheTest { @Test public final void testGetValue() throws Exception { Cache cache = CacheFactory.getCache(Constants.MAPP_STATIC_PARAM_CFG); cache.setPageData(new PageData()); cache.getValue("MAPP_AMAP_PARAM"); } }
接口
面向接口开发,先有一个接口[code]package com.company.mapp.cache; import java.util.Map; import com.company.component.PageData; /** * 缓存接口 * * @see CacheTest * @author ouyida3 * @since 2015.4.3 */ public interface Cache { public String getValue(String key) throws Exception; public Map<String, String> getMap(String key) throws Exception; public Cache setPageData(PageData pd); }
工厂类
[code]package com.company.mapp.cache; /** * 缓存工厂 * 生产各张数据表的缓存 * * @author ouyida3 * @since 2015.4.4 */ public class CacheFactory { public static Cache getCache(String name){ if (Constants.MAPP_STATIC_PARAM_CFG.equals(name)) return new StaticParamCfgCache(name); else if (Constants.MAPP_S_STATIC.equals(name)) return new SStaticCache(name); return null; } }
常量
[code]package com.company.mapp.cache; /** * 缓存常量类 * * @author ouyida3 * @since 2015.4.4 */ public class Constants { public static final String MAPP_STATIC_PARAM_CFG = "MAPP_STATIC_PARAM_CFG"; public static final String MAPP_S_STATIC = "MAPP_S_STATIC"; }
抽象类
每个缓存都有共同的方法,所以必须得有抽象类放共同方法[code]package com.company.mapp.cache; import java.util.Map; import com.company.component.PageData; import com.company.mapp.bean.FSBean; /** * 缓存抽象类 * 每个数据表的缓存类的重复的方法在这里实现 * * @author ouyida3 * @since 2015.4.4 */ public abstract class AbstractCache implements Cache { protected String cacheName; protected String eparchyCode; protected PageData pd; protected Cache setEparchyCode(String eparchyCode) { this.eparchyCode = eparchyCode; return this; } protected AbstractCache(String name) { this.cacheName = name; } @Override public Cache setPageData(PageData pd) { this.pd = pd; try { setEparchyCode(FSBean.getEparchyCode(pd)); } catch (Exception e) { e.printStackTrace(); } return this; } @Override public String getValue(String key) throws Exception { return null; } @Override public Map<String, String> getMap(String key) throws Exception { return null; } }
其中一张数据表
表里就两个字段最重要,很简单,就是根据key取value[code]package com.company.mapp.cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; /** * 数据表MAPP_STATIC_PARAM_CFG缓存 * * @author ouyida3 * @since 2015.4.3 */ public class StaticParamCfgCache extends AbstractCache { protected StaticParamCfgCache(String name) { super(name); } public String getValue(String key) throws Exception { net.sf.ehcache.Cache cache = CacheManager.getInstance().getCache(cacheName); String cacheKey = key + "_" + eparchyCode; if (cache != null) { Element element = cache.get(cacheKey); if (element != null) return (String) element.getValue(); } CacheDao dao = new CacheDao(); String value = dao.queryStaticParamCfg(cacheName, key, pd); if (cache != null) { cache.put(new Element(cacheKey, value)); } return value; } }
其中另一张数据表
比刚才复杂一点,有三个重要字段,根据1、2取到3,因此我设计为先根据1取到2和3的map,然后再根据2取3[code]package com.company.mapp.cache; import java.util.HashMap; import java.util.Map; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import com.company.appframework.data.DataMap; import com.company.appframework.data.IDataset; /** * 数据表MAPP_S_STATIC缓存 * * @author ouyida3 * @since 2015.4.4 */ public class SStaticCache extends AbstractCache { public SStaticCache(String name) { super(name); } @SuppressWarnings("unchecked") public Map<String, String> getMap(String key) throws Exception { net.sf.ehcache.Cache cache = CacheManager.getInstance().getCache(cacheName); String cacheKey = key + "_" + eparchyCode; if (cache != null) { Element element = cache.get(cacheKey); if (element != null) return (Map<String, String>) element.getValue(); } CacheDao dao = new CacheDao(); IDataset dataSet = dao.querySStatic(cacheName, key, pd); HashMap<String, String> value = new HashMap<String, String>(); for (Object dataMap : dataSet) { value.put((String)((DataMap)dataMap).get("DATA_ID"), (String)((DataMap)dataMap).get("DATA_NAME")); } if (cache != null) { cache.put(new Element(cacheKey, value)); } return value; } }
工具类
最后加的,同事认为这样调用更方便,我试了一下,确实是。[code]package com.company.mapp.cache; import java.util.Map; import com.company.component.PageData; /** * 缓存工具类 * 外部直接调用该类的方法,隐藏整个Cache框架的实现与调用细节 * * @author ouyida3 * @since 2015.4.4 */ public class CacheUtils { public static String getStaticValue(String dataId, String typeId, PageData pd) throws Exception { Cache cache = CacheFactory.getCache(Constants.MAPP_S_STATIC); cache.setPageData(pd); Map<String, String> map = cache.getMap(typeId); String dataName = (String)map.get(dataId); return dataName; } }
ouyida3的csdn blog
2015.4.4
相关文章推荐
- 使用Hibernate的一个完整例子
- 使用Hibernate的一个完整例子
- 一个关于把JS打包进DLL里,然后调用的完整例子
- VS2005配置ICE及写ICE的一个完整的例子
- VS2005写ICE的一个完整的例子
- 重温Struts,完整的一个简单例子
- 一个完整的AjaxPro例子
- 一个完整的hibernate的one-to-many的例子
- 以下是一个完整的关闭excel进程的例子
- Struts 国际化的一个完整例子(转)
- 使用Hibernate的一个完整例子
- 一个完整的hibernate的one-to-many的例子
- 使用Hibernate的一个完整例子
- qt4:一个简单而完整的例子
- WF工作流的一个完整例子
- 一个完整的DataGrid自定义分页例子
- 一个使用JdbcTemplate增加一组数据的完整例子
- 一个完整的hibernate的one-to-many的例子
- SAS Logistic回归:一个完整例子
- 一个完整的MapXtreme例子