java-storing data in memory when a project is loaded

Source: Internet
Author: User

1. Get the database data and store it in the Map object

Package Com.demo;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationevent;import Org.springframework.context.applicationlistener;import Org.springframework.stereotype.service;import Java.util.hashmap;import Java.util.list;import java.util.Map;@ Service Public classRegionserviceimpl {//implements Regionservice {@Autowired userrepository userrepository;  Public StaticMap<integer,string> Regionmap =NewHashmap<integer, string>();  PublicMap<integer,string>initregion () {if(Regionmap.isempty ()) {List<Users> regionlist =Userrepository.findall ();  for(Users r:regionlist) {regionmap.put (R.getid (), R.getname ()+","+r.getsex ()); }        }        returnRegionmap; }     PublicMap<integer,string>Getallregionmap () {returnGetregionmap (); }     Public StaticMap<integer, string>Getregionmap () {returnRegionmap; }     Public Static voidSetregionmap (Map<integer, string>Regionmap) {Regionserviceimpl.regionmap=Regionmap; }}

2. Load method at project startup

Package Com.demo;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.commandlinerunner;import org.springframework.stereotype.Component; @Component  publicclass  Mycommandlinerunner implements Commandlinerunner {    @Autowired    Regionserviceimpl Regionservice; 
/**
* Thread Start
*/

Public void start () {
New Thread (new  Xiancheng ()). Start ();        System. out. println (0);    }    @Override    publicvoid  run (String ... var1) throws exception{        Regionservice.initregion ();        Start ();        System. out. println (" initialization complete ");}    }

3. Use thread settings to refresh data periodically

Package Com.demo; Public classXiancheng implements runnable{ Public voidrun () {Regionserviceimpl Regionservice=NewRegionserviceimpl ();  while(true) {regionservice.initregion (); Try{System. out. println (111); Thread.Sleep (Ten* -); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}

java-storing data in memory when a project is loaded

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.