First we write two classes, one is cache (caching Class) and another CacheManager (Cache management Class)
1 Cache class
Package Com.yhsx.service.impl;
/** * Created by DHB on 2016/6/24.
* * public class Cache {Private String key;//cache ID Private Object value;//cache data Private long timeout;//update time private Boolean expired;
Whether to terminate public Cache () {super ();
SetTimeout (System.currenttimemillis () + 1000 * 60 * 60 * 24);
Setexpired (FALSE);
Public Cache (String key, Object value, Long TimeOut, Boolean expired) {This.key = key;
This.value = value;
This.timeout = TimeOut;
this.expired = expired;
Public String Getkey () {return key;
Public long GetTimeout () {return timeOut;
Public Object GetValue () {return value;
public void Setkey (string string) {key = string;
public void settimeout (long l) {timeOut = l;
public void SetValue (object) {value = object;
public boolean isexpired () { return expired;
} public void Setexpired (Boolean b) {expired = B;
}
}
2CacheManager
Package Com.yhsx.service.impl;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
/** * Created by guanxin on 2016/6/24.
* * public class CacheManager {//key unique name public static final String strformdict = "Formdictformmain";
/************************************************ Background Cache ********************************************************/ public static final String findallestate= "findallestate";/all real Estate information/******************************************** Background cache end********************************************************/private static HashMap Cachemap = new Hashma
P ();
Single instance construction method private CacheManager () {super (); //Gets the cached public static Boolean Getsimpleflag (String key) of a Boolean value {try{return (Boolean) CACHEMAP.G
ET (key);
}catch (NullPointerException e) {} return false; public static long Getserverstartdt (String key) {try {return (L)ONG) Cachemap.get (key);
catch (Exception ex) {return 0; The cache public synchronized static Boolean Setsimpleflag (String Key,boolean flag) {if (flag &am) of a Boolean value is set
p;& Getsimpleflag (key) {//If True is not allowed to be overwritten return false;
}else{cachemap.put (key, flag);
return true; } public synchronized static Boolean Setsimpleflag (String key,long Serverbegrundt) {if Cachemap.get (key
= = null) {cachemap.put (Key,serverbegrundt);
return true;
}else{return false; }//Get cached.
Synchronous static method private synchronized static Cache GetCache (String key) {return (Cache) cachemap.get (key); }//Determine if there is a cache private synchronized static Boolean Hascache (String key) {return Cachemap.containskey (key
);
//Clear all cached public synchronized static void ClearAll () {cachemap.clear (); //clear a specific cache of a class by traversing the HAAll objects under Shmap to determine whether its key matches the incoming type (public synchronized static void ClearAll (String type) {Iterator i = Cachemap
. EntrySet (). iterator ();
String key;
arraylist<string> arr = new arraylist<string> ();
try {while (I.hasnext ()) {Java.util.Map.Entry Entry = (java.util.Map.Entry) i.next ();
Key = (String) entry.getkey ();
if (Key.startswith (type)) {//If the match is deleted arr.add (key);
for (int k = 0; k < arr.size (); k++) {clearonly (Arr.get (k));
} catch (Exception ex) {ex.printstacktrace ();
}//Clear the specified cache public synchronized static void Clearonly (String key) {cachemap.remove (key);
}//Load cache public synchronized static void Putcache (String key, Cache obj) {cachemap.put (key, obj); //Get cached information public static cache Getcacheinfo (STring key) {if (Hascache (key)) {Cache cache = GetCache (key);
if (cacheexpired (cache)) {//call to determine whether to terminate the method cache.setexpired (true);
Clearonly (key);
return cache;
}else return null; //Load cache information public static void Putcacheinfo (String key, Cache obj, long Dt,boolean expired) {Cache cache
= new Cache ();
Cache.setkey (key); Cache.settimeout (dt + system.currenttimemillis ());
Set how long to update the cache cache.setvalue (obj); cache.setexpired (expired);
Cache default load, termination status is False Cachemap.put (key, cache);
//Overrides Load cache information method public static void Putcacheinfo (String key,cache obj,long dt) {Cache cache = new cache ();
Cache.setkey (key);
Cache.settimeout (Dt+system.currenttimemillis ());
Cache.setvalue (obj);
Cache.setexpired (FALSE);
Cachemap.put (Key,cache); }//Determine if cache terminates public static Boolean CACheexpired (cached Cache) {if (null = cache) {//incoming buffer does not exist return false; Long NOWDT = System.currenttimemillis (); The current number of milliseconds in the system is long Cachedt = Cache.gettimeout (); The number of expired milliseconds in the cache if (Cachedt <= 0| |
CACHEDT>NOWDT) {///expires at less than or equal to zero, or false to False if the expiration time is greater than the current time;
else {//greater than the expiration time is expired return true;
}//Get the size of the cache public static int getcachesize () {return cachemap.size ();
//Gets the size of the specified type public static int getcachesize (String type) {int k = 0;
Iterator i = Cachemap.entryset (). iterator ();
String key;
try {while (I.hasnext ()) {Java.util.Map.Entry Entry = (java.util.Map.Entry) i.next ();
Key = (String) entry.getkey ();
if (Key.indexof (type)!=-1) {///If the match is removed k++; The catch (Exception ex) {Ex.printstacktrace ();
} return k; }//Get all key-value names in cached objects public static arraylist<string> Getcacheallkey () {ArrayList a = new ArrayList ()
;
try {iterator i = Cachemap.entryset (). iterator ();
while (I.hasnext ()) {Java.util.Map.Entry Entry = (java.util.Map.Entry) i.next ();
A.add ((String) Entry.getkey ());
The catch (Exception ex) {} finally {return A; }//Gets the key-value name public static arraylist<string> Getcachelistkey (String type) {Arraylis of the specified type in the cached object
t a = new ArrayList ();
String key;
try {iterator i = Cachemap.entryset (). iterator ();
while (I.hasnext ()) {Java.util.Map.Entry Entry = (java.util.Map.Entry) i.next ();
Key = (String) entry.getkey ();
if (Key.indexof (type)!=-1) {A.add (key); Catch in}}} (Exception ex)
{} finally {return A;
}
}
}3 in the way you need to query the data to add a caching method, in addition to the search, generally only in the Query method, add the cache, the other generally do not add, the following we add the caching method in the method below
The following approach is to query all lists based on IDs
4 Here we have the cache ready, when we visit, we put the cache in memory, when there is cache in memory directly from the memory, if not, in the database, and then put in the cache, this will greatly improve the data access, reduce the pressure of the database
Public list<estate> Getallestatebyuserid (string submituserid) {
string key=cachemanager.findallestate;
List<estate> listestate;
Cache cache= Cachemanager.getcacheinfo (key);
if (null = cache) {
listestate=estatemapper.getallestatebyuserid (submituserid);
cache = new cache ();
Cache.setkey (key);
Cache.setvalue (listestate);
Cachemanager.putcache (key, cache);
else {
listestate= (list<estate>) cache.getvalue ();
}
Return Estatemapper.getallestatebyuserid (submituserid);
return listestate;
}