当前位置: 首页 > news >正文

【Java Web】利用Spring整合Redis,配置RedisTemplate

1. 在config中加入RedisConfig配置类

package com.nowcoder.community.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 设置Key的序列化方式template.setKeySerializer(RedisSerializer.string());// 设置value的序列化方式template.setValueSerializer(RedisSerializer.json());// 设置hash的key的序列化方式template.setHashKeySerializer(RedisSerializer.json());// 设置hash的value的序列化方式template.setHashValueSerializer(RedisSerializer.json());template.afterPropertiesSet();return template;}
}

2. 写个测试类测试一下

package com.nowcoder.community;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;import java.util.concurrent.TimeUnit;@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testStrings(){String redisKey = "test:count";redisTemplate.opsForValue().set(redisKey, 1);System.out.println(redisTemplate.opsForValue().get(redisKey));System.out.println(redisTemplate.opsForValue().increment(redisKey));System.out.println(redisTemplate.opsForValue().decrement(redisKey));}@Testpublic void testHash(){String redisKey = "test:user";redisTemplate.opsForHash().put(redisKey,"id",1);redisTemplate.opsForHash().put(redisKey,"name","katniss");redisTemplate.opsForHash().put(redisKey,"age",24);System.out.println(redisTemplate.opsForHash().get(redisKey,"id"));System.out.println(redisTemplate.opsForHash().get(redisKey,"name"));System.out.println(redisTemplate.opsForHash().get(redisKey,"age"));}@Testpublic void testLists(){String redisKey = "test:ids";redisTemplate.opsForList().leftPush(redisKey,101);redisTemplate.opsForList().leftPush(redisKey,102);redisTemplate.opsForList().leftPush(redisKey,103);System.out.println(redisTemplate.opsForList().size(redisKey));System.out.println(redisTemplate.opsForList().index(redisKey,0));System.out.println(redisTemplate.opsForList().range(redisKey,0,2));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));}@Testpublic void testSets(){String redisKey = "test:teachers";redisTemplate.opsForSet().add(redisKey,"111","222","333","444","555");System.out.println(redisTemplate.opsForSet().size(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));System.out.println(redisTemplate.opsForSet().members(redisKey));}@Testpublic void testSortedSets(){String redisKey = "test:students";redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);redisTemplate.opsForZSet().add(redisKey, "悟空", 90);redisTemplate.opsForZSet().add(redisKey, "八戒", 70);redisTemplate.opsForZSet().add(redisKey, "沙僧", 60);System.out.println(redisTemplate.opsForZSet().zCard(redisKey));System.out.println(redisTemplate.opsForZSet().score(redisKey,"悟空"));System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey,"八戒"));System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey,0,2));}@Testpublic void testKeys(){System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.delete("test:user");System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.expire("test:students",10, TimeUnit.SECONDS);}// 多次访问同一个Key@Testpublic void testBoundOperations(){String redisKey = "test:count";BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();System.out.println(operations.get());}// 编程式事务@Testpublic void testTransaction(){Object obj = redisTemplate.execute(new SessionCallback() {@Overridepublic Object execute(RedisOperations operations) throws DataAccessException {String redisKey = "test:tx";operations.multi();operations.opsForSet().add(redisKey,"zhangsan");operations.opsForSet().add(redisKey,"lisi");operations.opsForSet().add(redisKey,"wangwu");System.println.out(operations.opsForSet().members(redisKey));return operations.exec();}});System.out.println(obj);}
}

3. 注意事项

  • Redis不满足事务的原子性,原子性是指事务要么被全部执行,要么都不执行。但是Redis不支持回滚,就可能会出现有些语句执行成功,有些执行失败,因此具备原子性;
  • Redis事务的三个阶段:
    • 开始事务
    • 命令入队
    • 顺序执行
  • 用Redis管理事务的时候中间不要做查询,查询会被执行但无效。例如在testTransaction方法中,operations.opsForSet().members(redisKey)将返回空。

相关文章:

【Java Web】利用Spring整合Redis,配置RedisTemplate

1. 在config中加入RedisConfig配置类 package com.nowcoder.community.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFacto…...

如何正确的写出第一个java程序:hello java

1 前言 最近公司由于项目需要&#xff0c;开始撸java代码了。学习一门新的编程语言&#xff0c;刚开始总是要踩很多坑&#xff0c;所以记录一下学习过程&#xff0c;也希望对java初学者有所帮助。 2 hello java 2.1 程序源码 程序内容十分简单&#xff0c;这里就不再过多赘…...

使用llvm 编译最新的linux 内核(LoongArch)

1. 准备交叉工具链 llvm 使用了最新的llvm-17, 编译方法见:编译LoongArch的llvm交叉工具链 gcc 从linux 官方下载&#xff1a;http://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/x86_64/13.2.0/x86_64-gcc-13.2.0-nolibc-loongarch64-linux.tar.xz 发布llvm和g…...

