当前位置:   article > 正文

SpringBoot Redis的使用

SpringBoot Redis的使用

官方文档:

官方文档:Spring Data Redis :: Spring Data Redis

jedis一样,SpringBoot Redis 也可以让我在Java代码中使用redis,同样也是通过引入maven依赖的形式。

加速访问github:

使用steam++可以免费加速访问github

SpringBoot Redis使用:

SpringBoot Redis:

创建springboot项目:

创建项目时勾选此依赖:

 想在已有的springboot项目使用也可以直接添加依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

 yml文件:

  1. spring:
  2. redis:
  3. host: 127.0.0.1
  4. port: 8888
  5. server:
  6. port:8081

我们写的Java代码,会发送请求到127.0.0.1:8888然后被映射到云服务的端口,server.port:8080是我们springboot项目的访问端口,注意区分这两个端口的意义。

springboot redis代码

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/334982
推荐阅读
相关标签
  

闽ICP备14008679号