使用Redis的Spring缓存 - 如果连接失败到Redis,如何优雅地处理甚至跳过缓存

Bin*_* Le 11 spring-data-redis spring-cache

我在Spring应用程序中启用了缓存,并使用Redis来实现此目的.但是,每当发生连接失败时,应用程序就会停止工作,而我认为最好跳过缓存并继续执行正常的执行流程.

那么,有没有人知道如何在Spring中优雅地做到这一点?

这是我得到的例外.

Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Run Code Online (Sandbox Code Playgroud)

Ste*_*oll 7

从Spring Framework 4.1开始,CacheErrorHandler您可以实现一个处理此类异常的方法.有关更多详细信息,请参阅javadoc.

您可以通过@Configuration扩展课程来注册CachingConfigurerSupport(请参阅参考资料errorHandler()).