1.添加依赖
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.配置
spring.redis.host=42.192.50.187
spring.redis.port=6379
spring.redis.password=redis@123
spring.redis.database=0
spring.redis.timeout=10000ms
# 连接池 - 最大连接数,默认:8
spring.redis.lettuce.pool.max-active=8
# 连接池 - 最大连接阻塞等待时间,默认:-1
spring.redis.lettuce.pool.max-wait=10000ms
# 连接池 - 最大、最小空闲连接数,最大默认:8,最小默认:0
spring.redis.lettuce.pool.max-idle=200
spring.redis.lettuce.pool.min-idle=5
3.配置类
/**
* 配置 RedisTemplate 序列化
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// String 类型 - 序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// hash 类型 - 序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
// 注入连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
4.使用
# 注入依赖;
@Autowired
private RedisTemplate redisTemplate;
存储数据:redisTemplate.opsForValue().set("user:" + ticket, user);
spring boot 简单整合 Redis
197837932023-08-15 20:02
相关推荐
烽学长3 分钟前
(附源码)基于Springboot+vue的图书阅读分享系统的设计与实现isfox7 分钟前
Python 单例模式:一个类只能有一个实例,到底怎么实现?青山木21 分钟前
RocketMQ 入门到原理(三):消息存储原理小白男神26 分钟前
MySQL进阶学习三(视图)猿人谷27 分钟前
Jev:当 AI 不再生成 Token,而是直接做决策苍何28 分钟前
WorkBuddy + 腾讯乐享,原来知识库还能这么用站大爷IP31 分钟前
Python的生成器把我坑惨了,原来yield和return的区别这么大苍何32 分钟前
一份来自大厂内部的《AI 原生实践避坑指南》codigger1 小时前
服务器又卡了?一篇讲透 Linux 性能排查(基础四件套 + perf/strace/火焰图)