Using Multiple RDF Knowledge Graphs for Enriching ChatGPT Responses

本文是LLM系列文章&#xff0c;针对《Using Multiple RDF Knowledge Graphs for Enriching ChatGPT Responses》的翻译。 使用多个RDF知识图来丰富ChatGPT响应 摘要1 引言2 相关工作3 GPT-LODS的过程和用例4 结束语 摘要 最近有一种趋势是使用新型人工智能聊天GPT聊天箱&…...

【Hive-小文件合并】Hive外部分区表利用Insert overwrite的暴力方式进行小文件合并

这里我们直接用实例来讲解&#xff0c;Hive外部分区表有单分区多分区的不同情况&#xff0c;这里我们针对不同情况进行不同的方式处理。 利用overwrite合并单独日期的小文件 1、单分区 # 开启此表达式&#xff1a;(sample_date)?. set hive.support.quoted.identifiersnon…...

位运算 |(按位或) (按位与) ^(按位异或)

目录 文章目录&#xff1a;本章讲解的主要是刷题系列 1&#xff1a;首先会介绍 I & ^这三个操作符的作用&#xff0c;性质 2&#xff1a;三道使用位运算操作符的经典 笔试题(来自剑指offer) 题目链接如下&#xff1a; 1:136. 只出现一次的数字 - 力扣&#xff08;LeetCode…...

Qt应用开发(基础篇)——复选按钮 QCheckBox 单选按钮 QRadioButton

一、前言 QCheckBox类与QRadioButton类继承于QAbstractButton&#xff0c;QCheckBox是一个带有文本标签的复选框&#xff0c;QRadioButton是一个带有文本标签的单选按钮。 按钮基类 QAbstractButton QCheckBox QCheckBox复选框是一个很常用的控件&#xff0c;拥有开关(选中和未…...

AERMOD模型大气环境影响评价

随着我国经济快速发展&#xff0c;我国面临着日益严重的大气污染问题。近年来&#xff0c;严重的大气污染问题已经明显影响国计民生&#xff0c;引起政府、学界和人们越来越多的关注。大气污染是工农业生产、生活、交通、城市化等方面人为活动的综合结果&#xff0c;同时气象因…...

递归组装树结构的数据

开发中&#xff0c;经常遇到存在树形结构的数据&#xff0c;如行政区划这类数据&#xff0c;一级一级分层&#xff0c;后端需要组装好树形结构数据返回给前端。 由于返给前端的json数据中&#xff0c;如果是叶子节点了&#xff0c;说明它没有子节点&#xff0c;那么就没必要返…...

企业架构LNMP学习笔记7

PHP介绍&#xff1a; HTML&#xff1a;超文本标记语言 http: 超文本传输协议 端口80 浏览器将html代码解析成web页面。 PHP&#xff1a;超文本预处理器。后端语言开发&#xff0c;页面上需要动态改变修改的&#xff0c;需要连接数据库查询数据&#xff0c;转为html。 主要…...

开店星小程序上架教程和后台Request failed with status code 500[undefined]问题处理

开店星小程序上架教程和后台Request failed with status code 500[undefined]问题处理 刚刚安装好开店星网站后台之后都会出现这个code 500[undefined]的错误&#xff0c;需要改一下代码。改好了之后就可以正常使用了。如果大家不懂得这样处理的可以私聊我&#xff0c;帮忙处理…...

第一百三十六回 WillPopScope组件

文章目录 概念介绍使用方法示例代码 我们在上一章回中介绍了下拉刷新组件相关的内容&#xff0c;本章回中将介绍 WillPopScope组件.闲话休提&#xff0c;让我们一起Talk Flutter吧。 概念介绍 我们在本章回中介绍的WillPopScope组件是一种事件拦截类组件&#xff0c;它没有具…...

【论文爬虫】自动将论文详细信息直送notion并自动下载(含源码)

输入论文标题&#xff0c;本爬虫将自动在semanticscholar.com和arxiv.com搜索该文章&#xff0c;自动获取其日期、作者、url、摘要等信息&#xff0c;并自动发送到你提前设置好的notion数据库里&#xff0c;同时自动从arxiv下载论文&#xff0c;然后将论文的保存地址在notion页…...

Android知识点整理

关键点 Activity Fragment 调试应用 处理应用程序配置 Intent 和 Intent 过滤器 会使用Context 后台处理指南 Android 的数据隐私 Android 网络数据安全教程 Android 中的依赖项注入 内容提供程序 Android 内存管理概览 一些重要的库 1.Glide 是一个 Android 上的…...

JSON与电子表格

