A complete example of Ehcache
Test class
Test-driven development, first write test class
package com.company.mapp.cache;import org.junit.Test;import com.company.component.PageData;publicclass CacheTest { @Test publicfinalvoidtestGetValuethrows Exception { Cache cache = CacheFactory.getCache(Constants.MAPP_STATIC_PARAM_CFG); cache.setPageData(new PageData()); cache.getValue("MAPP_AMAP_PARAM"); }}
Interface
interface-oriented development with an interface first
PackageCom.company.mapp.cache;ImportJava.util.Map;ImportCom.company.component.PageData;/** * Cache interface * * @see cachetest * @author ouyida3 * @since 2015.4.3 */ Public interface Cache { PublicStringGetValue(String key)throwsException; PublicMap<string, string>Getmap(String key)throwsException; PublicCacheSetpagedata(Pagedata PD);}
Factory class
PackageCom.company.mapp.cache;/** * Cache factory * Production of each data table cache * * @author ouyida3 * @since 2015.4.4 * * Public class cachefactory { Public StaticCacheGetCache(String name) {if(Constants.MAPP_STATIC_PARAM_CFG.equals (name))return NewStaticparamcfgcache (name);Else if(Constants.MAPP_S_STATIC.equals (name))return NewSstaticcache (name);return NULL; }}
Constant
package com.company.mapp.cache;/** * 缓存常量类 * * @author ouyida3 * @since 2015.4.4 */publicclass Constants { publicstaticfinal"MAPP_STATIC_PARAM_CFG"; publicstaticfinal"MAPP_S_STATIC";}
Abstract class
Each cache has a common approach, so you have to have an abstract class to put together a common approach
PackageCom.company.mapp.cache;ImportJava.util.Map;ImportCom.company.component.PageData;ImportCom.company.mapp.bean.FSBean;/** * Cache abstract class * A repeating method of caching classes per data table is implemented here * * @author ouyida3 * @since 2015.4.4 * * Public Abstract class Abstractcache implements Cache { protectedString CacheName;protectedString Eparchycode;protectedPagedata PD;protectedCacheSeteparchycode(String Eparchycode) { This. Eparchycode = Eparchycode;return This; }protected Abstractcache(String name) { This. cachename = name; }@Override PublicCacheSetpagedata(Pagedata PD) { This. PD = PD;Try{Seteparchycode (Fsbean.geteparchycode (PD)); }Catch(Exception e) {E.printstacktrace (); }return This; }@Override PublicStringGetValue(String key)throwsException {return NULL; }@Override PublicMap<string, string>Getmap(String key)throwsException {return NULL; }}
One of the data sheets
Table on the two fields the most important, very simple, is based on key to take value
PackageCom.company.mapp.cache;ImportNet.sf.ehcache.CacheManager;ImportNet.sf.ehcache.Element;/** * Data Table mapp_static_param_cfg Cache * * @author ouyida3 * @since 2015.4.3 * * Public class staticparamcfgcache extends abstractcache { protected Staticparamcfgcache(String name) {Super(name); } PublicStringGetValue(String key)throwsException {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 =NewCachedao (); String value = dao.querystaticparamcfg (CacheName, Key, PD);if(Cache! =NULL) {Cache.put (NewElement (CacheKey, value)); }returnValue }}
One of the other data sheets
More complicated than just a little bit, there are three important fields, according to 1, 2 to 3, so I designed to first based on 1 to the map of 2 and 3, and then based on 2 to take 3
PackageCom.company.mapp.cache;ImportJava.util.HashMap;ImportJava.util.Map;ImportNet.sf.ehcache.CacheManager;ImportNet.sf.ehcache.Element;ImportCom.company.appframework.data.DataMap;ImportCom.company.appframework.data.IDataset;/** * Data Table mapp_s_static Cache * * @author ouyida3 * @since 2015.4.4 * * Public class sstaticcache extends abstractcache { Public Sstaticcache(String name) {Super(name); }@SuppressWarnings("Unchecked") PublicMap<string, string>Getmap(String key)throwsException {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 =NewCachedao (); Idataset DataSet = dao.querysstatic (CacheName, Key, PD); hashmap<string, string> value =NewHashmap<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 (NewElement (CacheKey, value)); }returnValue }}
Tool class
Finally add, colleagues think this call more convenient, I tried, is indeed.
PackageCom.company.mapp.cache;ImportJava.util.Map;ImportCom.company.component.PageData;/** * Cache Tool class * External calls the method of the class directly, hiding the implementation and invocation details of the entire cache framework * * @author ouyida3 * @since 2015.4.4 * * Public class cacheutils { Public StaticStringGetstaticvalue(String dataid, String typeId, Pagedata PD)throwsException {Cache cache = Cachefactory.getcache (constants.mapp_s_static); Cache.setpagedata (PD); map<string, string> map = Cache.getmap (typeId); String dataname = (string) map.get (Dataid);returnDataname; }}
Ouyida3 's CSDN Blog
2015.4.4
A complete example of Ehcache