Caching means that the objects that the program or system often calls are stored in memory, and can be called quickly when used once, without creating new duplicate instances. Doing so can reduce system overhead and improve system efficiency.
Caches can be mainly divided into two categories:
1. Through file caching, as the name implies, file caching refers to storing data on disk, whether you are in XML format, serialized file DAT format or other file formats;
2. Memory cache, that is, implement static maps in a class, and perform regular additions, deletion and search of this map.
import java.util.*; //Description: Manage cache//Extensible functions: When chche reaches memory overflow, some of the earliest cache objects must be cleared, which requires saving creation time for each cache object public class CacheManager { private static HashMap cacheMap = new HashMap(); //Single instance constructor private CacheManager() { super(); } //Get boolean cache 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 boolean cache public synchronized static boolean setSimpleFlag(String key,boolean flag){ if (flag && getSimpleFlag(key)) {//If true, overwrite is not allowed to be returned 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 cache. Synchronized static method private synchronized static Cache getCache(String key) { return (Cache) cacheMap.get(key); } //Judge whether there is a cache private synchronized static boolean hasCache(String key) { return cacheMap.containsKey(key); } //Clear all caches public synchronized static void clearAll() { cacheMap.clear(); } //Clear a certain type of cache, and determine whether its KEY and the incoming TYPE match the public synchronized static void clearAll(String type) { Iterator i = cacheMap.entrySet().iterator(); String key; ArrayList 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 it matches, delete 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 the 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); } 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); //When the cache is loaded by default, the termination status is FALSE cacheMap.put(key, cache); } //Rewrite the 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); } //Judge whether the cache terminates public static boolean cacheExpired(Cache cache) { if (null == cache) { //The incoming cache does not exist return false; } long nowDt = System.currentTimeMillis(); //The current number of milliseconds in the system long cacheDt = cache.getTimeOut(); //The number of expired milliseconds in the cache if (cacheDt <= 0||cacheDt>nowDt) { //When the expiration time is less than or equal to zero, or the expiration time is greater than the current time, it is FALSE return false; } else { //When greater than the expiration time is expired, return true; } } //Get the size in the cache public static int getCacheSize() { return cacheMap.size(); } //Get 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 it matches, delete k++; } } } catch (Exception ex) { ex.printStackTrace(); } return k; } //Get all 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; } } //Get 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; } } } package lhm.hcy.guge.frameset.cache; public class Cache { private String key;//Cache ID private Object value;//Cache data private long timeOut;//Update time private boolean expired; //Do not terminate public Cache() { super(); } 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 object) { value = object; } public boolean isExpired() { return expired; } public void setExpired(boolean b) { expired = b; } } // Test class, class Test { public static void main(String[] args) { System.out.println(CacheManager.getSimpleFlag("alksd")); // CacheManager.putCache("abc", new Cache()); // CacheManager.putCache("def", new Cache()); // CacheManager.putCache("ccc", new Cache()); // CacheManager.clearOnly(""); // Cache c = new Cache(); // for (int i = 0; i < 10; i++) { // CacheManager.putCache("" + i, c); // } // CacheManager.putCache("aaaaaaaa", c); // CacheManager.putCache("abchcy;alskd", c); // CacheManager.putCache("cccccccc", c); // CacheManager.putCache("abcoqiwhcy", c); // System.out.println("Size before deletion: "+CacheManager.getCacheSize()); // CacheManager.getCacheAllkey(); // CacheManager.clearAll("aaaa"); // System.out.println("Size after deletion: "+CacheManager.getCacheSize()); // CacheManager.getCacheAllkey(); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.