一、下载与安装
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 服务(推荐,永久运行)
操作:
- 找到目录里的
install_redis_service.bat。 - 右键 → 以管理员身份运行。
- 按两次回车(默认路径)。
- 看到"服务已经启动成功"就装好了。
效果:
- 开机自动启动 ✅
- 后台静默运行 ✅
- 关了终端也不影响 ✅
管理命令:
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 # 查看过期时间(还剩多少秒)
验证流程:
- 第一次
curl http://localhost:8080/api/users/1→ 查数据库,控制台打印 SQL → 结果存 Redis。 - 第二次
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 服务 |