有一个SpringBoot(v2.2.7)应用,里面配置了Redis缓存
pom.xml 的片段
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
当 Redis 服务启动并可用时,缓存按预期工作:@Cacheable正在缓存带有注释的方法的结果。不幸的是,当 Redis 服务不可用时,对可缓存方法的任何调用都会导致异常RedisConnectionFailureException: Unable to connect to Redis。
我想如果应用程序可以独立于缓存可用性工作(执行业务逻辑)是合理的。
可能的解决方案是:
- 自定义实现(即围绕 Redis 缓存处理错误的包装器)
- Spring中考虑Redis缓存服务健康的标准配置(如果有的话)
在 SpringBoot 中设置后备缓存的正确方法是什么?