APP本地缓存CacheManager

时间:2022-10-28 16:43:07

CacheManager(缓存)

通过将Bean对象缓存到SD,Bean对象需要序列化实现

CacheManager.java

public class CacheManager<T extends Serializable> {

private static String DIR = Environment.getExternalStorageDirectory().getPath();
private static int SPACE_MIN_CACHE = 1024 * 1024 * 20;//20M
private static CacheManager manager;

private CacheManager() {
}

public synchronized static CacheManager getInstance() {
if (manager == null) {
manager = new CacheManager();
}
return manager;
}

public void clearCache() {
File file = null;
File[] files = null;
if (BaseUtil.checkSDMounted()) {
file = new File(DIR);
files = file.listFiles();
if (files != null) {
for (File f : files) {
f.delete();
}
}
}
}

public synchronized void putCacheFile(String key, T t, long time) {
if (!TextUtils.isEmpty(key)) {
String md_key = BaseUtil.getMD5(key);
if (!TextUtils.isEmpty(md_key)) {
final CacheItem<T> cacheItem = new CacheItem<T>(md_key, t, time);
saveCacheFile(md_key, cacheItem);
}
}
}

private boolean saveCacheFile(String md_key, CacheItem cacheItem) {
if (BaseUtil.getSDSize() > SPACE_MIN_CACHE) {
saveCacheObject(cacheItem);
return true;
}
return false;
}

private void saveCacheObject(CacheItem cacheItem) {
if (cacheItem != null) {
BaseUtil.saveObject(cacheItem, DIR + cacheItem.getKey());
}
}

public synchronized T getCacheFile(String key) {
if (!TextUtils.isEmpty(key)) {
String md_key = BaseUtil.getMD5(key);
if (!TextUtils.isEmpty(md_key)) {
String path = DIR + md_key;
if (!TextUtils.isEmpty(path)) {
CacheItem<T> cacheItem = BaseUtil.getObject(path);
if (cacheItem != null) {
if (System.currentTimeMillis() < cacheItem.getTime()) {
return cacheItem.getData();
}
}
}
}
}
return null;
}

public void initCache() {
if (BaseUtil.checkSDMounted()) {
if (BaseUtil.getSDSize() < SPACE_MIN_CACHE) {
clearCache();
} else {
File file = new File(DIR);
if (!file.exists()) {
file.mkdirs();
}
}
}
}
}

BaseUtil.java

public class BaseUtil {

private static final char[] HEX = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};


public static String getMD5(String key) {

if (!TextUtils.isEmpty(key)) {
try {
final MessageDigest md = MessageDigest.getInstance(key);
md.update(key.trim().getBytes());
final byte[] bytes = md.digest();
return toHexString(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return key;
}

private static String toHexString(final byte[] bytes) {

final StringBuilder sb = new StringBuilder();

if (bytes != null) {
for (byte b : bytes) {
sb.append(HEX[(b & 0xf0)] >>> 4);
sb.append(HEX[b & 0x0f]);
}
}

return sb.toString();
}

public static boolean checkSDMounted() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}

public static int getSDSize() {
String path = Environment.getExternalStorageDirectory().getPath();
final StatFs s = new StatFs(path);
int block = s.getAvailableBlocks();
return s.getBlockSize() * block;
}

public static <T extends Serializable> void saveObject(T t, String path) {

if (!TextUtils.isEmpty(path)) {
ObjectOutputStream oos = null;
final File file = new File(path);
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(t);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

public static <T extends Serializable> T getObject(String path) {
if (!TextUtils.isEmpty(path)) {
ObjectInputStream ois = null;
final File file = new File(path);
if (file.exists()) {
try {
ois = new ObjectInputStream(new FileInputStream(file));
return (T) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}

}

CacheItem.java

public class CacheItem<T extends Serializable> implements Serializable {
private String key;//key
private T data;//缓存的数据
private long time;//时间戳

public CacheItem(String key, T data, long time) {
this.key = key;
this.data = data;
this.time = time;
}

public String getKey() {
return key;
}

public T getData() {
return data;
}

public long getTime() {
return time;
}
}