前言
最近在使用mmkv缓存的时候 发现没有集合缓存
非常不方便 自己写一个方法
MMKV
public class MmkvUtils {private MmkvUtils() {throw new UnsupportedOperationException("u can't instantiate me...");}public static void init() {MMKV.initialize(LeoUtils.getApplication());}/*默认表名put值*/public static void put(String key, Object object) {MMKV mmkv = MMKV.defaultMMKV();if (object instanceof String) {mmkv.encode(key, (String) object);} else if (object instanceof Integer) {mmkv.encode(key, (Integer) object);} else if (object instanceof Boolean) {mmkv.encode(key, (Boolean) object);} else if (object instanceof Float) {mmkv.encode(key, (Float) object);} else if (object instanceof Long) {mmkv.encode(key, (Long) object);} else {mmkv.encode(key, object.toString());}}/*默认表名获取值*/public static Object get(String key, Object defaultObject) {MMKV mmkv = MMKV.defaultMMKV();if (defaultObject instanceof String) {return mmkv.decodeString(key, (String) defaultObject);} else if (defaultObject instanceof Integer) {return mmkv.decodeInt(key, (Integer) defaultObject);} else if (defaultObject instanceof Boolean) {return mmkv.decodeBool(key, (Boolean) defaultObject);} else if (defaultObject instanceof Float) {return mmkv.decodeFloat(key, (Float) defaultObject);} else if (defaultObject instanceof Long) {return mmkv.decodeLong(key, (Long) defaultObject);}return null;}/*默认表名移除值*/public static void remove(String key) {MMKV mmkv = MMKV.defaultMMKV();mmkv.removeValueForKey(key);}/*默认表名判断当前值是否存在*/public static boolean contains(String key) {MMKV mmkv = MMKV.defaultMMKV();return mmkv.contains(key);}/*默认表明用来保存类*/public static <T extends Serializable> boolean putByClass(String key, T entity) {if (entity == null) {return false;}MMKV mmkv = MMKV.defaultMMKV();String json = GsonUtil.ser(entity);return mmkv.encode(key, json);}/*默认表明用来保存类1、移除和查询同普通数据类型*/public static <T extends Serializable> T getByClass(String key, Class<T> clazz) {MMKV mmkv = MMKV.defaultMMKV();String json = mmkv.decodeString(key,null);if (json == null) {return null;}return GsonUtil.deser(json, clazz);}/*如果不想用默认表明,也可以用如下方式,自定义个MMKV kv = MMKV.mmkvWithID("MyID");*//**用途:增改sp的数组 name-自定义集合的名称*/public static <T> Boolean putArray( List<T> list, String name) {MMKV kv = MMKV.defaultMMKV();if (list == null || list.size() == 0) { //清空kv.putInt(name + "size", 0);int size = kv.getInt(name + "size", 0);for (int i = 0; i < size; i++) {if (kv.getString(name + i, null) != null) {kv.remove(name + i);}}} else {kv.putInt(name + "size", list.size());if (list.size() > 20) {list.remove(0); //只保留后20条记录}for (int i = 0; i < list.size(); i++) {kv.remove(name + i);kv.remove(new Gson().toJson(list.get(i)));//删除重复数据 先删后加kv.putString(name + i, new Gson().toJson(list.get(i)));}}return kv.commit();}/**用途:加载sp的数组 name-自定义集合的名称*/public static <T> ArrayList<T> getArray( String name, T bean) {MMKV kv = MMKV.defaultMMKV();ArrayList<T> list = new ArrayList<T>();int size = kv.getInt(name + "size", 0);for (int i = 0; i < size; i++) {if (kv.getString(name + i, null) != null) {try {list.add((T) new Gson().fromJson(kv.getString(name + i, null), bean.getClass()));} catch (Exception e) {e.printStackTrace();}}}return list;}}
使用方法
业务场景 用户读取了谋篇文章 加入缓存
打开历史浏览 可以看到之前的item
MmkvUtils.putArray(cache, "history");ArrayList<HomeBean> history = MmkvUtils.getArray("history", new HomeBean());