• 14.Redis之JAVASpring客户端


    1.引入依赖

    此时就会引入操作 redis 的依赖了~~ 

    2.yml配置

    1. spring:
    2. redis:
    3. host: 127.0.0.1
    4. port: 8888

    3.准备

    • 前面使用 jedis,是通过 Jedis 对象里的各种方法来操作 redis 的.
    • 此处Spring 中则是通过 StringRedisTemplate 来操作 redis .
    • 最原始提供的类是 RedisTemplate
    • StringRedisTemplate 是 RedisTemplate 的子类, 专门用来处理 文本数据的
    • 这个类提供的方法,相比于之前的 Jedis 中的各种方法,还是存在较大的差异的!!

    4.代码 

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.data.redis.connection.RedisConnection;
    3. import org.springframework.data.redis.core.StringRedisTemplate;
    4. import org.springframework.data.redis.core.ZSetOperations;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import java.util.Set;
    9. // 后续 redis 测试的各种方法, 都通过这个 Controller 提供的 http 接口来触发.
    10. @RestController
    11. public class MyController {
    12. @Autowired
    13. private StringRedisTemplate redisTemplate;
    14. @GetMapping("/testString")
    15. @ResponseBody
    16. public String testString() {
    17. redisTemplate.execute((RedisConnection connection) -> {
    18. // execute 要求回调方法中必须写 return 语句. 返回个东西.
    19. // 这个回调返回的对象, 就会作为 execute 本身的返回值.
    20. connection.flushAll();
    21. return null;
    22. });
    23. redisTemplate.opsForValue().set("key", "111");
    24. redisTemplate.opsForValue().set("key2", "222");
    25. redisTemplate.opsForValue().set("key3", "333");
    26. String value = redisTemplate.opsForValue().get("key");
    27. System.out.println("value: " + value);
    28. return "OK";
    29. }
    30. @GetMapping("/testList")
    31. @ResponseBody
    32. public String testList() {
    33. // 先清除之前的数据.
    34. redisTemplate.execute((RedisConnection connection) -> {
    35. // execute 要求回调方法中必须写 return 语句. 返回个东西.
    36. // 这个回调返回的对象, 就会作为 execute 本身的返回值.
    37. connection.flushAll();
    38. return null;
    39. });
    40. redisTemplate.opsForList().leftPush("key", "111");
    41. redisTemplate.opsForList().leftPush("key", "222");
    42. redisTemplate.opsForList().leftPush("key", "333");
    43. String value = redisTemplate.opsForList().rightPop("key");
    44. System.out.println("value: " + value);
    45. value = redisTemplate.opsForList().rightPop("key");
    46. System.out.println("value: " + value);
    47. value = redisTemplate.opsForList().rightPop("key");
    48. System.out.println("value: " + value);
    49. return "OK";
    50. }
    51. @GetMapping("/testSet")
    52. @ResponseBody
    53. public String testSet() {
    54. redisTemplate.execute((RedisConnection connection) -> {
    55. connection.flushAll();
    56. return null;
    57. });
    58. redisTemplate.opsForSet().add("key", "111", "222", "333");
    59. Set<String> result = redisTemplate.opsForSet().members("key");
    60. System.out.println("result: " + result);
    61. Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
    62. System.out.println("exists: " + exists);
    63. Long count = redisTemplate.opsForSet().size("key");
    64. System.out.println("count: " + count);
    65. redisTemplate.opsForSet().remove("key", "111", "222");
    66. result = redisTemplate.opsForSet().members("key");
    67. System.out.println("result: " + result);
    68. return "OK";
    69. }
    70. @GetMapping("/testHash")
    71. @ResponseBody
    72. public String testHash() {
    73. redisTemplate.execute((RedisConnection connection) -> {
    74. connection.flushAll();
    75. return null;
    76. });
    77. redisTemplate.opsForHash().put("key", "f1", "111");
    78. redisTemplate.opsForHash().put("key", "f2", "222");
    79. redisTemplate.opsForHash().put("key", "f3", "333");
    80. String value = (String) redisTemplate.opsForHash().get("key", "f1");
    81. System.out.println("value: " + value);
    82. Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
    83. System.out.println("exists: " + exists);
    84. redisTemplate.opsForHash().delete("key", "f1", "f2");
    85. Long size = redisTemplate.opsForHash().size("key");
    86. System.out.println("size: " + size);
    87. return "OK";
    88. }
    89. @GetMapping("/testZSet")
    90. @ResponseBody
    91. public String testZSet() {
    92. redisTemplate.execute((RedisConnection connection) -> {
    93. connection.flushAll();
    94. return null;
    95. });
    96. redisTemplate.opsForZSet().add("key", "zhangsan", 10);
    97. redisTemplate.opsForZSet().add("key", "lisi", 20);
    98. redisTemplate.opsForZSet().add("key", "wangwu", 30);
    99. Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
    100. System.out.println("members: " + members);
    101. Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
    102. System.out.println("membersWithScore: " + membersWithScore);
    103. Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
    104. System.out.println("score: " + score);
    105. redisTemplate.opsForZSet().remove("key", "zhangsan");
    106. Long size = redisTemplate.opsForZSet().size("key");
    107. System.out.println("size: " + size);
    108. Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
    109. System.out.println("rank: " + rank);
    110. return "OK";
    111. }
    112. }

    5.测试

    Spring Data Redis 中文文档 (springdoc.cn)

  • 相关阅读:
    淘宝企业的定价是怎么来的呢淘宝企业店铺转让价格有哪些因素?
    FRP内网穿透教程
    e-table,表格单元格合并
    Android OTA差分包制作(RK平台)
    【echarts】06、echarts+vue2 - 雷达图
    Java 异常
    企业风险管理策略终极指南
    招股书写了“元宇宙“318次!飞天云动再战港股“元宇宙第一股“
    海洋cms新手入门安装配置教程
    基于rsync daemon 实现 sersync——sersync实现实时数据同步
  • 原文地址:https://blog.csdn.net/m0_47017197/article/details/139273597