There is a need in project development, each time you launch the app you need to request parameters to the server, and then go to an interface to take it out directly,
Instead of requesting the network for a better user experience, this data can only be saved for a certain amount of time, and when the app is closed, the data is destroyed,
Check for a long time API seems to not see the timing cache (not sure if there is no), here on their own simulation of a, comments have been very detailed ...
Look at the code:
Package Com.memorycache;import Java.util.date;import Java.util.linkedhashmap;import java.util.map;import com.bxg.news.utils.dateutils;/** * Cache some data into memory and set the Timer cleanup function * * @author Jiangyuesong * */public class Cachecontainer {/** * Cached Map */private static map<string, object> cachefile = new linkedhashmap<string, object> (),/** * Cache can only occupy the maximum heap inside Save */private static Long limit = 1000000;/** * Cache already used size */private static long Usedcache = 0;/** * Take from Cache * * @param key * @return * @throws Exception */synchronized public static Object Getfromcachefile (String key) {string Tempkey = key + "DATE "; Add an expiration time for cached data Object obj = Cachefile.get (Tempkey); Get data if (obj = = null) {//determine if NULL return null;} Date d = new Date (), int time = D.compareto (Dateutils.strtodatelong (string.valueof (obj))); Compare the saved time to the current time on the phone if (>= 0) {//if exceeded, delete Removevaluebykey (key, tempkey); return null;} else {return cachefile.get (key);}} /** * Remove Data from cache * * @param key */public static void Removevaluebykey (String ... key){Long start = 0;long end = 0;//First garbage collection System.GC (); start = Runtime.getruntime (). Freememory (); for (int i = 0; i < Key.leng Th i++) {if (Cachefile.containskey (Key[i])) Cachefile.remove (Key[i]);} System.GC (); end = Runtime.getruntime (). Freememory (); Usedcache-= (End-start);} /** * Save Data Island Map * * @param key * key * @param obj * value * @param holdtime * Save time */publ IC static void Savevaluebykey (String key, Object obj, int holdtime) {judgecontainer ();//Determine if the container exceeds the limit size long start = 0;long end = 0;//First garbage collection System.GC (); start = Runtime.getruntime (). Freememory (); String subkey = "", if (Key.length () > 5) {//due to the addition of a key+date as a timestamp for each key, it is here to determine if there is a timestamp subkey = Key.substring (Key.le Ngth ()-5);} else {subkey = key;} if (Subkey.contains ("DATE")) {if (Cachefile.containskey (key)) {Removevaluebykey (key); Removevaluebykey ( Key.substring (0, Key.length ()-4));}} Cachefile.put (key + "DATE", Dateutils.getpretime (Dateutils.getstringdate (), string.valueof (Holdtime))); Cachefile.put (Key, OBJ); System.GC (); end = Runtime.getruntime (). Freememory (); Usedcache + = (end-start);} /** * Determine if the container size exceeds memory */synchronized public static void Judgecontainer () {while (Usedcache >= limit) {Cachefile.remove (CAC Hefile.size ()-1);}} /** * Force Save key value to map, overwrite if previously present * * @param key * @param obj * @param holdtime */synchronized public static void Savemap 2CacheFile (String key, Object obj,int holdtime) {judgecontainer ();//key that is put in the time key to do the correlation savevaluebykey (key, obj, Holdtime);} /** * into the cache and determine if it exists, do not use * * @param file * @param holdtime * @return */synchronized public static void Putmap2cachefile (St Ring key, Object obj,int holdtime) {judgecontainer (); Savevaluebykey (key, obj, holdtime);} /** * into the cache and determine if it exists, do not use * * @param file * @param holdtime * @return */synchronized public static void Putmap2cachefile (Ma P<string, object> file,int holdtime) {Judgecontainer (); for (String Key:file.keySet ()) {Savevaluebykey (key, File.get (key), holdtime);}}}
Virtual out-of-memory cache for apps on Android