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集合

相关推荐
Uncertainty!!16 小时前
Windows 10 与 Ubuntu 22.04 通过 Samba 共享文件夹
windows·ubuntu·共享文件夹
imDwAaY17 小时前
操作系统如何管理程序运行?从线程、进程到内存隔离与用户态切换:CS162 Note2
android·java·redis
百事牛科技18 小时前
如何创建7Z压缩包?Windows下7-Zip操作方法
windows·7-zip
码途行者18 小时前
处理超时未支付订单:两套轻量化方案,附 PHP 极简实现
开发语言·redis·php
lojox18 小时前
C++ 手写 STL list(双向循环链表)原理剖析与实现
c++·链表·list
池央19 小时前
Windows本地部署Qwen3.8实战:LM Studio、Cherry Studio、远程API与Token鉴权
windows
草莓熊Lotso1 天前
【C++ 基于微服务的即时通讯系统】etcd + brpc 从安装到服务注册发现全链路实战
网络·数据库·c++·redis·微服务·etcd
我不会起名字3221 天前
Redis 突然只能读不能写:MISCONF 报错的 3 类触发条件与 4 步恢复
运维·redis·持久化·排查·misconf
我不会起名字3221 天前
Redis 从 0 到 1(九):主库宕机 30 秒自动切主——1 主 2 从 3 哨兵 + Go 客户端
redis·docker·sentinel·高可用·哨兵