当前位置:网站首页>redis入门练习
redis入门练习
2022-07-27 01:48:00 【我乃cv大师】
springboot整合redis
1.导入相应依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>2.springboot中的application.properties文件对应配置
#Redis服务器地址
spring.redis.host=你的redis的ip地址
#Redis服务器连接端口
spring.redis.port=你的redis端口
#Redis数据库索引(默认为0)
spring.redis.database= 0
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
spring.redis.password=你的redis密码对应的命令,详细命令可自己搜索操作

3. 在springboot测试类中实验一下
@SpringBootTest
public class redistest {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void test1(){
redisTemplate.opsForValue().set("1","1");
}
}测试成功

SpringDataRedis序列化
redis可以接收任意Object作为值写入redis,但是会将Object序列转换成字节形式,默认采用JDK序列化,得到如下的结果

可读性很差,内存占用也是非常大,非常痛苦
将其转换成JSON字符串可以完美解决这种问题
可以使用fastjson包,将Object类型的对象直接转换成json字符串,导入如下依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
将Object对象转换成json字符串 JSON.toJSONString(Object)
redisTemplate.opsForValue().set("TOKEN_"+token, JSON.toJSONString(sysUsers),1, TimeUnit.DAYS);将json字符串反向转化成原对象
SysUsers sysUsers=JSON.parseObject(userJson,SysUsers.class);以此完成redis序列化和反序列化,非常给力
边栏推荐
- 自己梳理的LocalDateTime的工具类
- Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
- Spark Learning Notes (VI) -- spark core core programming RDD action operator
- PyCharm中Debug模式进行调试详解
- How many implementation postures of delay queue? Daily essential skills!
- Bulk copy baby upload prompt garbled, how to solve?
- sqlserver select * 能不能排除某个字段
- Volatile keyword and its function
- Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
- C语言const用法详解
猜你喜欢

消息被拒MQ

Attention should be paid to the first parameter of setTimeout

深入理解Mysql索引底层数据结构与算法

mysql出现不存在错误

数据库概论 - MySQL的简单介绍

Byte side: can TCP and UDP use the same port?

Functions that should be selected for URL encoding and decoding

DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库

Spark: ranking statistics of regional advertising hits (small case)

Spark Learning Notes (IV) -- spark core programming RDD
随机推荐
spark学习笔记(六)——sparkcore核心编程-RDD行动算子
微信小程序生成Excel
Add support for @data add-on in idea
Single case mode (double check lock)
Leetcode 207. curriculum (July 26, 2022)
“date: write error: No space left on device”解决
Worthington果胶酶的特性及测定方案
How to uniquely identify a user SQL in Youxuan database cluster
[从零开始学习FPGA编程-54]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Altera)
sqlserver select * 能不能排除某个字段
An error in the fourth edition of the red book?
AcWing 2074. 倒计数 模拟
周全的照护 解析LYRIQ锐歌电池安全设计
Indexing best practices
易灵思T35 FPGA驱动LVDS显示屏
DNS记录类型及相关名词解释
Best practices of opentelemetry in service grid architecture
Spark Learning Notes (V) -- spark core core programming RDD conversion operator
Message rejected MQ
带你了解什么是 Web3.0