在Android上為APP虛擬出定時的記憶體緩衝

來源:互聯網
上載者:User

標籤:android開發   記憶體   使用者體驗   

在項目開發中有一個需求,每次啟動APP的時候都需要向伺服器請求參數,然後進入到某個介面時直接取出來,

而不去請求網路,實現更好的使用者體驗,但是這些資料只能儲存一定時間,而且當APP關閉時,這些資料就得銷毀,

查了半天API貌似沒看到定時緩衝(不確定到底有沒有),這裡就自己類比了一個,注釋已經很詳細了。。。

看代碼:


package com.memorycache;import java.util.Date;import java.util.LinkedHashMap;import java.util.Map;import com.bxg.news.utils.DateUtils;/** * 把一些資料緩衝到記憶體,然後設定定時清理功能 *  * @author JiangYueSong *  */public class CacheContainer {/** * 緩衝的map */private static Map<String, Object> cacheFile = new LinkedHashMap<String, Object>();/** * 緩衝只能佔用的最大堆記憶體 */private static long limit = 1000000;/** * 緩衝已經使用的大小 */private static long usedCache = 0;/** * 從緩衝中取 *  * @param key * @return * @throws Exception */synchronized public static Object getFromCacheFile(String key) {String tempkey = key + "DATE"; // 為緩衝的資料添加到期時間Object obj = cacheFile.get(tempkey); // 獲得資料if (obj == null) { // 判斷是否為空白return null;}Date d = new Date();int time = d.compareTo(DateUtils.strToDateLong(String.valueOf(obj))); // 把儲存的時間與現在手機上的時間對比if (time >= 0) { // 如果超出,則刪除removeValueByKey(key, tempkey);return null;} else {return cacheFile.get(key);}}/** * 從緩衝中移除資料 *  * @param key */public static void removeValueByKey(String... key) {long start = 0;long end = 0;// 先記憶體回收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);}/** * 儲存資料島map *  * @param key *            key * @param obj *            value * @param holdTime *            儲存時間 */public static void saveValueByKey(String key, Object obj, int holdTime) {judgeContainer(); // 判斷容器是否超出限制大小long start = 0;long end = 0;// 先記憶體回收System.gc();start = Runtime.getRuntime().freeMemory();String subKey = "";if (key.length() > 5) { // 由於為每一個key添加了一個key+DATE作為時間戳記,所以這裡判斷是否存在這個時間戳記subKey = key.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);}/** * 判斷容器大小是否超出記憶體 */synchronized public static void judgeContainer() {while (usedCache >= limit) {cacheFile.remove(cacheFile.size() - 1);}}/** * 強制儲存key value到map,如果之前存在則覆蓋 *  * @param key * @param obj * @param holdTime */synchronized public static void saveMap2CacheFile(String key, Object obj,int holdTime) {judgeContainer();// 首先放入時間的key給的key做關聯saveValueByKey(key, obj, holdTime);}/** * 放入緩衝中,並判斷是否存在,不使用 *  * @param file * @param holdTime * @return */synchronized public static void putMap2CacheFile(String key, Object obj,int holdTime) {judgeContainer();saveValueByKey(key, obj, holdTime);}/** * 放入緩衝中,並判斷是否存在,不使用 *  * @param file * @param holdTime * @return */synchronized public static void putMap2CacheFile(Map<String, Object> file,int holdTime) {judgeContainer();for (String key : file.keySet()) {saveValueByKey(key, file.get(key), holdTime);}}}


在Android上為APP虛擬出定時的記憶體緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.