当前位置:   article > 正文

StringRedisTemplate 通过opsForValue()对List Map 对象的保存与获取实操_redistemplate保存list和map集合

redistemplate保存list和map集合

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

二、对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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

三、对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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/511110
推荐阅读
相关标签
  

闽ICP备14008679号