缓存之 ACache
生活随笔
收集整理的這篇文章主要介紹了
缓存之 ACache
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.android緩存的介紹
Android開發本質上就是手機和互聯網中的webserver之間進行通信,就必定須要從服務端獲取數據。而重復通過網絡獲取數據是比較耗時的。特別是訪問比較多的時候。會極大影響了性能,Android中可通過二級緩存來降低頻繁的網絡操作,降低流量、提升性能。1.二級緩存定義:
當Android端須要獲得數據時比方獲取網絡中的圖片。我們首先從內存中查找(按鍵查找)。內存中沒有的再從磁盤文件或sqlite中去查找。若磁盤中也沒有才通過網絡獲取;當獲得來自網絡的數據,就以key-value對的方式先緩存到內存(一級緩存),同一時候緩存到文件或sqlite中(二級緩存)。注意:內存緩存會造成堆內存泄露。全部一級緩存通常要嚴格控制緩存的大小。一般控制在系統內存的1/4。2.保存在本地了怎么更新網絡數據呢?
理解了二級緩存大家可能會有個問題網絡中的數據是變化的,數據一旦放入緩存中,再取該數據就是從緩存中獲得。這樣豈不是不能體現數據的變化?我們在緩存數據時會設置有效時間。比方說30分鐘。若超過這個時間數據就失效并釋放空間。然后又一次請求網絡中的數據。有的童鞋就問30分鐘內咋辦?那好吧。我也沒招了,僅僅有下拉刷新了吧。?實際上這不是問題。2.本篇就介紹下Acache緩存
ACache是一個為android制定的?輕量級的?開源緩存框架。輕量到僅僅有一個java文件(由十幾個類精簡而來)。1、它能夠緩存什么東西?
普通的字符串、json、序列化的java對象。和?字節數字。2、它有什么特色?
特色主要是: 1:輕,輕到僅僅有一個JAVA文件。 2:可配置,能夠配置緩存路徑。緩存大小,緩存數量等。 3:能夠設置緩存超時時間。緩存超時自己主動失效,并被刪除。 4:多進程的支持。3.使用
??1.初始化ACache組件
????????ACache???acache=ACache.get(context) 或 ????????ACache???acache=ACache.get(context,max_size,max_count) 參數說明: ???????? max_size:設置限制緩存大小。默覺得50M ????????max_count:設置緩存數據的數量,默認不限制?? ?2.設置緩存數據?
?????????acache.put(key,data,time)或acache.put(key,data) ????????將數據同一時候上存入一級緩存(內存Map)和二級緩存(文件)中 參數說明: ????????Key:為存入緩存的數據設置唯一標識。取數據時就依據key來獲得的 ????????Data:要存入的數據,acache支持的數據類型如圖所看到的: ????????有String、可序列化的對象、字節數組、Drawable等 Time:設置緩存數據的有效時間。單位秒?????
? ? ?3.從緩存中取數據
????????提供一系列getAsXXX()方法,如圖所看到的。 ????????依據不同存入數據,調用不同的方法取數據 4.ACache代碼片 package com.example.acachetest.util;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.RandomAccessFile; import java.io.Serializable; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong;import org.json.JSONArray; import org.json.JSONObject;import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.PixelFormat; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable;public class ACache {public static final int TIME_HOUR = 60 * 60;public static final int TIME_DAY = TIME_HOUR * 24;private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mbprivate static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放數據的數量private static Map<String, ACache> mInstanceMap = new HashMap<String, ACache>();private ACacheManager mCache;public static ACache get(Context ctx) {return get(ctx, "ACache");}public static ACache get(Context ctx, String cacheName) {File f = new File(ctx.getCacheDir(), cacheName);return get(f, MAX_SIZE, MAX_COUNT);}public static ACache get(File cacheDir) {return get(cacheDir, MAX_SIZE, MAX_COUNT);}public static ACache get(Context ctx, long max_zise, int max_count) {File f = new File(ctx.getCacheDir(), "ACache");return get(f, max_zise, max_count);}public static ACache get(File cacheDir, long max_zise, int max_count) {ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());if (manager == null) {manager = new ACache(cacheDir, max_zise, max_count);mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);}return manager;}private static String myPid() {return "_" + android.os.Process.myPid();}private ACache(File cacheDir, long max_size, int max_count) {if (!cacheDir.exists() && !cacheDir.mkdirs()) {throw new RuntimeException("can't make dirs in "+ cacheDir.getAbsolutePath());}mCache = new ACacheManager(cacheDir, max_size, max_count);}// =======================================// ============ String數據 讀寫 ==============// =======================================/*** 保存 String數據 到 緩存中* * @param key* 保存的key* @param value* 保存的String數據*/public void put(String key, String value) {File file = mCache.newFile(key);BufferedWriter out = null;try {out = new BufferedWriter(new FileWriter(file), 1024);out.write(value);} catch (IOException e) {e.printStackTrace();} finally {if (out != null) {try {out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}mCache.put(file);}}/*** 保存 String數據 到 緩存中* * @param key* 保存的key* @param value* 保存的String數據* @param saveTime* 保存的時間,單位:秒*/public void put(String key, String value, int saveTime) {put(key, Utils.newStringWithDateInfo(saveTime, value));}/*** 讀取 String數據* * @param key* @return String 數據*/public String getAsString(String key) {File file = mCache.get(key);if (!file.exists())return null;boolean removeFile = false;BufferedReader in = null;try {in = new BufferedReader(new FileReader(file));String readString = "";String currentLine;while ((currentLine = in.readLine()) != null) {readString += currentLine;}if (!Utils.isDue(readString)) {return Utils.clearDateInfo(readString);} else {removeFile = true;return null;}} catch (IOException e) {e.printStackTrace();return null;} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}if (removeFile)remove(key);}}// =======================================// ============= JSONObject 數據 讀寫 ==============// =======================================/*** 保存 JSONObject數據 到 緩存中* * @param key* 保存的key* @param value* 保存的JSON數據*/public void put(String key, JSONObject value) {put(key, value.toString());}/*** 保存 JSONObject數據 到 緩存中* * @param key* 保存的key* @param value* 保存的JSONObject數據* @param saveTime* 保存的時間,單位:秒*/public void put(String key, JSONObject value, int saveTime) {put(key, value.toString(), saveTime);}/*** 讀取JSONObject數據* * @param key* @return JSONObject數據*/public JSONObject getAsJSONObject(String key) {String JSONString = getAsString(key);try {JSONObject obj = new JSONObject(JSONString);return obj;} catch (Exception e) {e.printStackTrace();return null;}}// =======================================// ============ JSONArray 數據 讀寫 =============// =======================================/*** 保存 JSONArray數據 到 緩存中* * @param key* 保存的key* @param value* 保存的JSONArray數據*/public void put(String key, JSONArray value) {put(key, value.toString());}/*** 保存 JSONArray數據 到 緩存中* * @param key* 保存的key* @param value* 保存的JSONArray數據* @param saveTime* 保存的時間,單位:秒*/public void put(String key, JSONArray value, int saveTime) {put(key, value.toString(), saveTime);}/*** 讀取JSONArray數據* * @param key* @return JSONArray數據*/public JSONArray getAsJSONArray(String key) {String JSONString = getAsString(key);try {JSONArray obj = new JSONArray(JSONString);return obj;} catch (Exception e) {e.printStackTrace();return null;}}// =======================================// ============== byte 數據 讀寫 =============// =======================================/*** 保存 byte數據 到 緩存中* * @param key* 保存的key* @param value* 保存的數據*/public void put(String key, byte[] value) {File file = mCache.newFile(key);FileOutputStream out = null;try {out = new FileOutputStream(file);out.write(value);} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}mCache.put(file);}}/*** 保存 byte數據 到 緩存中* * @param key* 保存的key* @param value* 保存的數據* @param saveTime* 保存的時間。單位:秒*/public void put(String key, byte[] value, int saveTime) {put(key, Utils.newByteArrayWithDateInfo(saveTime, value));}/*** 獲取 byte 數據* * @param key* @return byte 數據*/public byte[] getAsBinary(String key) {RandomAccessFile RAFile = null;boolean removeFile = false;try {File file = mCache.get(key);if (!file.exists())return null;RAFile = new RandomAccessFile(file, "r");byte[] byteArray = new byte[(int) RAFile.length()];RAFile.read(byteArray);if (!Utils.isDue(byteArray)) {return Utils.clearDateInfo(byteArray);} else {removeFile = true;return null;}} catch (Exception e) {e.printStackTrace();return null;} finally {if (RAFile != null) {try {RAFile.close();} catch (IOException e) {e.printStackTrace();}}if (removeFile)remove(key);}}// =======================================// ============= 序列化 數據 讀寫 ===============// =======================================/*** 保存 Serializable數據 到 緩存中* * @param key* 保存的key* @param value* 保存的value*/public void put(String key, Serializable value) {put(key, value, -1);}/*** 保存 Serializable數據到 緩存中* * @param key* 保存的key* @param value* 保存的value* @param saveTime* 保存的時間。單位:秒*/public void put(String key, Serializable value, int saveTime) {ByteArrayOutputStream baos = null;ObjectOutputStream oos = null;try {baos = new ByteArrayOutputStream();oos = new ObjectOutputStream(baos);oos.writeObject(value);byte[] data = baos.toByteArray();if (saveTime != -1) {put(key, data, saveTime);} else {put(key, data);}} catch (Exception e) {e.printStackTrace();} finally {try {oos.close();} catch (IOException e) {}}}/*** 讀取 Serializable數據* * @param key* @return Serializable 數據*/public Object getAsObject(String key) {byte[] data = getAsBinary(key);if (data != null) {ByteArrayInputStream bais = null;ObjectInputStream ois = null;try {bais = new ByteArrayInputStream(data);ois = new ObjectInputStream(bais);Object reObject = ois.readObject();return reObject;} catch (Exception e) {e.printStackTrace();return null;} finally {try {if (bais != null)bais.close();} catch (IOException e) {e.printStackTrace();}try {if (ois != null)ois.close();} catch (IOException e) {e.printStackTrace();}}}return null;}// =======================================// ============== bitmap 數據 讀寫 =============// =======================================/*** 保存 bitmap 到 緩存中* * @param key* 保存的key* @param value* 保存的bitmap數據*/public void put(String key, Bitmap value) {put(key, Utils.Bitmap2Bytes(value));}/*** 保存 bitmap 到 緩存中* * @param key* 保存的key* @param value* 保存的 bitmap 數據* @param saveTime* 保存的時間,單位:秒*/public void put(String key, Bitmap value, int saveTime) {put(key, Utils.Bitmap2Bytes(value), saveTime);}/*** 讀取 bitmap 數據* * @param key* @return bitmap 數據*/public Bitmap getAsBitmap(String key) {if (getAsBinary(key) == null) {return null;}return Utils.Bytes2Bimap(getAsBinary(key));}// =======================================// ============= drawable 數據 讀寫 =============// =======================================/*** 保存 drawable 到 緩存中* * @param key* 保存的key* @param value* 保存的drawable數據*/public void put(String key, Drawable value) {put(key, Utils.drawable2Bitmap(value));}/*** 保存 drawable 到 緩存中* * @param key* 保存的key* @param value* 保存的 drawable 數據* @param saveTime* 保存的時間,單位:秒*/public void put(String key, Drawable value, int saveTime) {put(key, Utils.drawable2Bitmap(value), saveTime);}/*** 讀取 Drawable 數據* * @param key* @return Drawable 數據*/public Drawable getAsDrawable(String key) {if (getAsBinary(key) == null) {return null;}return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key)));}/*** 獲取緩存文件* * @param key* @return value 緩存的文件*/public File file(String key) {File f = mCache.newFile(key);if (f.exists())return f;return null;}/*** 移除某個key* * @param key* @return 是否移除成功*/public boolean remove(String key) {return mCache.remove(key);}/*** 清除全部數據*/public void clear() {mCache.clear();}/*** @title 緩存管理器* @version 1.0*/public class ACacheManager {private final AtomicLong cacheSize;private final AtomicInteger cacheCount;private final long sizeLimit;private final int countLimit;private final Map<File, Long> lastUsageDates = Collections.synchronizedMap(new HashMap<File, Long>());protected File cacheDir;private ACacheManager(File cacheDir, long sizeLimit, int countLimit) {this.cacheDir = cacheDir;this.sizeLimit = sizeLimit;this.countLimit = countLimit;cacheSize = new AtomicLong();cacheCount = new AtomicInteger();calculateCacheSizeAndCacheCount();}/*** 計算 cacheSize和cacheCount*/private void calculateCacheSizeAndCacheCount() {new Thread(new Runnable() {@Overridepublic void run() {int size = 0;int count = 0;File[] cachedFiles = cacheDir.listFiles();if (cachedFiles != null) {for (File cachedFile : cachedFiles) {size += calculateSize(cachedFile);count += 1;lastUsageDates.put(cachedFile,cachedFile.lastModified());}cacheSize.set(size);cacheCount.set(count);}}}).start();}private void put(File file) {int curCacheCount = cacheCount.get();while (curCacheCount + 1 > countLimit) {long freedSize = removeNext();cacheSize.addAndGet(-freedSize);curCacheCount = cacheCount.addAndGet(-1);}cacheCount.addAndGet(1);long valueSize = calculateSize(file);long curCacheSize = cacheSize.get();while (curCacheSize + valueSize > sizeLimit) {long freedSize = removeNext();curCacheSize = cacheSize.addAndGet(-freedSize);}cacheSize.addAndGet(valueSize);Long currentTime = System.currentTimeMillis();file.setLastModified(currentTime);lastUsageDates.put(file, currentTime);}private File get(String key) {File file = newFile(key);Long currentTime = System.currentTimeMillis();file.setLastModified(currentTime);lastUsageDates.put(file, currentTime);return file;}private File newFile(String key) {return new File(cacheDir, key.hashCode() + "");}private boolean remove(String key) {File image = get(key);return image.delete();}private void clear() {lastUsageDates.clear();cacheSize.set(0);File[] files = cacheDir.listFiles();if (files != null) {for (File f : files) {f.delete();}}}/*** 移除舊的文件* * @return*/private long removeNext() {if (lastUsageDates.isEmpty()) {return 0;}Long oldestUsage = null;File mostLongUsedFile = null;Set<Entry<File, Long>> entries = lastUsageDates.entrySet();synchronized (lastUsageDates) {for (Entry<File, Long> entry : entries) {if (mostLongUsedFile == null) {mostLongUsedFile = entry.getKey();oldestUsage = entry.getValue();} else {Long lastValueUsage = entry.getValue();if (lastValueUsage < oldestUsage) {oldestUsage = lastValueUsage;mostLongUsedFile = entry.getKey();}}}}long fileSize = calculateSize(mostLongUsedFile);if (mostLongUsedFile.delete()) {lastUsageDates.remove(mostLongUsedFile);}return fileSize;}private long calculateSize(File file) {return file.length();}}/*** @title 時間計算工具類* @version 1.0*/private static class Utils {/*** 推斷緩存的String數據是否到期* * @param str* @return true:到期了 false:還沒有到期*/private static boolean isDue(String str) {return isDue(str.getBytes());}/*** 推斷緩存的byte數據是否到期* * @param data* @return true:到期了 false:還沒有到期*/private static boolean isDue(byte[] data) {String[] strs = getDateInfoFromDate(data);if (strs != null && strs.length == 2) {String saveTimeStr = strs[0];while (saveTimeStr.startsWith("0")) {saveTimeStr = saveTimeStr.substring(1, saveTimeStr.length());}long saveTime = Long.valueOf(saveTimeStr);long deleteAfter = Long.valueOf(strs[1]);if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) {return true;}}return false;}private static String newStringWithDateInfo(int second, String strInfo) {return createDateInfo(second) + strInfo;}private static byte[] newByteArrayWithDateInfo(int second, byte[] data2) {byte[] data1 = createDateInfo(second).getBytes();byte[] retdata = new byte[data1.length + data2.length];System.arraycopy(data1, 0, retdata, 0, data1.length);System.arraycopy(data2, 0, retdata, data1.length, data2.length);return retdata;}private static String clearDateInfo(String strInfo) {if (strInfo != null && hasDateInfo(strInfo.getBytes())) {strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1,strInfo.length());}return strInfo;}private static byte[] clearDateInfo(byte[] data) {if (hasDateInfo(data)) {return copyOfRange(data, indexOf(data, mSeparator) + 1,data.length);}return data;}private static boolean hasDateInfo(byte[] data) {return data != null && data.length > 15 && data[13] == '-'&& indexOf(data, mSeparator) > 14;}private static String[] getDateInfoFromDate(byte[] data) {if (hasDateInfo(data)) {String saveDate = new String(copyOfRange(data, 0, 13));String deleteAfter = new String(copyOfRange(data, 14,indexOf(data, mSeparator)));return new String[] { saveDate, deleteAfter };}return null;}private static int indexOf(byte[] data, char c) {for (int i = 0; i < data.length; i++) {if (data[i] == c) {return i;}}return -1;}private static byte[] copyOfRange(byte[] original, int from, int to) {int newLength = to - from;if (newLength < 0)throw new IllegalArgumentException(from + " > " + to);byte[] copy = new byte[newLength];System.arraycopy(original, from, copy, 0,Math.min(original.length - from, newLength));return copy;}private static final char mSeparator = ' ';private static String createDateInfo(int second) {String currentTime = System.currentTimeMillis() + "";while (currentTime.length() < 13) {currentTime = "0" + currentTime;}return currentTime + "-" + second + mSeparator;}/** Bitmap → byte[]*/private static byte[] Bitmap2Bytes(Bitmap bm) {if (bm == null) {return null;}ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();}/** byte[] → Bitmap*/private static Bitmap Bytes2Bimap(byte[] b) {if (b.length == 0) {return null;}return BitmapFactory.decodeByteArray(b, 0, b.length);}/** Drawable → Bitmap*/private static Bitmap drawable2Bitmap(Drawable drawable) {if (drawable == null) {return null;}// 取 drawable 的長寬int w = drawable.getIntrinsicWidth();int h = drawable.getIntrinsicHeight();// 取 drawable 的顏色格式Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立相應 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立相應 bitmap 的畫布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 內容畫到畫布中 drawable.draw(canvas); return bitmap; } /* * Bitmap → Drawable */ @SuppressWarnings("deprecation") private static Drawable bitmap2Drawable(Bitmap bm) { if (bm == null) { return null; } return new BitmapDrawable(bm); } } }
測試Activity: package com.example.acachetest;import com.example.acachetest.util.ACache;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.widget.TextView;public class MainActivity extends Activity {private ACache aCache;private TextView mTextView;private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg) {if(msg.what==1){mTextView.setText("開始保存");}else if(msg.what==2){initDate();}else{if(aCache.getAsString("newText")==null){mTextView.setText("沒有保存的數據了,又一次載入");}}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = (TextView) findViewById(R.id.list);initAcache();initDate();}private void initDate() {String cacheData = aCache.getAsString("newText");// 從緩存中取數據if (cacheData != null) {mTextView.setText(cacheData );} else {// 模擬網絡請求數據new Thread(new Runnable() {@Overridepublic void run() {SystemClock.sleep(1000);aCache.put("newText", "保存3秒", 3);//間數據放到緩存中,保存時間是2秒mHandler.sendEmptyMessage(1);mHandler.sendEmptyMessageDelayed(2, 1000);//驗證在保存轉態mHandler.sendEmptyMessageDelayed(3, 4000);//驗證不在保存轉態}}).start();}}private void initAcache() {aCache = ACache.get(this);// 默認選擇的路徑是new File(context.getCacheDir(),// "ACache")// String path = getExternalCacheDir().getAbsolutePath();// aCache = ACache.get(new File(path));//設置存儲路徑用于手動清空緩存使用。}}
轉載于:https://www.cnblogs.com/liguangsunls/p/7344887.html
總結
以上是生活随笔為你收集整理的缓存之 ACache的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL安装教程图解
- 下一篇: 基于 CentOS Mysql 安装与主