Redis 从安装到集成 Spring Boot 完整总结

一、下载与安装

1.1 下载地址

https://github.com/redis-windows/redis-windows/releases

选择版本(如 7.0.15),下载 Windows 版 zip 包。

1.2 安装方式一:手动启动(临时)
bash 复制代码
cd /d "D:\Download Software\Redis\Redis-7.0.15-Windows-x64-cygwin-with-Service"
redis-server.exe

⚠️ 窗口一关就停了,只适合临时测试。

1.3 安装方式二:注册为 Windows 服务(推荐,永久运行)

操作:

  1. 找到目录里的 install_redis_service.bat
  2. 右键 → 以管理员身份运行。
  3. 按两次回车(默认路径)。
  4. 看到"服务已经启动成功"就装好了。

效果:

  • 开机自动启动 ✅
  • 后台静默运行 ✅
  • 关了终端也不影响 ✅

管理命令:

bash 复制代码
net start Redis  # 启动
net stop Redis   # 停止
1.4 验证是否运行
bash 复制代码
redis-cli.exe ping
# 返回 PONG 说明正常运行

二、Spring Boot 集成 Redis

2.1 pom.xml 加依赖
xml 复制代码
<!-- 核心依赖 -->
<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.2 application.yml 配置
yaml 复制代码
spring:
  redis:
    host: localhost  # Redis 地址
    port: 6379       # Redis 端口
    timeout: 3000ms  # 连接超时
    lettuce:
      pool:
        max-active: 16  # 最大连接数
        max-idle: 8     # 最大空闲
        min-idle: 4     # 最小空闲
2.3 RedisConfig.java(必配)

3 个关键配置:

配置 作用
RedisTemplate 操作 Redis 的核心模板
CacheManager 管理缓存,设置过期时间
KeySerializer 序列化 key,避免乱码
2.4 RedisService.java(可选,手动操作)

常用的几个方法:

java 复制代码
redisService.set("key", 对象); // 存
redisService.set("key", 对象, 过期秒数); // 存(带过期)
redisService.get("key"); // 取
redisService.delete("key"); // 删
redisService.hasKey("key"); // 判断是否存在

三、使用缓存的方式

方式一:注解方式(推荐,最简单)
注解 含义 用在
@Cacheable 查询时缓存 查询方法
@CachePut 更新时缓存 更新方法
@CacheEvict 删除时清缓存 删除方法
方式二:RedisService 手动方式(灵活)
java 复制代码
@Service
public class YourService {
    private final RedisService redisService;
// 存
redisService.set("user:1", user, 1800);
// 取
User user = (User) redisService.get("user:1");
// 删
redisService.delete("user:1");
}

四、你项目中的完整缓存策略

  • getUserById(1) → 存 Redis:user::1 // 查完自动存
  • updateUser(用户) → 更新 Redis:user::1 // 改完自动更新
  • deleteUser(1) → 删 Redis:user::1 // 删完自动删
  • deleteUser(1) → 清空:userPage::* // 删完清分页
  • addUser(用户) → 清空:userPage::* // 新增清分页
  • getUsersByPage → 存 Redis:userPage::1-10 // 分页缓存

五、验证缓存是否生效

用 redis-cli 查看:

bash 复制代码
redis-cli.exe
flushall      # 清空所有缓存
keys *        # 查看所有 key
get user::1   # 查看具体缓存内容
ttl user::1   # 查看过期时间(还剩多少秒)

验证流程:

  1. 第一次 curl http://localhost:8080/api/users/1 → 查数据库,控制台打印 SQL → 结果存 Redis。
  2. 第二次 curl http://localhost:8080/api/users/1 → 从 Redis 返回,控制台无 SQL 日志。

六、注意事项(坑点)

1. 实体类要实现 Serializable
java 复制代码
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
}

不然缓存序列化会报错。

2. LocalDateTime 需要 JavaTimeModule
java 复制代码
ObjectMapper om = new ObjectMapper();
om.registerModule(new JavaTimeModule()); // 处理 LocalDateTime

否则报:Java 8 date/time type not supported

3. 同类中方法调用,缓存不生效
java 复制代码
// ❌ 这样调用 @Cacheable 不生效
public User someMethod(Long id) {
    return this.getUserById(id); // this 不走代理
}
// ✅ 外部调用才生效
userService.getUserById(1); // 走代理
4. @CacheEvict 不能写两个

需要同时删多个缓存时,用 @Caching 包起来:

java 复制代码
@Caching(evict = {
    @CacheEvict(value = "userPage", allEntries = true),
    @CacheEvict(value = "user", key = "#id")
})
5. value 和 key 要配对
  • @Cacheable(value = "user", key = "#id") → 存到 user::1
  • @CacheEvict(value = "user", key = "#id") → 删的是 user::1 ✅ 必须一致
6. Redis 服务要启动

项目启动前必须先启动 Redis,否则项目启动报错。装了 Windows 服务就不用担心了。

7. 缓存过期时间

cacheManager 里配的是 30 分钟,30 分钟后自动过期,下次查询重新查数据库。


七、常见命令

命令 说明
redis-cli.exe 打开 Redis 客户端
keys * 查看所有缓存 key
get key名 查看某个缓存内容
ttl key名 查看过期时间
flushall 清空所有缓存
del key名 删除某个缓存
net start Redis 启动 Redis 服务
net stop Redis 停止 Redis 服务
相关推荐
苏生Susheng3 小时前
【软件实施】Windows 服务器运维实战
java·运维·服务器·windows·spring boot·javaweb·实施
学长毕业设计5 小时前
基于SpringBoot的健康食谱管理系统的设计与实现(源码+文档+讲解视频)
java·spring boot·后端
骇客野人6 小时前
Springboot 的 配置文件 application.yml 和 bootrap.yml 的由来和作用
java·spring boot·后端
xcl09257 小时前
幼儿托管系统开发实战:从需求分析到部署上线全指南
java·spring boot·需求分析
步行cgn7 小时前
Spring Boot 中的 YAML 完全指南
android·spring boot·firefox
csdn2015_7 小时前
java springboot项目,接收到的参数多个逗号
java·开发语言·spring boot
摇滚侠7 小时前
《SpringBoot 3:入门与应用实战》第 13 章 整合 MyBatis MyBatis 简单开发 阅读笔记 39
spring boot·笔记·mybatis
Mr.敦的私房菜8 小时前
【SpringEvent】Spring Boot / Spring Framework 事件大全
spring boot·spring
程序员阿明8 小时前
spring boot4+springAI 2加redis多轮对话存储
spring boot·redis·后端
Mr.敦的私房菜8 小时前
【SpringBoot请求记录】Spring Boot Actuator 自定义记录 HTTP 服务请求
spring boot·mvc·运维开发