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
相关推荐
林太白几秒前
docker安装以及部署node项目计算机毕设VX:Fegn089510 分钟前
计算机毕业设计|基于springboot + vue高校实验室教学管理系统(源码+数据库+文档)计算机学姐11 分钟前
基于SpringBoot的共享单车管理系统【2026最新】、BeYourself13 分钟前
Spring AI ChatClient -Prompt 模板小贝IT~42 分钟前
基于SpringBoot的网页时装购物系统-049悟空码字1 小时前
文档变形记,SpringBoot实战:3步让Word乖乖变PDF用户2190326527351 小时前
能省事”。SpringBoot+MyBatis-Plus:开发效率提升10倍!用户47949283569151 小时前
Superset 展示 JSONB 中文变天书?真相竟然是 Python 的“过度保护”古城小栈1 小时前
Rust语言:优势解析与擅长领域深度探索小楼v1 小时前
构建高效AI工作流:Java生态的LangGraph4j框架详解