国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring @Cacheable redis異常不影響正常業務方案

瀏覽:5日期:2023-07-23 09:27:20
背景

項目中,使用@Cacheable進行數據緩存。發現:當redis宕機之后,@Cacheable注解的方法并未進行緩存沖突,而是直接拋出異常。而這樣的異常會導致服務不可用。

原因分析

我們是通過@EnableCaching進行緩存啟用的,因此可以先看@EnableCaching的相關注釋

Spring @Cacheable redis異常不影響正常業務方案

通過@EnableCaching的類注釋可發現,spring cache的核心配置接口為:org.springframework.cache.annotation.CachingConfigurer

/** * Interface to be implemented by @{@link org.springframework.context.annotation.Configuration * Configuration} classes annotated with @{@link EnableCaching} that wish or need to * specify explicitly how caches are resolved and how keys are generated for annotation-driven * cache management. Consider extending {@link CachingConfigurerSupport}, which provides a * stub implementation of all interface methods. * * <p>See @{@link EnableCaching} for general examples and context; see * {@link #cacheManager()}, {@link #cacheResolver()} and {@link #keyGenerator()} * for detailed instructions. * * @author Chris Beams * @author Stephane Nicoll * @since 3.1 * @see EnableCaching * @see CachingConfigurerSupport */public interface CachingConfigurer { /** * Return the cache manager bean to use for annotation-driven cache * management. A default {@link CacheResolver} will be initialized * behind the scenes with this cache manager. For more fine-grained * management of the cache resolution, consider setting the * {@link CacheResolver} directly. * <p>Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public CacheManager cacheManager() { * // configure and return CacheManager instance * } * // ... * } * </pre> * See @{@link EnableCaching} for more complete examples. */ CacheManager cacheManager(); /** * Return the {@link CacheResolver} bean to use to resolve regular caches for * annotation-driven cache management. This is an alternative and more powerful * option of specifying the {@link CacheManager} to use. * <p>If both a {@link #cacheManager()} and {@code #cacheResolver()} are set, * the cache manager is ignored. * <p>Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public CacheResolver cacheResolver() { * // configure and return CacheResolver instance * } * // ... * } * </pre> * See {@link EnableCaching} for more complete examples. */ CacheResolver cacheResolver(); /** * Return the key generator bean to use for annotation-driven cache management. * Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public KeyGenerator keyGenerator() { * // configure and return KeyGenerator instance * } * // ... * } * </pre> * See @{@link EnableCaching} for more complete examples. */ KeyGenerator keyGenerator(); /** * Return the {@link CacheErrorHandler} to use to handle cache-related errors. * <p>By default,{@link org.springframework.cache.interceptor.SimpleCacheErrorHandler} * is used and simply throws the exception back at the client. * <p>Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public CacheErrorHandler errorHandler() { * // configure and return CacheErrorHandler instance * } * // ... * } * </pre> * See @{@link EnableCaching} for more complete examples. */ CacheErrorHandler errorHandler();}

該接口errorHandler方法可配置異常的處理方式。通過該方法上的注釋可以發現,默認的CacheErrorHandler實現類是org.springframework.cache.interceptor.SimpleCacheErrorHandler

/** * A simple {@link CacheErrorHandler} that does not handle the * exception at all, simply throwing it back at the client. * * @author Stephane Nicoll * @since 4.1 */public class SimpleCacheErrorHandler implements CacheErrorHandler { @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) { throw exception; } @Override public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) { throw exception; } @Override public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) { throw exception; } @Override public void handleCacheClearError(RuntimeException exception, Cache cache) { throw exception; }}

SimpleCacheErrorHandler類注釋上說明的很清楚:對cache的異常不做任何處理,直接將該異常拋給客戶端。因此默認的情況下,redis服務器異常后,直接就阻斷了正常業務

解決方案

通過上面的分析可知,我們可以通過自定義CacheErrorHandler來干預@Cacheable的異常處理邏輯。具體代碼如下:

public class RedisConfig extends CachingConfigurerSupport { /** * redis數據操作異常處理。該方法處理邏輯:在日志中打印出錯誤信息,但是放行。 * 保證redis服務器出現連接等問題的時候不影響程序的正常運行 */ @Override public CacheErrorHandler errorHandler() { return new CacheErrorHandler() { @Override public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {handleRedisErrorException(exception, key); } @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {handleRedisErrorException(exception, key); } @Override public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {handleRedisErrorException(exception, key); } @Override public void handleCacheClearError(RuntimeException exception, Cache cache) {handleRedisErrorException(exception, null); } }; } protected void handleRedisErrorException(RuntimeException exception, Object key) { log.error('redis異常:key=[{}]', key, exception); }}

到此這篇關于Spring @Cacheable redis異常不影響正常業務方案的文章就介紹到這了,更多相關Spring @Cacheable redis異常內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 亚洲国产第一区二区香蕉 | 免费看a级毛片 | 91大神大战丝袜美女在线观看 | 欧美午夜三级我不卡在线观看 | 在线免费成人 | 国产在线观看网址你懂得 | 亚洲 欧美 国产 中文 | 久久综合九九亚洲一区 | 看一级特黄a大一片 | 91丨九色丨首页在线观看 | 中文字幕亚洲高清综合 | 成人在线精品 | 一级做a爰片久久毛片16 | 夜色精品国产一区二区 | 欧美成人高清免费大片观看 | 又黄又湿又爽 | 国产精品久久免费 | 久久国产首页 | 欧美午夜视频一区二区 | 90岁老太婆一级毛片 | 一区二区三区伦理 | 精品国产亚洲一区二区在线3d | 怡红院在线观看在线视频 | 色视频在线免费 | 成人免费手机在线看网站 | 第四色成人网 | 一级做a级爰片性色毛片视频 | 日本成本人视频 | 美女在线看永久免费网址 | 精品在线观看一区 | 亚洲自拍高清 | 日韩综合久久 | a在线v| 日韩一区二区在线播放 | 国产日b视频 | 色欧美hdvideosxs4k | 久久香蕉精品视频 | 亚洲三级在线免费观看 | 亚洲国产欧洲精品路线久久 | 日韩一级影院 | 欧美专区视频 |