一、介绍 电子表格是一种常见的电子数据处理工具&#xff0c;而JSON是一种数据交换格式。电子表格和JSON之间可以进行数据的导入和导出&#xff0c;以实现数据的相互转换和交互。 在电子表格中&#xff0c;数据以行和列的形式组织&#xff0c;并可以包含不同的数据类型。每个…...

Oracle创建用户、授权视图权限

1、创建用户密码 create user 用户名 identified by 密码;2、创建视图 CREATE VIEW 用户1.表名1 AS SELECT * FROM 用户2.表名2 t;3、授权 GRANT SELECT ON 用户2.表名2 TO 用户1 with GRANT OPTION &#xff1b;grant connect to 用户名; grant select on 用户1.表名1 t…...

MT4移动端应用指南:随时随地进行交易

如今&#xff0c;随着科技的不断发展&#xff0c;我们可以随时随地通过手机进行各种操作&#xff0c;包括进行金融交易。本文将为大家介绍一款优秀的金融交易软件——MT4&#xff08;可在mtw.so/6gwPno这点下&#xff09;移动端应用&#xff0c;并提供详细的使用指南&#xff0…...

【数据挖掘】学习笔记

文章目录 < 数据预处理 > 聚集&#xff1a;多个样本或特征进行合并&#xff08;减少样本规模、转换标度、更稳定&#xff09;抽样&#xff1a;抽取一部分样本降维&#xff1a;在地位空间中表示样本&#xff08;PCA、SVD&#xff09;特征选择&#xff1a;选取重要特征&am…...

MyBatis-Plus排除不必要的字段

查询学生信息排除年龄列表 &#x1f4da;&#x1f50d; 使用MyBatis-Plus排除某些字段。如果你想要进行查询&#xff0c;但又不需要包含某些字段&#xff0c;那么这个功能将非常适合你。&#x1f50d;&#x1f393;&#x1f4dd; 1. 学生信息查询-排除年龄列表 在使用 MyBat…...

webpack打包

文章目录 一、什么是webpack二、使用步骤1.创建一个新的文件夹,并将其初始化2.在当前目录下安装webpack以及webpack-cli3.配置webpack自定义命令,使之生效4.运行自定义命令,打包webpack5.打包成功之后会将内容打包到dist文件夹下6.配置webpack1)修改webpack打包入口和出口2)配置…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

视频字幕质量评估的大规模细粒度基准

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 摘要 视频字幕在文本到视频生成任务中起着至关重要的作用&#xff0c;因为它们的质量直接影响所生成视频的语义连贯性和视觉保真度。尽管大型视觉-语言模型&#xff08;VLMs&#xff09;在字幕生成方面…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

Ascend NPU上适配Step-Audio模型

1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统&#xff0c;支持多语言对话&#xff08;如 中文&#xff0c;英文&#xff0c;日语&#xff09;&#xff0c;语音情感&#xff08;如 开心&#xff0c;悲伤&#xff09;&#x…...

C++.OpenGL (14/64)多光源(Multiple Lights)

多光源(Multiple Lights) 多光源渲染技术概览 #mermaid-svg-3L5e5gGn76TNh7Lq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3L5e5gGn76TNh7Lq .error-icon{fill:#552222;}#mermaid-svg-3L5e5gGn76TNh7Lq .erro…...

IP如何挑?2025年海外专线IP如何购买?

你花了时间和预算买了IP&#xff0c;结果IP质量不佳&#xff0c;项目效率低下不说&#xff0c;还可能带来莫名的网络问题&#xff0c;是不是太闹心了&#xff1f;尤其是在面对海外专线IP时&#xff0c;到底怎么才能买到适合自己的呢&#xff1f;所以&#xff0c;挑IP绝对是个技…...

从“安全密码”到测试体系:Gitee Test 赋能关键领域软件质量保障

关键领域软件测试的"安全密码"&#xff1a;Gitee Test如何破解行业痛点 在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的"神经中枢"。从国防军工到能源电力&#xff0c;从金融交易到交通管控&#xff0c;这些关乎国计民生的关键领域…...

LangFlow技术架构分析

&#x1f527; LangFlow 的可视化技术栈 前端节点编辑器 底层框架&#xff1a;基于 &#xff08;一个现代化的 React 节点绘图库&#xff09; 功能&#xff1a; 拖拽式构建 LangGraph 状态机 实时连线定义节点依赖关系 可视化调试循环和分支逻辑 与 LangGraph 的深…...

【Linux手册】探秘系统世界:从用户交互到硬件底层的全链路工作之旅

目录 前言 操作系统与驱动程序 是什么&#xff0c;为什么 怎么做 system call 用户操作接口 总结 前言 日常生活中&#xff0c;我们在使用电子设备时&#xff0c;我们所输入执行的每一条指令最终大多都会作用到硬件上&#xff0c;比如下载一款软件最终会下载到硬盘上&am…...