redis存取list集合

一 、存取为list类型

  1. @RestController

  2. @RequestMapping("/test")

  3. @Slf4j

  4. publicclassTestController

  5. @Autowired

  6. private RedisTemplate redisTemplate;

  7. @ApiOperation("redis-savelist")

  8. @PostMapping("/redis/save/list")

  9. public void redisSaveList() {

  10. Listlist = getPersonList();

  11. //清空

  12. while (redisTemplate.opsForList().size("oowwoo") >0){

  13. redisTemplate.opsForList().leftPop("oowwoo");

  14. }

  15. //存储

  16. redisTemplate.opsForList().rightPushAll("oowwoo",list);

  17. //取出

  18. List oowwoo = redisTemplate.opsForList().range("oowwoo",0,-1);

  19. log.info(">>>>>>>>>>>>>>>list = {}", oowwoo.toString());

  20. Iterator it = oowwoo.iterator();

  21. while(it.hasNext()){

  22. Person p = it.next();

  23. log.info("person = {}", p.toString());

  24. }

  25. }

  26. privateList getPersonList() {

  27. Person p1 =new Person();

  28. p1.setId(1L);

  29. p1.setName("张一");

  30. p1.setAge(11);

  31. Person p2 =new Person();

  32. p2.setId(2L);

  33. p2.setName("张二");

  34. p2.setAge(22);

  35. Person p3 =new Person();

  36. p3.setId(3L);

  37. p3.setName("张三");

  38. p3.setAge(33);

  39. Listlist =new ArrayList<>();

  40. list.add(p1);

  41. list.add(p2);

  42. list.add(p3);

  43. returnlist;

  44. }

  45. }

二 、将list转为json对象存取

  1. import com.alibaba.fastjson.JSON;

  2. import com.alibaba.fastjson.JSONObject;

  3. @Autowired

  4. private StringRedisTemplate stringRedisTemplate;

  5. //存

  6. List businessIdList = eeFreecarriageShopService.selectBusinessIdInPromotion();

  7. stringRedisTemplate.opsForValue().set(RedisConstants.FREECARRIAGE_BUSINESSIDLIST, JSON.toJSON(businessIdList).toString());

  8. //取

  9. String businessJsonArray = stringRedisTemplate.opsForValue().get(RedisConstants.FREECARRIAGE_BUSINESSIDLIST);

  10. List businessIdList = JSONObject.parseArray(businessJsonArray,Long.class);

java 用redisTemplate 的 Operations存取list集合

相关推荐
我不会起名字3223 小时前
Redis 从 0 到 1(十七):订单 30 分钟不支付自动关闭——ZSet 延迟队列 Go 实现
redis·go·延迟队列·zset·订单
视觉小萌新4 小时前
AI Class 3 · Chapter 1:变量、字符串、list/dict、if/for(python学习-小白版-有经验可跳过)
python·学习·list
利威尔xu4 小时前
Go IM 源码实战(13):30 个会话只有 2 个红点,未读数错在三条路径
redis·后端·微服务·面试·golang
will_net6 小时前
Redis集群部署+Redis 链接地址放入Aollo +c#项目接入Redis
redis·c#·bootstrap
共享家95276 小时前
408 操作系统第一章
服务器·数据库·windows
用户6919026813398 小时前
Redis 1:简述Redis概念与Agent的联系
redis·后端·设计
Sakura_j_h_8 小时前
GitHub Actions 打包 Windows exe:中文编码与产物上传
windows·ci/cd·打包发布·github·pyinstaller·github actions
小南家的青蛙9 小时前
华为云DCS全形态原生Redis命令兼容性测试方案
redis·华为云·兼容性·兼容性测试·dcs
xiaoqiMikko10 小时前
Redis 实战(三):持久化出错 —— fork 失败、磁盘写满、AOF 膨胀与损坏,逐个复现(6.2.4)
redis·后端