当前位置:网站首页>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序列化和反序列化,非常给力
边栏推荐
- Activiti5.22.0扩展支持达国产数据库,以GBase据库为例
- 最低票价(DAY 80)
- Spark Learning Notes (VI) -- spark core core programming RDD action operator
- Portraiture5 new and upgraded leather filter plug-in artifact
- 队列达到最大长度代码实战
- 将幕布文章OPML转换为Markdown的方法
- Hcip day 14 notes
- Oracle有没有分布式数据库?
- JMeter distributed pressure measurement
- 如何进行 360 评估
猜你喜欢

索引最佳实践

spark学习笔记(四)——sparkcore核心编程-RDD

Best practices of opentelemetry in service grid architecture

It's too strong. An annotation handles the data desensitization returned by the interface

Idea 中添加支持@Data 插件

太强了,一个注解搞定接口返回数据脱敏

Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议

图解用户登录验证流程,写得太好了!

关于OpenFeign的源码分析

注解@Autowired和@Resource的区别总结
随机推荐
The diagram of user login verification process is well written!
Volatile keyword and its function
积分发放带给商家的两个帮助
vector 转 svg 方法
常见弱口令大全
Penetration test - post penetration - Trace cleaning
Boom 3D new 2022 audio enhancement app
Technology vane | interpretation of cloud native technology architecture maturity model
二叉树(北京邮电大学机试题)(DAY 85)
Graphic SQL, this is too vivid!
Learn the recycling mechanism of recyclerview again
What are "full five unique" and "full two unique"? Any difference?
[learn FPGA programming from scratch -54]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Altera)
关于OpenFeign的源码分析
【树链剖分】模板题
图解用户登录验证流程,写得太好了!
易灵思T35 FPGA驱动LVDS显示屏
Quick sequencing and optimization
太强了,一个注解搞定接口返回数据脱敏
FactoryBean的getObject调用时机