转载麻烦声明出处:https://cloud.tencent.com/developer/user/1148436/activities
目录:
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,整页代码
内部注释丰富,相信你决定看得懂,而且,代码量真心少啊,功能强大。
1 package cn.share.bananacloud.common;
2
3 import android.content.Context;
4 import android.util.Log;
5
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.util.ArrayList;
12
13 /**
14 * Created by Administrator(林冠宏) on 2016/1/20.
15 *
16 * 本类 采用泛型 广泛接受 ArrayList 直接做缓存,即是开发中经常用到的 bean;
17 *
18 * 使用注意: 传进来的 ArrayList 所绑定的 种子,一定要是 已经继承 Serializable 接口的;
19 *
20 * 使用文本流做缓存。
21 *
22 */
23
24 public class DataInfoCache {
25
26 /** 定义一些你项目里面的 缓存文件名字 ,自定义,不要也没关系,调用函数再传入也行 */
27
28 public static String QzInfo = "Qz_List_Info";
29 public static String CyInfo = "Cy_List_Info";
30 private static String DataCache = "Data_Cache_File";
31
32 /**
33 * 保存 一组 数据
34 * @param ctx 上下文
35 * @param data 种子
36 * @param cacheName 缓存文件名
37 */
38 public static<T> void saveListCache(Context ctx, ArrayList<T> data,String cacheName) {
39 new DataCache<T>().saveGlobal(ctx, data, cacheName);
40 }
41
42 /**
43 * 直接根据 缓存文件名获取
44 * */
45 public static<T> ArrayList<T> loadListCache(Context ctx,String cacheName) {
46 return new DataCache<T>().loadGlobal(ctx,cacheName);
47 }
48
49 /**
50 * 获取 一组 数据
51 * @param <T> 数据缓存 save or load
52 */
53 static class DataCache<T> {
54 public void save(Context ctx, ArrayList<T> data, String name) {
55 save(ctx, data, name, "");
56 }
57
58 public void saveGlobal(Context ctx, ArrayList<T> data, String name) {
59 save(ctx, data, name, DataCache);
60 }
61
62 private void save(Context ctx, ArrayList<T> data, String name,String folder) {
63 if (ctx == null) {
64 return;
65 }
66 File file;
67 if (!folder.isEmpty()) {
68 File fileDir = new File(ctx.getFilesDir(), folder);
69 if (!fileDir.exists() || !fileDir.isDirectory()) {
70 fileDir.mkdir();
71 }
72 file = new File(fileDir, name);
73 } else {
74 file = new File(ctx.getFilesDir(), name);
75 }
76 if (file.exists()) {
77 file.delete();
78 }
79 Log.d("zzzzz", file.getAbsolutePath());
80 try {
81 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
82 oos.writeObject(data);
83 oos.close();
84 } catch (Exception e) {
85 e.printStackTrace();
86 }
87 }
88
89 public ArrayList<T> load(Context ctx, String name) {
90 return load(ctx, name, "");
91 }
92
93 public ArrayList<T> loadGlobal(Context ctx, String name) {
94 return load(ctx, name, DataCache);
95 }
96
97 private ArrayList<T> load(Context ctx, String name, String folder) {
98 ArrayList<T> data = null;
99
100 File file;
101 if (!folder.isEmpty()) {
102 File fileDir = new File(ctx.getFilesDir(), folder);
103 if (!fileDir.exists() || !fileDir.isDirectory()) {
104 fileDir.mkdir();
105 }
106 file = new File(fileDir, name);
107 } else {
108 file = new File(ctx.getFilesDir(), name);
109 }
110 Log.d("zzzzz","file "+file.getAbsolutePath());
111 if (file.exists()) {
112 try {
113 Log.d("zzzzz","write object");
114 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
115 data = (ArrayList<T>) ois.readObject();
116 ois.close();
117 } catch (Exception e) {
118 Log.d("zzzzz",e.toString());
119 }
120 }
121 if (data == null) { /** 如果没有 */
122 Log.d("zzzzz","data == null");
123 data = new ArrayList<T>();
124 }
125 return data;
126 }
127 }
128 }
打完收工,麻烦点个赞。