SpringBoot依赖之Spring Data Redis 一 List 类型

概念

Spring Data Redis (Access+Driver)
  • 依赖名称: Spring Data Redis (Access+Driver)
  • 功能描述: Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.
  • 中文释义:用于同步、异步和反应式使用的高级且线程安全的 Java Redis 客户端。支持集群、哨兵、管道、自动重新连接、编解码器等。

项目学习代码地址

操作演示:

在IDEA中创建项目过程可以参考上一篇:
SpringBoot依赖之Spring Data Redis 一 String类型

Spring Boot 项目中使用 Spring Data Redis 实现列表list

接下来我们演示在 Spring Boot 项目中使用 Spring Data Redis 实现列表(List)操作,我们可以在之前的项目代码基础上扩展 Redis 服务类和控制器类,以支持对 Redis 列表的常见操作。以下是具体的实现步骤。

1. 更新 Redis 服务类

RedisService 类中添加列表相关的方法。

java 复制代码
package com.dependencies.springdataredis;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisService {

    private final RedisTemplate<String, Object> redisTemplate;
    private final ListOperations<String, Object> listOperations;

    public RedisService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.listOperations = redisTemplate.opsForList();
    }

    // 向列表左侧推入值
    public void pushToListLeft(String key, String value) {
        listOperations.leftPush(key, value);
    }

    // 向列表右侧推入值
    public void pushToListRight(String key, String value) {
        listOperations.rightPush(key, value);
    }

    // 从列表左侧弹出值
    public String popFromListLeft(String key) {
        return (String) listOperations.leftPop(key);
    }

    // 从列表右侧弹出值
    public String popFromListRight(String key) {
        return (String) listOperations.rightPop(key);
    }

    // 获取列表中所有值
    public List<Object> getList(String key) {
        return listOperations.range(key, 0, -1);
    }
}

2. 更新控制器类

RedisController 中添加处理列表操作的端点。

java 复制代码
package com.dependencies.springdataredis;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhizhou   2024/8/17 12:02
 */
@RestController
public class RedisController {

    private final RedisService redisService;

    public RedisController(RedisService redisService) {
        this.redisService = redisService;
    }

    // 向列表左侧推入值
    @GetMapping("/list/leftpush")
    public String leftPushToList(@RequestParam String key, @RequestParam String value) {
        redisService.pushToListLeft(key, value);
        return "值被推到列表的左侧";
    }

    // 向列表右侧推入值
    @GetMapping("/list/rightpush")
    public String rightPushToList(@RequestParam String key, @RequestParam String value) {
        redisService.pushToListRight(key, value);
      	return "值被推到列表的右侧";
    }

    // 从列表左侧弹出值
    @GetMapping("/list/leftpop")
    public String leftPopFromList(@RequestParam String key) {
        return redisService.popFromListLeft(key);
    }

    // 从列表右侧弹出值
    @GetMapping("/list/rightpop")
    public String rightPopFromList(@RequestParam String key) {
        return redisService.popFromListRight(key);
    }

    // 获取列表中所有值
    @GetMapping("/list/getall")
    public List<Object> getAllFromList(@RequestParam String key) {
        return redisService.getList(key);
    }
}

3. 验证测试list相关操作

我们启动项目以后,就通过以下的额 URL 测试 Redis 列表的功能:

  • 向列表左侧PUSH推入值 : http://localhost:8080/list/leftpush?key=oneList&value=value1

    • 这会将 value1 推入到列表 myList 的左侧。
  • 向列表右侧PUSH推入值 : http://localhost:8080/list/rightpush?key=oneList&value=value2

    • 这会将 value2 推入到列表 myList 的右侧。
  • 从列表左侧POP弹出值 : http://localhost:8080/list/leftpop?key=oneList

    • 这会从列表 myList 的左侧弹出一个值。
  • 从列表右侧POP弹出值 : http://localhost:8080/list/rightpop?key=oneList

    • 这会从列表 myList 的右侧弹出一个值。
  • 获取列表中所有值 : http://localhost:8080/list/getall?key=oneList

    • 这会获取列表 myList 中的所有值。

4. 总结

​ 通过上述步骤,我们成功在 Spring Boot 项目中使用 Spring Data Redis 实现了对 Redis 列表类型的操作。您可以使用这些方法来处理复杂的队列、栈或其他基于列表的数据结构需求。

​ 这是基本的list相关操作,还是要结合具体的业务选择具体的存储类型,这样对于项目后期的迭代具有事半功倍的效果。所以项目前期的技术选型也很重要,不容忽视。

可以关注我一起学习!一起为程序员职业生涯蓄能!

相关推荐
二进制独立开发8 分钟前
[Trae 04.22+]适用于JAVA Spring开发的智能体提示词
spring·trae
特立独行的猫a12 分钟前
redis客户端库redis++在嵌入式Linux下的交叉编译及使用
linux·数据库·c++·redis·redis客户端库
A阳俊yi1 小时前
Spring Boot日志配置
java·spring boot·后端
苹果酱05671 小时前
2020-06-23 暑期学习日更计划(机器学习入门之路(资源汇总)+概率论)
java·vue.js·spring boot·mysql·课程设计
斜月1 小时前
一个服务预约系统该如何设计?
spring boot·后端
大家都说我身材好1 小时前
Spring缓存注解深度实战:3大核心注解解锁高并发系统性能优化‌
spring·缓存·性能优化
爱吃泡芙的小白白1 小时前
爬虫学习——使用HTTP服务代理、redis使用、通过Scrapy实现分布式爬取
redis·分布式·爬虫·http代理·学习记录
都叫我大帅哥2 小时前
Spring AI中的ChatClient:从入门到精通,一篇搞定!
java·spring·ai编程
都叫我大帅哥2 小时前
《@SpringBootApplication:Spring Boot的"一键启动"按钮,还是程序员的"免死金牌"?》
java·后端·spring
Java水解2 小时前
线程池详解:在SpringBoot中的最佳实践
spring boot·后端