Spring Boot结合嵌入式Redis实现REST数据缓存的可行性及配置方法
嵌入式Redis在Spring Boot缓存中的使用方案
当然可以不用单独安装Redis服务器!嵌入式Redis是开发、测试阶段非常方便的选择,完全能满足你在Spring Boot中缓存REST接口数据的需求,下面给你详细说说适配版本和配置步骤:
适配的Spring Boot版本
一般来说,Spring Boot 2.x及以上版本都能很好兼容嵌入式Redis,更推荐使用Spring Boot 2.2+(包括3.x版本)——这个区间的版本对Redis缓存的自动配置逻辑更完善,和主流嵌入式Redis库的兼容性也更稳定。如果是Spring Boot 3.x,记得选支持Java 17的嵌入式Redis版本(比如4.0.0及以上)。
具体配置步骤
1. 添加依赖
首先在你的项目构建文件中加入Spring Boot Redis Starter和嵌入式Redis的依赖:
Maven(pom.xml)
<dependencies> <!-- Spring Boot Redis核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 嵌入式Redis依赖 --> <dependency> <groupId>redis.embedded</groupId> <artifactId>embedded-redis</artifactId> <!-- Spring Boot 2.x选2.8.0,3.x选4.0.0+ --> <version>2.8.0</version> <!-- 若只在开发/测试用,可加<scope>test</scope> --> </dependency> </dependencies>
Gradle(build.gradle)
dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-redis' // Spring Boot 2.x用2.8.0,3.x用4.0.0+ implementation 'redis.embedded:embedded-redis:2.8.0' }
2. 配置嵌入式Redis实例
创建一个配置类,用来启动和关闭嵌入式Redis服务器,还可以通过@Profile指定只在特定环境启用(比如dev/test):
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import redis.embedded.RedisServer; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Configuration @Profile({"dev", "test"}) // 仅在开发、测试环境启用嵌入式Redis public class EmbeddedRedisConfig { private RedisServer redisServer; @PostConstruct public void startEmbeddedRedis() { // 默认使用6379端口,如需自定义可传参new RedisServer(6380) redisServer = new RedisServer(); redisServer.start(); } @PreDestroy public void stopEmbeddedRedis() { if (redisServer != null) { redisServer.stop(); } } }
3. 配置Spring缓存
在application.properties或application.yml中开启Redis缓存,并配置基础属性(嵌入式Redis默认localhost:6379,所以大部分配置可以用默认值):
application.properties
# 开启缓存功能 spring.cache.type=redis # Redis服务器地址(嵌入式默认localhost) spring.redis.host=localhost # Redis端口(默认6379,自定义端口的话要对应修改) spring.redis.port=6379 # 可选:设置缓存默认过期时间(这里是10分钟) spring.cache.redis.time-to-live=600000
4. 启用缓存并使用
在Spring Boot启动类上加上@EnableCaching注解开启缓存支持,然后在需要缓存的Service方法上用@Cacheable等注解标记:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class RestDataService { // 将方法返回结果缓存到名为"restData"的缓存中,以id作为缓存key @Cacheable(value = "restData", key = "#id") public String fetchDataFromRestApi(String id) { // 这里写调用REST Web Service获取数据的逻辑 return "Fetched data for ID: " + id; } }
注意事项
- 嵌入式Redis只适合开发、测试环境,生产环境一定要使用独立部署的Redis服务器/集群——嵌入式Redis是内存级实例,重启就会丢失所有缓存数据,性能和稳定性也远不如独立Redis。
- 如果遇到版本兼容性问题,优先检查嵌入式Redis的版本是否和Spring Boot版本匹配(比如Spring Boot 3.x需要嵌入式Redis 4.0.0+)。
内容的提问来源于stack exchange,提问作者NAZEHA




