Redis與fastjson實現儲存與讀取
阿新 • • 發佈:2019-01-23
1、陣列list
Redis結合fastjson儲存
List<Home> home = new ArrayList<Home>();
String key = "redisKey";
redisUtil.set(key, JSON.toJSONString(home));
Redis結合fastjson讀取
String key = "redisKey"; Object value = redisUtil.get(key); List<Home> home = JSON.parseArray((String)value, Home.class);
2、HashMap
Redis結合fastjson儲存
Map<Integer, List<ItemsToTemplate>> itemMap = new HashMap<Integer, List<ItemsToTemplate>>();
String key = "redisKey";
redisUtil.set(key, JSON.toJSONString(itemMap));
Redis結合fastjson讀取
String key = "redisKey"; String value = redisUtil.getStr(key); Map<Integer, List<ItemsToTemplate>> itemMap = JSON.parseObject(value,new TypeReference<Map<Integer,List<ItemsToTemplate>>>(){}.getType());