feat(cache): 重构缓存架构并新增多级缓存及统计接口
This commit is contained in:
1 parent
a7c6e1a00a
commit
6e995e0795
8 files changed
+418
-62
No files matched your search
@@ -6,9 +6,7 @@ import android.graphics.drawable.Drawable;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fuying.sn.config.CommonConfig;
|
||||
import com.jakewharton.disklrucache.DiskLruCache;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@@ -19,20 +17,21 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 磁盘缓存帮助类
|
||||
* https://www.cnblogs.com/aademeng/articles/6817058.html
|
||||
*/
|
||||
public class CacheHelper {
|
||||
private static final String TAG = "DiskLruCacheHelper";
|
||||
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
private static final String DIR_NAME = "diskCache";
|
||||
private static final int MAX_COUNT = 1024 * 1024 * 1024;
|
||||
private static final int DEFAULT_APP_VERSION = 1;
|
||||
@@ -72,9 +71,8 @@ public class CacheHelper {
|
||||
|
||||
int appVersion = context == null ? DEFAULT_APP_VERSION : Utils.getAppVersion(context);
|
||||
|
||||
DiskLruCache diskLruCache = null;
|
||||
try {
|
||||
diskLruCache = DiskLruCache.open(
|
||||
return DiskLruCache.open(
|
||||
dir,
|
||||
appVersion,
|
||||
DEFAULT_APP_VERSION,
|
||||
@@ -82,13 +80,12 @@ public class CacheHelper {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return diskLruCache;
|
||||
return null;
|
||||
}
|
||||
|
||||
private DiskLruCache generateCache(Context context, String dirName, int maxCount) {
|
||||
DiskLruCache diskLruCache = null;
|
||||
try {
|
||||
diskLruCache = DiskLruCache.open(
|
||||
return DiskLruCache.open(
|
||||
getDiskCacheDir(context, dirName),
|
||||
Utils.getAppVersion(context),
|
||||
DEFAULT_APP_VERSION,
|
||||
@@ -96,7 +93,7 @@ public class CacheHelper {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return diskLruCache;
|
||||
return null;
|
||||
}
|
||||
|
||||
// =======================================
|
||||
@@ -104,9 +101,7 @@ public class CacheHelper {
|
||||
// =======================================
|
||||
|
||||
public void put(String key, String value) {
|
||||
// Log.e(TAG, "put: key = " + key + " value = " + value);
|
||||
mMMKV.encode(key + "_time", System.currentTimeMillis());
|
||||
mMMKV.encode(key + "_mmkv", value);
|
||||
Log.e(TAG, "put: key = " + key + " value = " + value);
|
||||
|
||||
DiskLruCache.Editor edit = null;
|
||||
BufferedWriter bw = null;
|
||||
@@ -116,15 +111,16 @@ public class CacheHelper {
|
||||
return;
|
||||
}
|
||||
OutputStream os = edit.newOutputStream(0);
|
||||
bw = new BufferedWriter(new OutputStreamWriter(os));
|
||||
bw = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
|
||||
bw.write(value);
|
||||
edit.commit();//write CLEAN
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "put: " + e.getMessage());
|
||||
try {
|
||||
//s
|
||||
edit.abort();//write REMOVE
|
||||
if (edit != null) {
|
||||
edit.abort();//write REMOVE
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
Log.e(TAG, "put: " + e1.getMessage());
|
||||
@@ -142,30 +138,30 @@ public class CacheHelper {
|
||||
}
|
||||
|
||||
public String getAsString(String key) {
|
||||
// Log.e(TAG, "getAsString: " + key);
|
||||
Log.e(TAG, "getAsString: " + key);
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
//write READ
|
||||
inputStream = get(key);
|
||||
if (inputStream == null) {
|
||||
return mMMKV.decodeString(key + "_mmkv", null);
|
||||
return null;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = 0;
|
||||
byte[] buf = new byte[128];
|
||||
while ((len = inputStream.read(buf)) != -1) {
|
||||
sb.append(new String(buf, 0, len));
|
||||
InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
||||
char[] buf = new char[1024];
|
||||
int len;
|
||||
while ((len = isr.read(buf)) != -1) {
|
||||
sb.append(buf, 0, len);
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "getAsString: " + e.getMessage());
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
Log.e(TAG, "getAsString: " + e1.getMessage());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,9 +195,9 @@ public class CacheHelper {
|
||||
|
||||
public JSONArray getAsJSONArray(String key) {
|
||||
String JSONString = getAsString(key);
|
||||
if (JSONString == null) return null;
|
||||
try {
|
||||
JSONArray obj = new JSONArray(JSONString);
|
||||
return obj;
|
||||
return new JSONArray(JSONString);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@@ -233,7 +229,9 @@ public class CacheHelper {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
editor.abort();//write REMOVE
|
||||
if (editor != null) {
|
||||
editor.abort();//write REMOVE
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
@@ -256,8 +254,8 @@ public class CacheHelper {
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[256];
|
||||
int len = 0;
|
||||
while ((len = is.read(buf)) != -1) {
|
||||
@@ -266,6 +264,12 @@ public class CacheHelper {
|
||||
res = baos.toByteArray();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -289,7 +293,9 @@ public class CacheHelper {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
editor.abort();
|
||||
if (editor != null) {
|
||||
editor.abort();
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
@@ -365,8 +371,8 @@ public class CacheHelper {
|
||||
// =======================================
|
||||
public boolean remove(String key) {
|
||||
try {
|
||||
key = Utils.hashKeyForDisk(key);
|
||||
return mDiskLruCache.remove(key);
|
||||
String hashKey = Utils.hashKeyForDisk(key);
|
||||
return mDiskLruCache.remove(hashKey);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -379,7 +385,6 @@ public class CacheHelper {
|
||||
|
||||
public void delete() throws IOException {
|
||||
mDiskLruCache.delete();
|
||||
mMMKV.clearAll();
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
@@ -413,12 +418,12 @@ public class CacheHelper {
|
||||
//basic editor
|
||||
public DiskLruCache.Editor editor(String key) {
|
||||
try {
|
||||
key = Utils.hashKeyForDisk(key);
|
||||
String hashKey = Utils.hashKeyForDisk(key);
|
||||
//wirte DIRTY
|
||||
DiskLruCache.Editor edit = mDiskLruCache.edit(key);
|
||||
DiskLruCache.Editor edit = mDiskLruCache.edit(hashKey);
|
||||
//edit maybe null :the entry is editing
|
||||
if (edit == null) {
|
||||
Log.w(TAG, "the entry spcified key:" + key + " is editing by other . ");
|
||||
Log.w(TAG, "the entry spcified key:" + hashKey + " is editing by other . ");
|
||||
}
|
||||
return edit;
|
||||
} catch (IOException e) {
|
||||
@@ -436,7 +441,7 @@ public class CacheHelper {
|
||||
DiskLruCache.Snapshot snapshot = mDiskLruCache.get(Utils.hashKeyForDisk(key));
|
||||
if (snapshot == null) //not find entry , or entry.readable = false
|
||||
{
|
||||
// Log.e(TAG, "not find entry , or entry.readable = false");
|
||||
Log.e(TAG, "not find entry , or entry.readable = false");
|
||||
return null;
|
||||
}
|
||||
//write READ
|
||||
@@ -455,20 +460,17 @@ public class CacheHelper {
|
||||
// =======================================
|
||||
|
||||
private File getDiskCacheDir(Context context, String uniqueName) {
|
||||
String cachePath;
|
||||
File cacheDir;
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|
||||
|| !Environment.isExternalStorageRemovable()) {
|
||||
if (context.getExternalCacheDir() != null) {
|
||||
cachePath = context.getExternalCacheDir().getPath();
|
||||
} else if (context.getExternalFilesDir("cache") != null) {
|
||||
cachePath = context.getExternalFilesDir("cache").getPath();
|
||||
} else {
|
||||
cachePath = context.getCacheDir().getPath();
|
||||
cacheDir = context.getExternalCacheDir();
|
||||
if (cacheDir == null) {
|
||||
cacheDir = context.getCacheDir();
|
||||
}
|
||||
} else {
|
||||
cachePath = context.getCacheDir().getPath();
|
||||
cacheDir = context.getCacheDir();
|
||||
}
|
||||
return new File(cachePath + File.separator + uniqueName);
|
||||
return new File(cacheDir, uniqueName);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user