我在 Spring 应用程序中启用了缓存,并使用 Redis 来达到目的。
但是,每当发生连接失败时,应用程序就会停止工作,而我认为它更好
跳过缓存并继续正常的执行流程。
那么,有没有人知道如何在 Spring 中优雅地做到这一点?
这是我得到的异常(exception)。
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
最佳答案
从 Spring Framework 4.1 开始,有一个 CacheErrorHandler
您可以实现以处理此类异常。引用 to the javadoc更多细节。
您可以通过您的 @Configuration
注册它类扩展 CachingConfigurerSupport
(见 errorHandler()
)。
关于spring-cache - Spring Cache with Redis - 如何在与 Redis 连接失败的情况下优雅地处理甚至跳过缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27707857/