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 服务
相关推荐
Flittly12 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
小强库计算机毕业设计18 小时前
基于 Spring Boot + Vue3 的校园管理系统
java·spring boot·后端·项目实战·管理系统·技术分享·校园管理系统实战
毕设单片机21 小时前
顺顺救助站流浪动物领养系统的设计与实现46649----SpringBoot + Vue.js + MySQL|领养审核、寻宠发布、救助站点与动物论坛协同
java·spring boot·管理系统·流浪动物
云烟成雨TD21 小时前
Micrometer 系列【63】统一观测:基于 Spring Boot 的生产级演示案例 | 基于 OTLP 集成 Prometheus + Jaeger
spring boot·云原生·prometheus
csdn2015_21 小时前
springboot +mybatis 查询myql开启程序级别缓存
spring boot·缓存·mybatis
chuan.bai21 小时前
Java RAG 实战(第 8 篇):Spring Boot RAG 查询 API
java·spring boot·贪心算法
vx-程序开发1 天前
springboot旅游推介平台---附源码24175
java·spring boot·python·spring cloud·eclipse·django·idea
小蒜学长1 天前
“喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·宠物
xbgRS1 天前
springboot的自动装配
java·spring boot
嘻哈∠※1 天前
0061基于 SpringBoot 的投稿与稿件处理系统设计与实现
java·spring boot·后端