HashMap Implementing caching

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.