av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案

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

項(xiàng)目中,使用@Cacheable進(jìn)行數(shù)據(jù)緩存。發(fā)現(xiàn):當(dāng)redis宕機(jī)之后,@Cacheable注解的方法并未進(jìn)行緩存沖突,而是直接拋出異常。而這樣的異常會(huì)導(dǎo)致服務(wù)不可用。

原因分析

我們是通過@EnableCaching進(jìn)行緩存啟用的,因此可以先看@EnableCaching的相關(guān)注釋

Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案

通過@EnableCaching的類注釋可發(fā)現(xiàn),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方法可配置異常的處理方式。通過該方法上的注釋可以發(fā)現(xiàn),默認(rèn)的CacheErrorHandler實(shí)現(xiàn)類是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的異常不做任何處理,直接將該異常拋給客戶端。因此默認(rèn)的情況下,redis服務(wù)器異常后,直接就阻斷了正常業(yè)務(wù)

解決方案

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

public class RedisConfig extends CachingConfigurerSupport { /** * redis數(shù)據(jù)操作異常處理。該方法處理邏輯:在日志中打印出錯(cuò)誤信息,但是放行。 * 保證redis服務(wù)器出現(xiàn)連接等問題的時(shí)候不影響程序的正常運(yùn)行 */ @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); }}

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: www.com久久久 | 在线观看视频一区 | av激情在线 | 精品久久中文字幕 | 亚洲日本欧美 | 精精国产xxxx视频在线播放 | 国产精品久久久久久久久久久新郎 | 日韩在线视频一区二区三区 | 午夜免费观看网站 | 成人欧美一区二区三区白人 | 久久九九免费 | 久久国产视频网 | 毛片免费观看 | 色综合久久久 | 国产精品一区二区三区久久 | 日本成人中文字幕在线观看 | 日本亚洲欧美 | 日韩不卡视频在线观看 | 国产精品美女久久久 | 欧美一区二区三区在线看 | 国产精品成人品 | 亚洲欧美日韩精品 | 日本三级网址 | 国产欧美一级二级三级在线视频 | 99久久国产精 | 黄色大片在线播放 | 日韩欧美成人精品 | 资源首页二三区 | 国产一区二区影院 | 国产精品三级 | 欧美色999| 免费的色网站 | 国产精品一区二区三区在线 | 天天色天天 | 中文字幕在线看 | 国产成人精品久久二区二区91 | 91在线| 亚洲午夜网| 亚洲一级黄色 | 久久91| 不卡一区 |