Package Com.cache;import Java.util.map;import java.util.concurrent.concurrenthashmap;import Org.apache.commons.logging.log;import org.apache.commons.logging.LogFactory; Public classMapcachemanager {PrivateFinalStaticLog log = Logfactory.getlog (Mapcachemanager.class); Private volatile LongUpdateTime =0L;//time to record when updating the cache Private volatileBoolean Updateflag =true;//When the valve is being updated, false indicates that the cache is currently not updated and is true when the cache is currently being updated Private volatile StaticMapcachemanager Mapcacheobject;//Cache Instance Objects Private Staticmap<string, string> cachemap =NewConcurrenthashmap<string, string> ();//Cache Container PrivateMapcachemanager () { This. Loadcache ();//Load CacheUpdateTime = System.currenttimemillis ();//Cache Update Time } /** * use singleton mode to get Cache object instance * * @return*/ Public StaticMapcachemanager getinstance () {if(NULL==mapcacheobject) {synchronized (Mapcachemanager).class) { if(NULL==mapcacheobject) {Mapcacheobject=NewMapcachemanager (); } } } returnMapcacheobject; } /** * Load Cache*/ Private voidLoadcache () { This. Updateflag =true;//is updating /********** data into the CACHEMAP cache **begin * * * * * **/Cachemap.put ("Key1","value1"); Cachemap.put ("Key2","value2"); Cachemap.put ("Key3","Value3"); Cachemap.put ("Key4","Value4"); Cachemap.put ("Key5","Value5"); /********** data into the CACHEMAP cache ***end *******/ This. Updateflag =false;//Update is complete } /** * Return Cached Object * * @return*/ PublicMap<string, string>Getmapcache () {LongCurrentTime =System.currenttimemillis (); if( This. Updateflag) {//pre-cache is being updatedLog.info ("cache is Instance ....."); return NULL; } if( This. Istimeout (currenttime)) {//Reload If the current cache is being updated or the cache is out of timeSynchronized ( This) { This. Reloadcache (); This. UpdateTime =currenttime; } } return This. Cachemap; } PrivateBoolean Istimeout (Longcurrenttime) { return(CurrentTime- This. updatetime) >1000000);//exceeded time limit, timeout } /** * Get cache entry Size * @return*/ Private intgetcachesize () {returncachemap.size (); } /** * Get update time * @return*/ Private LongGetupdatetime () {return This. UpdateTime; } /** * GET update flag * @return*/ PrivateBoolean Getupdateflag () {return This. Updateflag; } /** Re-loading*/ Private voidReloadcache () { This. Cachemap.clear (); This. Loadcache (); }}
Test Example:
Package Com.cache;import Java.util.iterator;import java.util.map;import java.util.set;import Java.util.concurrent.ConcurrentHashMap; Public classCachetest { Public Static voidMain (string[] args) {Mapcachemanager cache=mapcachemanager.getinstance (); Map<string, string> cachemap =NewConcurrenthashmap<string, string>(); Cachemap=Cache.getmapcache (); Set<String>Set=Cachemap.keyset (); Iterator<String> it =Set. iterator (); while(It.hasnext ()) {String key=It.next (); System. out. println (key+"="+cachemap.Get(key)); } }}
HashMap Implementing caching