异步缓存,异步加载
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);
}
}
}
}
}
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);
}
}
}
}
}
相关文章推荐
- 小问题大脑洞
- [算法学习笔记]冒泡排序和选择排序
- WebStorm的基本配置
- CodeForces 615A Bulbs (水。)
- jasmine单元测试
- 日历中的星期一问题
- 从线程返回信息
- UDP 多线程 通讯协议
- Java 程序员应会的 Linux 命令
- MySQL crash-safe replication
- 未关闭InputStream 引起的血案
- Android基础之Activity四种启动模式
- java中的 private Logger log=Logger.getLogger(this.getClass());
- c++学习笔记(十一):C++类的访问修饰符
- 数据结构实验之数组一:矩阵转置
- JavaScript的垃圾回收
- shell初步学习
- sql
- CodeForces 597A Divisibility
- Android 接入微信支付