1. 程式人生 > >Redis與fastjson實現儲存與讀取

Redis與fastjson實現儲存與讀取

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());