当前位置: 首页 > 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)配置…...

5分钟打造专业级抽奖系统:Magpie-LuckyDraw全平台使用终极指南

5分钟打造专业级抽奖系统&#xff1a;Magpie-LuckyDraw全平台使用终极指南 【免费下载链接】Magpie-LuckyDraw &#x1f3c5;A fancy lucky-draw tool supporting multiple platforms&#x1f4bb;(Mac/Linux/Windows/Web/Docker) 项目地址: https://gitcode.com/gh_mirrors/…...

【计算机网络硬核指南】子网划分终极篇:定长+VLSM+超网三合一实战(3道大厂真题逐字节演算)

【计算机网络硬核指南】子网划分终极篇&#xff1a;定长VLSM超网三合一实战&#xff08;3道大厂真题逐字节演算&#xff09; 前言 在上一篇文章中&#xff0c;我们系统学习了IP地址基础和子网划分的核心方法&#xff0c;逐题演算了9道经典真题。很多读者反馈说&#xff0c;看…...

二叉树‘找叶子’的三种姿势:从PTA真题到LeetCode变体(层次/先序/后序遍历对比)

二叉树‘找叶子’的三种姿势&#xff1a;从PTA真题到LeetCode变体&#xff08;层次/先序/后序遍历对比&#xff09; 在算法学习的道路上&#xff0c;二叉树遍历是每个程序员必须掌握的基本功。而"找叶子节点"这一看似简单的任务&#xff0c;却能衍生出多种解法&…...

《如何追上那只乌龟》阅读指北

《如何追上那只乌龟》是一本以“芝诺悖论”为引子的漫画科普书&#xff0c;用轻松方式讲解微积分核心概念‌。它通过“阿基里斯追乌龟”这一哲学难题&#xff0c;带领读者从古希腊思辨走向现代数学工具&#xff0c;理解“无穷”“极限”“微分与积分”等抽象概念如何被一步步构…...

56.自定义协议

lesson43client加个while decode防止请求积压协议不仅可以用在网络&#xff0c;文件也是面向字节流&#xff0c;可以用在文件守护进程化&#xff1a;父进程创建管道&#xff0c;兄弟进程都可以看见这个管道&#xff0c;关闭对应读写端就可以实现通信。UID是代表谁创建的4-2课件…...

中小团队如何利用Taotoken统一管理多个AI模型的API调用

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 中小团队如何利用Taotoken统一管理多个AI模型的API调用 对于需要协调使用多个大模型的中小开发团队而言&#xff0c;一个常见的工程…...

明日方舟终极自动化助手:MAA如何彻底解放你的游戏时间

明日方舟终极自动化助手&#xff1a;MAA如何彻底解放你的游戏时间 【免费下载链接】MaaAssistantArknights 《明日方舟》小助手&#xff0c;全日常一键长草&#xff01;| A one-click tool for the daily tasks of Arknights, supporting all clients. 项目地址: https://git…...

长期使用Taotoken服务在稳定性与响应速度上的综合体验

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 长期使用Taotoken服务在稳定性与响应速度上的综合体验 在持续数月的日常开发与测试工作中&#xff0c;我们团队将多个项目的大模型…...

5分钟搭建Windows离线语音转文字系统:TMSpeech让你的会议记录零压力

5分钟搭建Windows离线语音转文字系统&#xff1a;TMSpeech让你的会议记录零压力 【免费下载链接】TMSpeech 腾讯会议摸鱼工具 项目地址: https://gitcode.com/gh_mirrors/tm/TMSpeech 在数字化办公时代&#xff0c;实时语音转文字已成为提升工作效率的关键技术。TMSpeec…...

ncmdumpGUI:3分钟解锁网易云音乐ncm格式,让你的音乐无处不在

ncmdumpGUI&#xff1a;3分钟解锁网易云音乐ncm格式&#xff0c;让你的音乐无处不在 【免费下载链接】ncmdumpGUI C#版本网易云音乐ncm文件格式转换&#xff0c;Windows图形界面版本 项目地址: https://gitcode.com/gh_mirrors/nc/ncmdumpGUI 还在为网易云音乐下载的nc…...