首页 技术 正文
技术 2022年11月23日
0 收藏 381 点赞 3,726 浏览 3287 个字

转载麻烦声明出处:http://www.cnblogs.com/linguanh/

目录:

  1,前序

2,作用

   3,特点

4,代码

1,前序

   在开发过程中,client 和 server 数据交流一般用到 json 格式传输数据。缓存机制,是任何一个优秀的 app 都必须存在的,android 的缓存数据的方法很多,无论是文本还是图像,我这里要讲的是我自己 编写 并 一直使用的, DataInfoCache 类。

2,本类作用

   专门存储 ArrayList 种子数据,我举个 例子: List<UserInfo> mInfoBean = new ArrayList<>();  这里的 bean 种子是 UserInfo 类,处理这里信息,我们一般是在接受完 server 的数据,例如 json 后,解析完 json,再通过 setXXX() 函数来存入,再用 getXXX() 获取。 在有网络的情况下,我们可以轻易 获取数据,没有网络呢? 那么我们就应该去获取缓存的。 那么问题就来了,要获取缓存,需要满足条件:

有网络时获取数据 —> 先保存数据到本地,作为缓存;

没网络时             —> 读取本地缓存;

目前到这里来说,本类的作用和 一般的 缓存类 没什么差别,OK,请看第三点。

3,特点

1,代码量少,通俗易懂,连带注释,不到 130 行;

2,存储时,直接 存储 List,读取时,直接读出 List , 直接用,无需 再解析。    下面举个例子

List<UserInfo> mInfoBean = new ArrayList<>();

saveListCache(mInfoBean,”我的缓存”);   /**  存储 */

mInfoBean = loadListCache(“我的缓存”); /** 获取 */

有没有觉得很快,我既不是 存储 json,也不是存储 文本,如果存储的是 json,读取的时候,你就还需要解析了,文本也是。

4,整页代码

内部注释丰富,相信你决定看得懂,而且,代码量真心少啊,功能强大。

 package cn.share.bananacloud.common; import android.content.Context;
import android.util.Log; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList; /**
* Created by Administrator(林冠宏) on 2016/1/20.
*
* 本类 采用泛型 广泛接受 ArrayList 直接做缓存,即是开发中经常用到的 bean;
*
* 使用注意: 传进来的 ArrayList 所绑定的 种子,一定要是 已经继承 Serializable 接口的;
*
* 使用文本流做缓存。
*
*/ public class DataInfoCache { /** 定义一些你项目里面的 缓存文件名字 ,自定义,不要也没关系,调用函数再传入也行 */ public static String QzInfo = "Qz_List_Info";
public static String CyInfo = "Cy_List_Info";
private static String DataCache = "Data_Cache_File"; /**
* 保存 一组 数据
* @param ctx 上下文
* @param data 种子
* @param cacheName 缓存文件名
*/
public static<T> void saveListCache(Context ctx, ArrayList<T> data,String cacheName) {
new DataCache<T>().saveGlobal(ctx, data, cacheName);
} /**
* 直接根据 缓存文件名获取
* */
public static<T> ArrayList<T> loadListCache(Context ctx,String cacheName) {
return new DataCache<T>().loadGlobal(ctx,cacheName);
} /**
* 获取 一组 数据
* @param <T> 数据缓存 save or load
*/
static class DataCache<T> {
public void save(Context ctx, ArrayList<T> data, String name) {
save(ctx, data, name, "");
} public void saveGlobal(Context ctx, ArrayList<T> data, String name) {
save(ctx, data, name, DataCache);
} private void save(Context ctx, ArrayList<T> data, String name,String folder) {
if (ctx == null) {
return;
}
File file;
if (!folder.isEmpty()) {
File fileDir = new File(ctx.getFilesDir(), folder);
if (!fileDir.exists() || !fileDir.isDirectory()) {
fileDir.mkdir();
}
file = new File(fileDir, name);
} else {
file = new File(ctx.getFilesDir(), name);
}
if (file.exists()) {
file.delete();
}
Log.d("zzzzz", file.getAbsolutePath());
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(data);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
} public ArrayList<T> load(Context ctx, String name) {
return load(ctx, name, "");
} public ArrayList<T> loadGlobal(Context ctx, String name) {
return load(ctx, name, DataCache);
} private ArrayList<T> load(Context ctx, String name, String folder) {
ArrayList<T> data = null; File file;
if (!folder.isEmpty()) {
File fileDir = new File(ctx.getFilesDir(), folder);
if (!fileDir.exists() || !fileDir.isDirectory()) {
fileDir.mkdir();
}
file = new File(fileDir, name);
} else {
file = new File(ctx.getFilesDir(), name);
}
Log.d("zzzzz","file "+file.getAbsolutePath());
if (file.exists()) {
try {
Log.d("zzzzz","write object");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
data = (ArrayList<T>) ois.readObject();
ois.close();
} catch (Exception e) {
Log.d("zzzzz",e.toString());
}
}
if (data == null) { /** 如果没有 */
Log.d("zzzzz","data == null");
data = new ArrayList<T>();
}
return data;
}
}
}

打完收工,麻烦点个顶。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,470
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,284
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,100
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,730
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766