Virtualize timed memory cache for apps on Android
There is a requirement in project development. Each time you start an APP, you need to request parameters from the server, and then retrieve them directly when you enter a certain interface,
Instead of requesting the network for a better user experience, the data can only be stored for a certain period of time. When the APP is closed, the data must be destroyed,
After checking the API for half a day, it seems that the timed cache is not displayed (not sure whether it is available). Here I simulate it myself, and the comments are very detailed...
Check 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 to the memory, then set the timed cleanup function ** @ author JiangYueSong **/public class CacheContainer {/*** cached map */private static Map
CacheFile = new LinkedHashMap
();/*** Maximum heap memory that the cache can only occupy */private static long limit = 1000000; /*** cache size used */private static long usedCache = 0; /*** obtain ** @ param key * @ return * @ throws Exception */synchronized public static Object getFromCacheFile (String key) from the cache) {String tempkey = key + "DATE"; // Add the expiration time for the cached data Object obj = cacheFile. get (tempkey); // obtain the data if (obj = null) {// judge whether it is null return null;} Date d = new Date (); int time = d. compareTo (DateUtils. strToDateLong (String. valueOf (obj); // compare the storage time with the current time on the mobile phone if (time> = 0) {// if the storage time exceeds, delete removeValueByKey (key, tempkey); return null;} else {return cacheFile. get (key) ;}}/*** remove data from the 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. length; 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 * @ param obj * value * @ param holdTime * save time */public static void saveValueByKey (String key, Object obj, int holdTime) {judgeContainer (); // determines whether the container exceeds the maximum size long start = 0; long end = 0; // first garbage collection System. gc (); start = Runtime. getRuntime (). freeMemory (); String subKey = ""; if (key. length ()> 5) {// since a key + DATE is added for each key as the timestamp, we can determine whether the timestamp subKey = key exists here. substring (key. length ()-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 whether the container size exceeds the memory */synchronized public static void judgeContainer () {while (usedCache> = limit) {cacheFile. remove (cacheFile. size ()-1) ;}}/*** forcibly Save the key value to map, if it exists before, it overwrites ** @ param key * @ param obj * @ param holdTime */synchronized public static void saveMap2CacheFile (String key, Object obj, int holdTime) {judgeContainer (); // first, associate the key with the time key to saveValueByKey (key, obj, holdTime);}/*** into the cache and determine whether the cache exists, do not use ** @ param file * @ param holdTime * @ return */synchronized public static void putMap2CacheFile (String key, Object obj, int holdTime) {judgeContainer (); saveValueByKey (key, obj, holdTime);}/*** put into the cache and determine whether the cache exists, do not use ** @ param file * @ param holdTime * @ return */synchronized public static void putMap2CacheFile (Map
File, int holdTime) {judgeContainer (); for (String key: file. keySet () {saveValueByKey (key, file. get (key), holdTime );}}}