在Spring Boot微服务使用ListOperations操作Redis List列表

news/2024/4/20 20:15:29/文章来源:https://blog.csdn.net/zhangbeizhen18/article/details/130119502

记录:402

场景:在Spring Boot微服务使用RedisTemplate的ListOperations操作Redis List列表。

版本:JDK 1.8,Spring Boot 2.6.3,redis-6.2.5

1.微服务中Redis配置信息

1.1在application.yml中Redis配置信息

spring:redis:host: 192.168.19.203port: 28001password: 12345678timeout: 50000

1.2加载简要逻辑

Spring Boot微服务在启动时,自动注解机制会读取application.yml的注入到RedisProperties对象。在Spring环境中就能取到Redis相关配置信息了。

类全称:org.springframework.boot.autoconfigure.data.redis.RedisProperties

1.3在pom.xml添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.配置RedisTemplate

2.1配置RedisTemplate

@Configuration
public class RedisConfig {@Bean("redisTemplate")public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {// 1.创建RedisTemplate对象RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();// 2.加载Redis配置redisTemplate.setConnectionFactory(lettuceConnectionFactory);// 3.配置key序列化RedisSerializer<?> stringRedisSerializer = new StringRedisSerializer();redisTemplate.setKeySerializer(stringRedisSerializer);redisTemplate.setHashKeySerializer(stringRedisSerializer);// 4.配置Value序列化Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper objMapper = new ObjectMapper();objMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objMapper.activateDefaultTyping(objMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objMapper);redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// 5.初始化RedisTemplateredisTemplate.afterPropertiesSet();return redisTemplate;}@Beanpublic ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {return redisTemplate.opsForList();}
}

2.2解析

在配置RedisTemplate后,在Spring环境中,可以@Autowired自动注入方式注入操作Redis对象。比如:RedisTemplate、ListOperations。

3.使用ListOperations操作Redis List列表

3.1简要说明

使用ListOperationsRedis List列表,常用操作:增、查、删、设置超时等。

3.2操作示例

@RestController
@RequestMapping("/hub/example/load")
@Slf4j
public class LoadController {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate ListOperations listOperations;/*** 操作List,使用ListOperations* 对应写命令: LPUSH 队列名称 值* 对应读命令: LPOP 队列名称* 对应写命令: RPUSH 队列名称 值* 对应读命令: RPOP 队列名称*/@GetMapping("/listOperations")public Object loadData03() {log.info("ListOperations操作开始...");// 1.增listOperations.leftPush("CityInfo:Hangzhou03", "杭州");listOperations.rightPush("CityInfo:Hangzhou03", "苏州");// 2.查,查出队列指定范围元素,不会删除队列里面数据,(0,-1)查出全部元素listOperations.leftPush("CityInfo:Hangzhou03", "杭州");listOperations.leftPush("CityInfo:Hangzhou03", "苏州");List cityList = redisTemplate.boundListOps("CityInfo:Hangzhou03").range(0, -1);cityList.forEach((value)->{System.out.println("value="+value);});// 3.取,逐个取出队列元素(取出一个元素后,队列就没有这个元素了)Object city01 = listOperations.leftPop("CityInfo:Hangzhou03");Object city02 = listOperations.rightPop("CityInfo:Hangzhou03");log.info("city01=" + city01 + ",city02=" + city02);// 4.删listOperations.leftPush("CityInfo:Hangzhou03", "杭州");listOperations.leftPush("CityInfo:Hangzhou03", "苏州");redisTemplate.delete("CityInfo:Hangzhou03");// 5.设置超时listOperations.leftPush("CityInfo:Hangzhou03", "上海");redisTemplate.boundValueOps("CityInfo:Hangzhou03").expire(5, TimeUnit.MINUTES);redisTemplate.expire("CityInfo:Hangzhou03", 10, TimeUnit.MINUTES);// 6.查询List的元素个数Long size =listOperations.size("CityInfo:Hangzhou03");System.out.println("查询List的元素个数,size="+size);log.info("ListOperations操作结束...");return "执行成功";}
}

3.3测试验证

使用Postman测试。

请求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/load/listOperations

4.ListOperations接口

4.1接口

ListOperations是一个接口,默认实现类是DefaultListOperations。

接口:org.springframework.data.redis.core.ListOperations。

实现类:org.springframework.data.redis.core.DefaultListOperations。

4.2接口源码

在源码中,查看接口具体方法,可以快速了解该接口具备功能,以便在生产中能根据实际场景对号入座找到合适方法解决实际问题。

public interface ListOperations<K, V> {@NullableList<V> range(K key, long start, long end);void trim(K key, long start, long end);@NullableLong size(K key);@NullableLong leftPush(K key, V value);@NullableLong leftPushAll(K key, V... values);@NullableLong leftPushAll(K key, Collection<V> values);@NullableLong leftPushIfPresent(K key, V value);@NullableLong leftPush(K key, V pivot, V value);@NullableLong rightPush(K key, V value);@NullableLong rightPushAll(K key, V... values);@NullableLong rightPushAll(K key, Collection<V> values);@NullableLong rightPushIfPresent(K key, V value);@NullableLong rightPush(K key, V pivot, V value);@Nullabledefault V move(ListOperations.MoveFrom<K> from, ListOperations.MoveTo<K> to) {Assert.notNull(from, "Move from must not be null");Assert.notNull(to, "Move to must not be null");return this.move(from.key, from.direction, to.key, to.direction);}@NullableV move(K sourceKey, Direction from, K destinationKey, Direction to);@Nullabledefault V move(ListOperations.MoveFrom<K> from, ListOperations.MoveTo<K> to, Duration timeout) {Assert.notNull(from, "Move from must not be null");Assert.notNull(to, "Move to must not be null");Assert.notNull(timeout, "Timeout must not be null");Assert.isTrue(!timeout.isNegative(), "Timeout must not be negative");return this.move(from.key, from.direction, to.key, to.direction, TimeoutUtils.toMillis(timeout.toMillis(), TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);}@Nullabledefault V move(K sourceKey, Direction from, K destinationKey, Direction to, Duration timeout) {Assert.notNull(timeout, "Timeout must not be null");Assert.isTrue(!timeout.isNegative(), "Timeout must not be negative");return this.move(sourceKey, from, destinationKey, to, TimeoutUtils.toMillis(timeout.toMillis(), TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);}@NullableV move(K sourceKey, Direction from, K destinationKey, Direction to, long timeout, TimeUnit unit);void set(K key, long index, V value);@NullableLong remove(K key, long count, Object value);@NullableV index(K key, long index);Long indexOf(K key, V value);Long lastIndexOf(K key, V value);@NullableV leftPop(K key);@NullableList<V> leftPop(K key, long count);@NullableV leftPop(K key, long timeout, TimeUnit unit);@Nullabledefault V leftPop(K key, Duration timeout) {Assert.notNull(timeout, "Timeout must not be null");Assert.isTrue(!timeout.isNegative(), "Timeout must not be negative");return this.leftPop(key, TimeoutUtils.toSeconds(timeout), TimeUnit.SECONDS);}@NullableV rightPop(K key);@NullableList<V> rightPop(K key, long count);@NullableV rightPop(K key, long timeout, TimeUnit unit);@Nullabledefault V rightPop(K key, Duration timeout) {Assert.notNull(timeout, "Timeout must not be null");Assert.isTrue(!timeout.isNegative(), "Timeout must not be negative");return this.rightPop(key, TimeoutUtils.toSeconds(timeout), TimeUnit.SECONDS);}@NullableV rightPopAndLeftPush(K sourceKey, K destinationKey);@NullableV rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);@Nullabledefault V rightPopAndLeftPush(K sourceKey, K destinationKey, Duration timeout) {Assert.notNull(timeout, "Timeout must not be null");Assert.isTrue(!timeout.isNegative(), "Timeout must not be negative");return this.rightPopAndLeftPush(sourceKey, destinationKey, TimeoutUtils.toSeconds(timeout), TimeUnit.SECONDS);}RedisOperations<K, V> getOperations();public static class MoveTo<K> {final K key;final Direction direction;MoveTo(K key, Direction direction) {this.key = key;this.direction = direction;}public static <K> ListOperations.MoveTo<K> toHead(K key) {return new ListOperations.MoveTo(key, Direction.first());}public static <K> ListOperations.MoveTo<K> toTail(K key) {return new ListOperations.MoveTo(key, Direction.last());}}public static class MoveFrom<K> {final K key;final Direction direction;MoveFrom(K key, Direction direction) {this.key = key;this.direction = direction;}public static <K> ListOperations.MoveFrom<K> fromHead(K key) {return new ListOperations.MoveFrom(key, Direction.first());}public static <K> ListOperations.MoveFrom<K> fromTail(K key) {return new ListOperations.MoveFrom(key, Direction.last());}}
}

以上,感谢。

2023年4月12日

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_286210.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数据结构】-计数排序

&#x1f387;作者&#xff1a;小树苗渴望变成参天大树 &#x1f389; 作者宣言&#xff1a;认真写好每一篇博客 &#x1f38a;作者gitee:link 如 果 你 喜 欢 作 者 的 文 章 &#xff0c;就 给 作 者 点 点 关 注 吧&#xff01; 文章目录前言一、计数排序二、排序算法复杂度…

《花雕学AI》18:AI绘画尝鲜Prompt Hunt,使用人工智能模型来创造、探索和分享艺术作品

引言&#xff1a; 人工智能是当今科技领域的热门话题&#xff0c;它不仅可以帮助人类解决各种实际问题&#xff0c;也可以激发人类的创造力和艺术感。Prompt Hunt就是一个利用人工智能模型来创造、探索和分享艺术作品的AI绘画网站。它提供了三种不同的模型&#xff0c;分别是S…

Java垃圾收集原理

程序计数器、虚拟机栈、本地方法栈这三个区域随线程而灭&#xff0c;栈中栈帧的内存大小也是在确定的。这几个区域的内存分配和回收都具有确定性&#xff0c;因此不需要过多考虑如何回收。 Java堆和方法区这两个区域有着很显著的不确定性 一个接口的实现类需要的内存可能不一…

用Flutter开发一款音乐App(从0到1开发一款音乐App)

Flutter Music_Listener(flutter音乐播放器) Flutter version 3.9 项目介绍 1、项目整体基于getxretrofitdiojsonserialize开发 2、封装通用控制器BaseController&#xff0c;类似jetpack mvvm框架中的BaseViemodel 3、封装基础无状态基类BaseStatelessWidget&#xff0c;结合…

十三、市场活动:全部导出

功能需求&#xff1a;批量导出市场活动 用户在市场活动主页面,点击"批量导出"按钮,把所有市场活动生成一个excel文件,弹出文件下载的对话框; 用户选择要保存的目录,完成导出市场活动的功能. *导出成功之后,页面不刷新 功能分析&#xff1a;导出市场活动 1.给批量…

Vue组件化编程【Vue】

2.Vue 组件化编程 2.1 模块与组件、模块化与组件化 2.1.1 模块 理解&#xff1a;向外提供特定功能的js程序&#xff0c;一般就是一个js文件为什么&#xff1a;js文件很多很复杂作用&#xff1a;复用js、简化js的编写、提高js运行效率。 2.1.2 组件 理解&#xff1a;用来实…

接口自动化【一】(抓取后台登录接口+postman请求通过+requests请求通过+json字典区别)

文章目录 前言一、requests库的使用二、json和字典的区别三、后端登录接口-请求数据生成四、接口自动化-对应电商项目中的功能五、来自postman的代码-后端登录总结前言 记录&#xff1a;json和字典的区别&#xff0c;json和字段的相互转化&#xff1b;postman发送请求与Python…

source insight4.0使用技巧总结

一、技巧1&#xff1a;查看函数调用关系 步骤 1&#xff1a;在主菜单中点击下图中的按钮 图 1 打开relation界面 步骤 2&#xff1a;在弹出的relation界面点击“设置”按钮&#xff0c; 图2 点击“设置”按钮 步骤3&#xff1a; 在“设置”界面中&#xff0c;“Levels”选择…

AC7811-FOC无感控制代码详解

目录 矢量控制原理 矢量控制框图 电流采样方式 电流在整个控制过程中的传递 采样关键点 三电阻 双电阻 单电阻 三者对比 坐标变换 dq轴电流的PI控制 启动方式 启动波形 脉冲注入 高频注入 Startup 预定位到指定角度 PulseInject_api hfi_api Speed loop s…

前端学习:HTML块、类、Id

目录 快 一、块元素、内联元素 二、HTML 元素 三、HTML元素 类 一、分类块级元素 二、分类行内元素 Id 一、使用 id 属性 二、 class与ID的差异 三、总结 快 一、块元素、内联元素 大多数HTML元素被定义为块级元素或内联元素。 块级元素在浏览器显示时&#xff0c;通常会…

FTP-----局域网内部传输文件(1)

在日常工作中&#xff0c;如果需要跨设备的传输文件&#xff0c;您需要借助USB数据线或者借助应用实现无线互联&#xff0c;将所需文件传输到对应设备&#xff0c;这一来一去&#xff0c;花费的时间与精力变多了&#xff0c;那么&#xff0c;怎么实现不使用第三方软件来实现跨设…

3-5年以上的功能测试如何进阶自动化?【附学习路线】

做为功能测试人员来讲&#xff0c;从发展方向上可分两个方面&#xff1a; 1、业务流程方向 2、专业技能方向。 当确定好方向后&#xff0c;接下来就是如何达到了。(文末自动化测试学习资料分享) 一、业务流程方向 1、熟悉底层的业务 作为功能测试工程师来讲&#xff0c;了解…

【C++高级】手写线程池项目-经典死锁问题分析-简历项目输出指导

作为五大池之一&#xff0c; 线程池的应用非常广 泛&#xff0c;不管是客户端程序&#xff0c;还是后台服务程序&#xff0c;掌握线程池&#xff0c;是提高业务处理能力的必备模块 本课程将带你从零开始&#xff0c;设计一个支持fixed和cached模式的线程池&#xff0c;玩转C11、…

IGA_PLSM3D的理解1

文章目录前言一、IgaTop3D_FAST.m给的参数二、Material properties 材料特性对Geom_Mod3D的理解对Pre_IGA3D的理解 输出1-----CtrPts&#xff1a; 输出2-----Ele&#xff1a; 输出3-----GauPts&#xff1a;前言 只是为方便学习&#xff0c;不做其他用途 一、IgaTop3D_FAST.m给的…

Python爬虫-某跨境电商(AM)搜索热词

前言 本文是该专栏的第42篇,后面会持续分享python爬虫干货知识,记得关注。 关于某跨境电商(AM),本专栏前面有单独详细介绍过,获取配送地的cookie信息以及商品库存数据,感兴趣的同学可往前翻阅。 1. python爬虫|爬取某跨境电商AM的商品库存数据(Selenium实战) 2. Seleni…

5.39 综合案例2.0 - STM32蓝牙遥控小车1(手机APP遥控)

综合案例2.0 - 蓝牙遥控小车1- 手机APP遥控成品展示案例说明器件说明连线小车源码手机遥控APPAPP使用说明成品展示 案例说明 用STM32单片机做了一辆蓝牙控制的麦轮小车&#xff0c;分享一下小车的原理和制作过程。 控制部分分为手机APP&#xff0c;语音模块控制&#xff0c;Ha…

15-721 chapter2 内存数据库

Background 随着时代的发展&#xff0c;DRAM可以容纳足够的便宜&#xff0c;容量也变大了。对于数据库来说&#xff0c;数据完全可以fit in memory&#xff0c;但同时面向disk的数据库架构不能很好的发挥这个特性 这张图是disk database的cpu instruction cost 想buffer pool…

第5章 继承-Java核心技术·卷1

文章目录Java与C不同基本概念继承&#xff1a;基于已有的类创建新的类。构造器多态定义超类变量可以引用所有的子类对象&#xff0c;但子类变量不能引用超类对象。子类引用的数组可以转换成超类引用的数组覆写返回子类型强制类型转换阻止继承&#xff1a;final类和方法多态 vs …

ROS学习-ROS简介

文章目录1.ROS1.1 ROS概念1.2 ROS特征1.3 ROS特点1.4 ROS版本1.5 ROS程序其他名词介绍1. 元操作系统2. IDL 接口定义语言一些网站1.ROS 1.1 ROS概念 ROS(Robot Operating System&#xff0c;机器人操作系统) ROS 是一个适用于机器人的开源的元操作系统&#xff0c;提供一系列…

linux驱动开发 - 04_Linux 设备树学习 - DTS语法

文章目录Linux 设备树学习 - DTS语法1 什么是设备树&#xff1f;2 DTS、DTB和DTC3 DTS 语法3.1 dtsi 头文件3.2 设备节点3.3 标准属性1、compatible 属性2、model 属性3、status 属性4、#address-cells 和#size-cells 属性5、reg 属性6、ranges 属性7、name 属性8、device_type…