您的位置:首页 > 其它

异步缓存,异步加载

2016-07-21 18:03 267 查看
CacheManager.java  管理类

public class CacheManager {

// wifi缓存时间30分钟
private static long wifi_cache_time = 120 * 60 * 1000;
// 其他网络环境为1小时
private static long other_cache_time = 120 * 60 * 1000;

/**
* 保存对象
*
* @param ser
* @param file
* @throws IOException
*/
public static boolean saveObject(Context context, Serializable ser, String file) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = context.openFileOutput(file, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(ser);
oos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
oos.close();
} catch (Exception e) {
}
try {
fos.close();
} catch (Exception e) {
}
}
}

/**
* 读取对象
*
* @param file
* @return
* @throws IOException
*/
public static Serializable readObject(Context context, String file) {
if (!isExistDataCache(context, file))
return null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = context.openFileInput(file);
ois = new ObjectInputStream(fis);
return (Serializable) ois.readObject();
} catch (FileNotFoundException e) {
} catch (Exception e) {
e.printStackTrace();
// 反序列化失败 - 删除缓存文件
if (e instanceof InvalidClassException) {
File data = context.getFileStreamPath(file);
data.delete();
}
} finally {
try {
ois.close();
} catch (Exception e) {
}
try {
fis.close();
} catch (Exception e) {
}
}
return null;
}

/**
* 判断缓存是否存在
*
* @param cachefile
* @return
*/
public static boolean isExistDataCache(Context context, String cachefile) {
if (context == null)
return false;
boolean exist = false;
File data = context.getFileStreamPath(cachefile);
if (data.exists())
exist = true;
return exist;
}

/**
* 判断缓存是否已经失效
*/
public static boolean isCacheDataFailure(Context context, String cachefile) {
if (context == null) {
return false;
}
File data = context.getFileStreamPath(cachefile);
if (!data.exists()) {
return false;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
Date date2 = new Date(data.lastModified());
Date date1 = new Date(System.currentTimeMillis());
long existTime = System.currentTimeMillis() - data.lastModified();
boolean failure = false;
MZLog.d("Runtimes", "---isCacheDataFailure--data1=="+formatter.format(date1)+"---data2==="+formatter.format(date2));
if (NetWorkUtils.isWifiConnected(context)) {
failure = existTime > wifi_cache_time ? true : false;
} else {
failure = existTime > other_cache_time ? true : false;
}
return failure;
}
}
异步保存缓存任务 SaveCacheTask

private class SaveCacheTask extends AsyncTask<Void, Void, Boolean> {
private WeakReference<Context> mContext;
private Serializable mSeri;
private String mKey;

private SaveCacheTask(Context context, Serializable seri, String key) {
//MZLog.d("Runtimes", "---------SaveCacheTask---------SaveCacheTask--缓存主框架数据key======");
mContext = new WeakReference<Context>(context);
this.mSeri = seri;
this.mKey = key;
}

@Override
protected Boolean doInBackground(Void... params) {
return CacheManager.saveObject(mContext.get(), mSeri, mKey);
}

@Override
protected void onPostExecute(Boolean isCacheSuccess) {
//MZLog.d(TAG, "----Runtime-------key::"+mKey+" cache sucess? "+isCacheSuccess);
}
}
异步加载缓存数据CacheTask
private static class CacheTask extends AsyncTask<String, Void, Serializable> {

private final WeakReference<Context> mContext;
private GetCacheDataListener mListener;
private String mKey;

public CacheTask(Context ctx,String key, GetCacheDataListener listener) {
this.mContext = new WeakReference<Context>(ctx);
this.mListener = listener;
this.mKey = key;
}

@Override
protected Serializable doInBackground(String... params) {
Serializable seri = CacheManager.readObject(mContext.get(), mKey);
if (seri == null) {
return null;
} else {
return seri;
}
}

@Override
protected void onPostExecute(Serializable seri) {
if (seri == null) {
mListener.onFail();
}else{
if (mKey!= null && mKey.contains(BOOKSTORE_MODULE_CACHE_KEY_PREFIX)) {
Map<String, Serializable> muduleMap = new HashMap<String, Serializable>();
muduleMap.put(mKey, (BookStoreModuleBookListEntity)seri);
mListener.onSuccess(muduleMap);
}else{
mListener.onSuccess(seri);
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: