• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Redis的Spring客户端使用小结

    发布者: 土豆服务器 | 发布时间: 2025-6-19 12:36| 查看数: 51| 评论数: 0|帖子模式

    前面使用 Jedis 时, 是借助 Jedis 对象中的各种方法来对 Redis 进行操作. 而在 Spring 框架中, 则是通过 StringRedisTemplate 来操作 Redis. 最开始提供的类是 RedisTemplate, StringRedisTemplate 是 RedisTemplate 的子类, 专门用于处理文本数据.

    0. 配置 Spring 的 Redis环境


    (1) 引入 Redis 的 Spring 依赖

    选中 NoSQL 中的 Spring Data Redis (Access+Driver) 依赖.


    (2) 写 Spring 配置文件

    application.yml:
    1. spring:
    2.   data:
    3.     redis:
    4.       host: 127.0.0.1
    5.       port: 8888
    复制代码
    (3) 创建 Controller 类, 并注入 StringRedisTemplate 对象.
    1. @RestController
    2. public class MyController {
    3.     @Autowired
    4.     StringRedisTemplate stringRedisTemplate;
    5. }
    复制代码
    [!NOTE]
    这里的 RedisTemplate 将 Redis 中的命令, 又做了进一步封装, 分成了几个类别 (每个类别操作特定的数据类型)

    • opsForValue(): 专门操作 string 类型.
    • opsForList(): 专门操作 list 类型.
    • opsForSet(): 专门操作 set 类型.
    • opsForHash(): 专门操作 hash 类型.
    • opsForZSet(): 专门操作 zset 类型.
    这样一来, 它提供的一些接口风格和原生的Redis命令就存在一定差异.
    还有一点要注意的是: Spring 并没有封装 Redis 的所有命令 (如 flushAll 就没有封装), 此时我们可以使用 execute 方法来使用 Redis 的原始命令.
    例如:
    1. stringRedisTemplate.execute((RedisConnection connection) -> {
    2.             // execute 要求回调方法中必须写 return 语句,返回个东西
    3.             // 这个回调返回的对象,就会作为 execute 本身的返回值
    4.             connection.flushAll();
    5.             return null;
    6.         });
    7. //这里的RedisConnection对象, 就相当于Jedis里的Jedis对象.
    复制代码
    1.使用 string
    1. @RestController
    2. public class MyController {
    3.     @Autowired
    4.     StringRedisTemplate stringRedisTemplate;

    5.     @GetMapping("/testString")
    6.     public String testString() {
    7.         stringRedisTemplate.opsForValue().set("key", "111");
    8.         stringRedisTemplate.opsForValue().set("key2", "222");
    9.         stringRedisTemplate.opsForValue().set("key3", "333");

    10.         String value = stringRedisTemplate.opsForValue().get("key");
    11.         System.out.println("value: " + value);

    12.         return "OK";
    13.     }
    14. }
    复制代码

    • 请求结果:
    postman:

    日志:


    2. 使用 list
    1.     @GetMapping("/testList")
    2.     public String testList() {
    3.         // 先清除之前的数据
    4.         tringRedisTemplate.execute((RedisConnection connection) -> {
    5.             // execute 要求回调方法中必须写 return 语句,返回个东西
    6.             // 这个回调返回的对象,就会作为 execute 本身的返回值
    7.             connection.flushAll();
    8.             return null;
    9.         });

    10.         stringRedisTemplate.opsForList().leftPush("key", "111");
    11.         stringRedisTemplate.opsForList().leftPush("key", "222");
    12.         stringRedisTemplate.opsForList().leftPush("key", "333");

    13.         String value = stringRedisTemplate.opsForList().rightPop("key");
    14.         System.out.println("value: " + value);
    15.         value = stringRedisTemplate.opsForList().rightPop("key");
    16.         System.out.println("value: " + value);
    17.         value = stringRedisTemplate.opsForList().rightPop("key");
    18.         System.out.println("value: " + value);

    19.         return "OK";
    20.     }
    复制代码
    运行结果:


    3. 使用 set
    1.     @GetMapping("/testSet")
    2.     public String testSet() {
    3.         stringRedisTemplate.execute((RedisConnection connection) -> {
    4.             connection.flushAll();
    5.             return null;
    6.         });

    7.         stringRedisTemplate.opsForSet().add("key", "111", "222", "333");
    8.         Set<String> result = stringRedisTemplate.opsForSet().members("key");
    9.         System.out.println("result: " + result);

    10.         Boolean exists = stringRedisTemplate.opsForSet().isMember("key", "111");
    11.         System.out.println("exists: " + exists);

    12.         Long count = stringRedisTemplate.opsForSet().size("key");
    13.         System.out.println("count: " + count);

    14.         stringRedisTemplate.opsForSet().remove("key", "111", "222");
    15.         result = stringRedisTemplate.opsForSet().members("key");
    16.         System.out.println("result: " + result);

    17.         return "OK";
    18.     }
    复制代码
    运行结果:


    4. 使用 Hash
    1.     @GetMapping("/testHash")
    2.     public String testHash() {
    3.         stringRedisTemplate.execute((RedisConnection connection) -> {
    4.             connection.flushAll();
    5.             return null;
    6.         });

    7.         stringRedisTemplate.opsForHash().put("key", "f1", "111");
    8.         stringRedisTemplate.opsForHash().put("key", "f2", "222");
    9.         stringRedisTemplate.opsForHash().put("key", "f3", "333");

    10.         String value = (String) stringRedisTemplate.opsForHash().get("key", "f1");
    11.         System.out.println("value: " + value);

    12.         Boolean exists = stringRedisTemplate.opsForHash().hasKey("key", "f1");
    13.         System.out.println("exists: " + exists);

    14.         stringRedisTemplate.opsForHash().delete("key", "f1", "f2");

    15.         Long size = stringRedisTemplate.opsForHash().size("key");
    16.         System.out.println("size: " + size);

    17.         return "OK";
    18.     }
    复制代码
    运行结果:


    5. 使用 zset
    1.     @GetMapping("/testZSet")
    2.     public String testZSet() {
    3.         stringRedisTemplate.execute((RedisConnection connection) -> {
    4.             connection.flushAll();
    5.             return null;
    6.         });

    7.         stringRedisTemplate.opsForZSet().add("key", "zhangsan", 10D);
    8.         stringRedisTemplate.opsForZSet().add("key", "lisi", 20D);
    9.         stringRedisTemplate.opsForZSet().add("key", "wangwu", 30D);

    10.         Set<String> members = stringRedisTemplate.opsForZSet().range("key", 0, -1);
    11.         System.out.println("members: " + members);

    12.         Set<ZSetOperations.TypedTuple<String>> membersWithScore = stringRedisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
    13.         System.out.println("membersWithScore: " + membersWithScore);

    14.         Double score = stringRedisTemplate.opsForZSet().score("key", "zhangsan");
    15.         System.out.println("score: " + score);

    16.         stringRedisTemplate.opsForZSet().remove("key", "zhangsan");

    17.         Long size = stringRedisTemplate.opsForZSet().size("key");
    18.         System.out.println("size: " + size);

    19.         Long rank = stringRedisTemplate.opsForZSet().rank("key", "lisi");
    20.         System.out.println("rank: " + rank);

    21.         return "OK";
    22.     }
    复制代码
    运行结果:

    到此这篇关于Redis的Spring客户端使用小结的文章就介绍到这了,更多相关Redis Spring客户端使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/database/339077jj5.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表