Spring Boot 3.x 整合 Redis 实现高性能缓存的完整指南

在分布式系统或高并发场景下,数据库查询往往成为性能瓶颈。Redis 作为内存数据库,凭借其高速读写能力成为缓存的首选方案。本文将详细介绍如何在 Spring Boot 3.x 中整合 Redis,实现数据缓存、缓存穿透/雪崩防护等核心功能。

一、环境准备

  1. 技术栈
    • Spring Boot 3.1.5
    • Redis 7.0+
    • JDK 17+
    • Maven 3.8+
  2. 依赖配置pom.xml):

xml

复制代码
`<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 可选:用于序列化(如JSON) -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
`

二、核心实现步骤

1. 配置 Redis 连接

application.yml 中配置 Redis 连接参数:

yaml

复制代码
`spring:
  redis:
    host: localhost
    port: 6379
    password: yourpassword
    database: 0
    lettuce: # 连接池配置
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
`

2. 配置 RedisTemplate 序列化方式

默认的 JDK 序列化会导致 key 显示乱码,推荐使用 String + JSON 序列化:

java

复制代码
`@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        
        // String 序列化
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        
        // JSON 序列化(使用 Jackson)
        Jackson2JsonRedisSerializer<Object> jsonSerializer = 
            new Jackson2JsonRedisSerializer<>(Object.class);
        template.setValueSerializer(jsonSerializer);
        template.setHashValueSerializer(jsonSerializer);
        
        return template;
    }
}
`

3. 封装通用缓存工具类

java

复制代码
`@Component
public class RedisCacheUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 设置缓存(带过期时间)
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 获取缓存
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除缓存
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    // 更多方法:Hash、List、Set 操作...
}
`

4. 使用 Spring Cache 注解(推荐)

在启动类添加 @EnableCaching 注解,然后通过注解简化缓存操作:

java

复制代码
`@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    // 从缓存获取,不存在则查询数据库并写入缓存
    @Cacheable(value = "user", key = "#id")
    public User getUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    // 更新缓存
    @CachePut(value = "user", key = "#user.id")
    public User updateUser(User user) {
        return userRepository.save(user);
    }

    // 删除缓存
    @CacheEvict(value = "user", key = "#id")
    public void deleteUser(Long id) {
        userRepository.deleteById(id);
    }
}
`

三、高级优化技巧

1. 防止缓存穿透

  • 方案:使用空值缓存或布隆过滤器(Bloom Filter)。
  • 示例

java

复制代码
`public User getUserByIdSafe(Long id) {
    String key = "user:" + id;
    Object cached = redisTemplate.opsForValue().get(key);
    if (cached != null) {
        if (cached instanceof User) return (User) cached;
        return null; // 空值缓存
    }
    
    User user = userRepository.findById(id).orElse(null);
    if (user != null) {
        redisTemplate.opsForValue().set(key, user, 1, TimeUnit.HOURS);
    } else {
        // 空值缓存(防止穿透)
        redisTemplate.opsForValue().set(key, "", 10, TimeUnit.MINUTES);
    }
    return user;
}
`

2. 防止缓存雪崩

  • 方案
    • 设置随机过期时间
    • 使用多级缓存(本地缓存 + Redis)
  • 示例

java

复制代码
`public void setWithRandomExpire(String key, Object value) {
    int randomExpire = 3600 + new Random().nextInt(1800); // 1~1.5小时随机
    redisTemplate.opsForValue().set(key, value, randomExpire, TimeUnit.SECONDS);
}
`

3. 分布式锁实现

java

复制代码
`public boolean tryLock(String lockKey, String requestId, long expireTime) {
    Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, requestId, expireTime, TimeUnit.SECONDS);
    return Boolean.TRUE.equals(success);
}

public void unlock(String lockKey, String requestId) {
    String value = (String) redisTemplate.opsForValue().get(lockKey);
    if (requestId.equals(value)) {
        redisTemplate.delete(lockKey);
    }
}
`

四、常见问题排查

  1. 连接失败:检查 Redis 服务是否启动、防火墙设置、密码是否正确。
  2. 序列化异常 :确保所有缓存对象实现 Serializable 接口或配置正确的序列化器。
  3. 注解不生效 :确认启动类添加了 @EnableCaching 注解。

五、总结

通过 Spring Boot 整合 Redis,可以快速构建高性能缓存系统。合理使用缓存策略(如穿透/雪崩防护)能显著提升系统稳定性。完整代码示例已上传至 GitHub,欢迎 Star 和交流!

相关推荐
禾小西2 小时前
Spring AI 流式输出底层原理解析
java·人工智能·spring
DROm RAPS2 小时前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
丸辣,我代码炸了2 小时前
如何手搓序列化器(以java为例)
java·开发语言·kafka
快乐柠檬不快乐2 小时前
基于Java+SpringBoot+SSM攻防靶场实验室平台
java·开发语言·spring boot
爱丽_2 小时前
Spring Boot 启动链路:自动装配、条件注解与排错方法论
java·spring boot·后端
weixin_425023002 小时前
Spring Boot 2.7+JDK8+WebSocket对接阿里云百炼Qwen3.5-Plus 实现流式对话+思考过程实时展示
java·spring boot·websocket·ai编程
快乐柠檬不快乐2 小时前
IDEA报错内存溢出解决(java.lang.OutOfMemoryError)
java·ide·intellij-idea
RDCJM2 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
.柒宇.2 小时前
力扣hot 100之和为 K 的子数组(Java版)
java·算法·leetcode