Because the project needs more data query, the number is large, so to use the cache, in the case of non-reference Redis, only one speed of ascension
1. Cache class
public class Cache<e> {
Private String key;//Cache ID, storing values to HASHMAP based on ID
Private E value;//Cache data, object entities that require caching
Private Long timeout;//Update time
private Boolean expired; Is expired
Public Cache () {
Super ();
}
Public Cache (String key, E value, Long TimeOut, Boolean expired) {
This.key = key;
This.value = value;
This.timeout = Timeout+system.currenttimemillis ();
this.expired = expired;
}
Public String GetKey () {
Return key;
}
Public long gettimeout () {
return timeOut;
}
Public E GetValue () {
return value;
}
public void Setkey (string string) {
key = string;
}
public void SetTimeOut (long l) {
TimeOut = l;
}
public void SetValue (E object) {
Value = object;
}
public boolean isexpired () {
return expired;
}
public void setexpired (Boolean b) {
expired = b;
}
}
2, CacheManager class
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
/**
* Created by admin on 2018/1/2.
*/
public class CacheManager {
private static HashMap Cachemap = new HashMap ();
Single Instance construction method
Private CacheManager () {
Super ();
}
Get the cache of a Boolean value
public static Boolean Getsimpleflag (String key) {
try{
Return (Boolean) cachemap.get (key);
}catch (NullPointerException e) {
return false;
}
}
public static long Getserverstartdt (String key) {
try {
Return (Long) cachemap.get (key);
} catch (Exception ex) {
return 0;
}
}
Set the cache for a Boolean value
Public synchronized Static Boolean Setsimpleflag (String Key,boolean flag) {
if (flag && 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 methods
Private synchronized static Cache GetCache (String key) {
Return (Cache) cachemap.get (key);
}
Determine if a cache exists
Private synchronized Static Boolean Hascache (String key) {
return Cachemap.containskey (key);
}
Clear all Caches
Public synchronized static void ClearAll () {
Cachemap.clear ();
}
Clears a particular type of cache by traversing all objects under the HashMap to determine if 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 ();
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 dropped
Arr.add (key);
}
}
for (int k = 0; k < arr.size (); k++) {
Clearonly (Arr.get (k));
}
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
Clears 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 cache 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);
Cachemap.remove (key);
return null;
}
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 after the cache is updated
Cache.setvalue (obj);
cache.setexpired (expired); The cache is loaded by default and the termination status is False
Cachemap.put (key, cache);
}
Override 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 the cache is terminated
public static Boolean cacheexpired (cache cache) {
if (null = = cache) {//incoming caches do not exist
return false;
}
Long NOWDT = System.currenttimemillis (); The current number of milliseconds in the system
Long Cachedt = Cache.gettimeout (); Number of expired milliseconds in cache
if (Cachedt <= 0| | CACHEDT>NOWDT) {//The expiration time is less than or equal to zero, or false if the expiration time is greater than the current time
return false;
} else {//greater than expiration time expires
return true;
}
}
Get the size in 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 dropped
k++;
}
}
} catch (Exception ex) {
Ex.printstacktrace ();
}
return k;
}
Gets all the key value names in the cache object
public static ArrayList 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 ());
}
} catch (Exception ex) {} finally {
return A;
}
}
Gets the key value name of the specified type in the cache object
public static ArrayList Getcachelistkey (String type) {
ArrayList 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 (Exception ex) {} finally {
return A;
}
}
}
3. Call
@Override
Public Dcjqrfbview Getmap (string qlinkid, String country, string online, String devtype, String devtype1, String beautify, String adminqlinkid) throws Exception {
Dcjqrfbview viewrespons=new Dcjqrfbview ();
String cache_key=new StringBuilder (). Append (Qlinkid). Append (Country). Append (online). Append (Devtype). Append ( DEVTYPE1). Append (Beautify). Append (Adminqlinkid). toString ();
cache<dcjqrfbview> C = cachemanager.getcacheinfo (Cache_key);
if (c!=null&&!c.isexpired ()) {
return C.getvalue ();
}else{
c = new Cache (cache_key,viewrespons,10*60*1000,false);
}
int positionfialdnum=0;
int noactivitynum=0;
List<dcjqrfb> alldata=new arraylist<dcjqrfb> ();
List<dcjqrfb> data=new arraylist<dcjqrfb> ();
Dcwebresponse<list<dcjqrfb>> response=new dcwebresponse<list<dcjqrfb>> ();
Map<string,dcjqrfb> map=new hashmap<string,dcjqrfb> ();
Response.setsuccess (TRUE);
Response.setdata (data);
Viewrespons.setresponse (response);
Viewrespons.setpositionfialdnum (Positionfialdnum);
Viewrespons.setnoactivitynum (Noactivitynum);
Cachemanager.putcache (CACHE_KEY,C);
return viewrespons;
}
Java uses static HashMap for simple caching