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
相关推荐
智购科技无人售货机厂家5 小时前
2026自动售货机制冷系统维护指南:从散热器清洁到压缩机换油的工程实践~YH2601_962055975 小时前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围EXI-小洲5 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载2601_961901706 小时前
SpringBoot使用Nacos进行application.yml配置管理Lost of 程序猿6 小时前
ASP.NET Core API 幂等性设计深度实战:从一次重复申领事故说起QQ_21696290967 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战郑州光合科技余经理9 小时前
本地生活服务系统:模块边界与结算字段怎么拆IT_陈寒9 小时前
Vue的嵌套组件竟然吃掉了我的事件?2601_962174179 小时前
Redis 简介2601_962128419 小时前
SpringBoot篇(缓存层)