日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

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

瀏覽:55日期: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
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精品v日韩精品v欧美精品网站| 欧美在线日韩| 国产精品乱战久久久| 日韩精品欧美大片| 亚洲美女久久| 亚洲精品极品| 日韩一区免费| 国产美女亚洲精品7777| 国产无遮挡裸体免费久久| 国产亚洲字幕| 美女精品久久| 丁香婷婷久久| 国产成人1区| 神马久久午夜| 亚洲第一精品影视| 99pao成人国产永久免费视频| 久久亚洲色图| 日韩精品1区2区3区| 你懂的国产精品永久在线| 久久av免费看| 92国产精品| 亚洲婷婷在线| 男人天堂欧美日韩| 日本va欧美va精品| 另类综合日韩欧美亚洲| 日本不卡免费高清视频在线| 亚洲国内精品| 精品欧美久久| 免费人成精品欧美精品| 欧美一区二区三区免费看| 牛牛精品成人免费视频| 丝袜诱惑一区二区| 丝袜美腿亚洲一区二区图片| 69堂精品视频在线播放| 日韩理论视频| 中文在线日韩| 麻豆精品视频在线观看| 久久亚洲成人| 欧美亚洲自偷自偷| 久久天堂av| 亚洲精品看片| 国产一区2区| 蜜桃tv一区二区三区| 中文字幕免费一区二区| 精品国产乱码久久久久久1区2匹| 久久久水蜜桃av免费网站| 亚洲精品韩国| 久久久久久夜| 亚洲日本三级| 激情综合婷婷| 中文国产一区| 老牛国内精品亚洲成av人片| 欧美日韩在线网站| 日韩高清三区| 91精品精品| 国产毛片精品| 黄色精品网站| 牛牛精品成人免费视频| 亚洲精品一区二区妖精| 国产精品tv| 手机精品视频在线观看| 欧美一级鲁丝片| 国产日韩一区二区三免费高清 | 婷婷综合电影| 日韩理论视频| 国产调教一区二区三区| 免费av一区二区三区四区| 国产亚洲精品美女久久久久久久久久| 亚洲成人精品| 精品欧美视频| 四虎成人精品一区二区免费网站| 狂野欧美性猛交xxxx| 免费精品视频最新在线| 92国产精品| 美女视频黄免费的久久| 中文字幕一区二区三区日韩精品| 日韩精品免费一区二区在线观看 | 欧美不卡高清| 老司机精品视频网| 日本成人在线一区| 亚洲欧洲午夜| 亚洲二区精品| 国产 日韩 欧美 综合 一区| 免费人成黄页网站在线一区二区| 久久久久99| 成人一区而且| 欧美国产中文高清| 91精品国产自产在线丝袜啪| 视频在线观看一区| 好看不卡的中文字幕| 久久婷婷久久| 日韩欧美二区| 国产自产自拍视频在线观看 | 国产精品亚洲片在线播放| 中文不卡在线| 久久av一区二区三区| 91精品一区二区三区综合在线爱| 免费一级欧美片在线观看网站| 日本一区二区三区视频在线看| 亚洲一区国产| 国产亚洲激情| 99pao成人国产永久免费视频| 久久国产亚洲精品| 亚洲精品在线影院| 国产91在线精品| 成人午夜网址| 久久久久久网| 日韩国产专区| 欧美精品资源| 麻豆精品蜜桃| 久久精品国内一区二区三区水蜜桃| 黑森林国产精品av| 精品久久视频| 91视频一区| 麻豆视频在线观看免费网站黄 | 日韩精品一区二区三区免费观影| 国产白浆在线免费观看| 精品久久在线| 色综合狠狠操| 色偷偷色偷偷色偷偷在线视频| 国产在线看片免费视频在线观看| 久久精品国产网站| 精品五月天堂| 中文字幕在线看片| 亚洲爱爱视频| 亚洲无线一线二线三线区别av| 亚洲精品91| 日韩精品一卡二卡三卡四卡无卡| 亚洲一区二区三区在线免费| 日本亚洲最大的色成网站www | 国产精品对白| 国产精品久久国产愉拍| 麻豆国产91在线播放| 成午夜精品一区二区三区软件| 偷拍精品精品一区二区三区| 午夜免费一区| 免费观看在线色综合| 日本成人在线一区| 麻豆一区在线| 91精品精品| 一区二区国产在线| 久久精品999| 成人高清一区| 欧美aa国产视频| 亚洲欧洲国产精品一区| 国产精品白丝av嫩草影院| 水蜜桃精品av一区二区| 久久裸体视频| 视频一区在线播放| 国产日韩欧美一区二区三区在线观看 | 免费视频一区二区三区在线观看| 国产一区2区| 亚洲香蕉网站| 日韩国产在线观看| 精品美女在线视频| 国产在线成人| 日韩精品三级| 国产成人免费av一区二区午夜| 99久久视频| 中文字幕日韩欧美精品高清在线| 国产精品免费精品自在线观看| 国产理论在线| 久久成人精品| 国产精品久久| 亚洲一本视频| 久久激情五月婷婷| 成人在线视频免费| 六月天综合网| 久久久久伊人| 国产亚洲在线观看| 国产精东传媒成人av电影| 激情久久五月| 欧美精品观看| 91精品国产成人观看| 亚洲欧洲免费| 日韩欧美不卡| 日韩精品国产欧美| 日韩欧美午夜| 日本成人精品| 91精品一区二区三区综合| 欧美一级二级视频| 国产一区欧美| 欧美国产视频| 免费日韩一区二区| 国产精品久久观看| 日韩中文字幕无砖| 91精品精品| 卡一卡二国产精品| 日韩中文字幕91| 捆绑调教日本一区二区三区| 蜜臀av亚洲一区中文字幕| 成人台湾亚洲精品一区二区| 亚洲午夜国产成人| 91精品一区二区三区综合在线爱| 国产精品永久| 久久国产福利| 日韩欧美一区二区三区在线观看 | 精品72久久久久中文字幕| 亚洲一区激情| 国产精品久久久久久久免费观看|