StringRedisTemplate 通过字符串存取 对 List、Map、对象的操作需要引用com.alibaba.fastjson.TypeReference工具类

一、对单一对象的操作

@Autowired
StringRedisTemplate stringRedisTemplate;

//需要获取的对象
StandardGoodsCity standardGoodsCity = new StandardGoodsCity();
//保存的key名
String key = "city:store:goodsId";
if (stringRedisTemplate.hasKey(key)) {  //缓存中存在,从缓存中获取并转换为自己需要的对象
    standardGoodsCity = JSON.parseObject(stringRedisTemplate.opsForValue().get(key),new TypeReference<StandardGoodsCity>(){});
} else {  //缓存中不存在,先从数据库中获取,然后保存到缓存中
    standardGoodsCity.setId(3L);
    standardGoodsCity.setRegionId(333L);
    standardGoodsCity.setGoodsId(3L);
	standardGoodsCity.setContext("");
	//保存到缓存中,并设置过期时间
    stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(standardGoodsCity), 60 * 10, TimeUnit.SECONDS);
}

二、对List对象集合的操作

@Autowired
StringRedisTemplate stringRedisTemplate;

//需要获取的List对象集合
List<StandardGoodsCity> standardGoodsCityList = Lists.newLinkedList();
//保存的key名
String key = "city:store:goodsId";
if (stringRedisTemplate.hasKey(key)) {  //缓存中存在,从缓存中获取并转换为自己需要的对象
    standardGoodsCityList = JSON.parseObject(stringRedisTemplate.opsForValue().get(key),new TypeReference<List<StandardGoodsCity>>(){});
} else {  //缓存中不存在,先从数据库中获取,然后保存到缓存中
	StandardGoodsCity standardGoodsCity1 = new StandardGoodsCity();
    standardGoodsCity1.setId(1L);
    standardGoodsCity1.setRegionId(111L);
    standardGoodsCity1.setGoodsId(1L);
	standardGoodsCity1.setContext("");
	
	StandardGoodsCity standardGoodsCity3 = new StandardGoodsCity();
    standardGoodsCity3.setId(3L);
    standardGoodsCity3.setRegionId(333L);
    standardGoodsCity3.setGoodsId(3L);
	standardGoodsCity3.setContext("");
	//List
	standardGoodsCityList.add(standardGoodsCity1);
    standardGoodsCityList.add(standardGoodsCity3);
	//保存到缓存中,并设置过期时间
    stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(standardGoodsCityList), 60 * 10, TimeUnit.SECONDS);
}

三、对Map对象的操作

@Autowired
StringRedisTemplate stringRedisTemplate;

//需要获取的Map对象集合
Map<Long, StandardGoodsCity> standardGoodsCityMap = Maps.newLinkedHashMap();
//保存的key名
String key = "city:store:goodsId";
if (stringRedisTemplate.hasKey(key)) {  //缓存中存在,从缓存中获取并转换为自己需要的对象
    standardGoodsCityMap = JSON.parseObject(stringRedisTemplate.opsForValue().get(key),new TypeReference<Map<Long,StandardGoodsCity>>(){});
} else {  //缓存中不存在,先从数据库中获取,然后保存到缓存中
	StandardGoodsCity standardGoodsCity1 = new StandardGoodsCity();
    standardGoodsCity1.setId(1L);
    standardGoodsCity1.setRegionId(111L);
    standardGoodsCity1.setGoodsId(1L);
	standardGoodsCity1.setContext("");
	
	StandardGoodsCity standardGoodsCity3 = new StandardGoodsCity();
    standardGoodsCity3.setId(3L);
    standardGoodsCity3.setRegionId(333L);
    standardGoodsCity3.setGoodsId(3L);
	standardGoodsCity3.setContext("");
	
	//Map
	standardGoodsCityMap.put(standardGoodsCity1.getId(),standardGoodsCity1);
    standardGoodsCityMap.put(standardGoodsCity3.getId(),standardGoodsCity3);
	//保存到缓存中,并设置过期时间
    stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(standardGoodsCityMap), 60 * 10, TimeUnit.SECONDS);
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