Java caching mechanism

Source: Internet
Author: User

The cache can be divided into two major categories:

First, through the file cache, as the name implies, the file cache refers to the data stored on disk, whether you are in XML format, serialized file dat format or other file format;

Second, the memory cache, that is, to implement a static map of a class, the map for general additions and deletions.

Importjava.util.Collection;Importjava.util.Collections;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.concurrent.ConcurrentHashMap;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Component;ImportNet.blogjava.frankiegao123.log.slf4j.Log;Importnet.blogjava.frankiegao123.log.slf4j.LogFactory;/*** <p>system.config Configuration Cache </p> * *@authorfrankiegao123 * 2010-6-10 pm 02:48:35*/@Component ("Configcache") Public classConfigcacheImplementsConfigservice {Private Final StaticLog log = Logfactory.getlog (Configcache.class); /*** Time recorded when updating the cache*/    Private volatile LongTime = 0L; /*** The latch when the cache is being updated, true indicates that the cache is currently not being updated and is true when the cache is currently being updated*/    Private volatile BooleanUpdategate =true; /*** Cache Container*/    Privatemap<string, sysconfig> cache =NewConcurrenthashmap<string, sysconfig>(); PrivateCommondao Commondao; @Autowired PublicConfigcache (Commondao Commondao) { This. Commondao =Commondao; Log.info ("Initializing Cache ...");        RefreshCache (); time=System.currenttimemillis (); Log.info ("Initialized cache finished, cache size: {}, set cache time to current: {}, Cache timeout: {}ms", Cache.size (), time, configconstant.cache_timeout); }     /*** <p> Get configuration value based on configured key name </p> * *@paramConfigkey *@return     * @authorfrankiegao123 * 2010-6-10 a.m. 11:18:33*/     PublicSysConfig getsysconfig (String configkey) {LongCurrent =System.currenttimemillis (); if(Updategate &&Istimeout (current)) {            synchronized( This) {                if(updategate) {Timeoutsynrefresh (current); }            }        }        returnCache.get (Configkey); }     /*** <p> Update cache. This method needs to be called in the synchronous environment </p> *@paramCurrent *@authorfrankiegao123 * 2010-6-10 a.m. 11:16:30*/    Private voidTimeoutsynrefresh (LongCurrent ) {Updategate=false; Log.info ("Refresh cache start ..., time out: {}, Size: {}, set Updategate to False", (current-time)/1000.0, Cache.size ()); Try{RefreshCache (); time=Current ; Log.info ("Refresh cache finished, size after update: {}, set cache time-to-current: {}", Cache.size (), string.valueof (time)); } Catch(Exception e) {log.error ("Refresh Cache Failed", E); } finally{updategate=true; Log.info ("Refresh Cache finished, set Updategate to True"); }    }     /*** <p> Update cache data </p> *@authorfrankiegao123 * 2010-6-10 a.m. 11:15:55*/    Private voidRefreshCache () {List<SysConfig> configs =Commondao.getsysconfigs ();  for(Iterator<sysconfig> i =configs.iterator (); I.hasnext ();) {SysConfig config=I.next ();        Cache.put (Config.getkey (), config);        } commondao.clear (); SysConfig Config=Cache.get (Sysconfig.test_key); if(config = =NULL) {Log.error ("Refresh cache, cannot find Test_key"); } Else{log.info ("Refresh cache, Find Test_key = [{}]", Config.getvalue ()); }    }     /*** <p> Cache Timeout </p> * *@paramCurrent *@return     * @authorfrankiegao123 * 2010-6-10 a.m. 11:16:12*/    Private BooleanIstimeout (LongCurrent ) {        return(Current-time >=configconstant.cache_timeout); } Collection<SysConfig>Getsysconfigs () {returncollections.unmodifiablecollection (Cache.values ()); }     intGetSize () {returncache.size (); }     LongGetTime () {returnTime ; }     Booleanisupdategate () {returnupdategate; }     voidRefresh () { time= 0L; Log.info ("Refresh:reset Cache time to 0"); Getsysconfig ("None"); Log.info ("Refresh:refresh Cache finished, cache: {0}", String.valueof (time)); }}

Java caching mechanism

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.