激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring @Cacheable redis異常不影響正常業(yè)務方案

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

2021-08-09 11:30神農(nóng)L Java教程

這篇文章主要介紹了Spring @Cacheable redis異常不影響正常業(yè)務方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

背景

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

原因分析

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

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

通過@EnableCaching的類注釋可發(fā)現(xiàn),spring cache的核心配置接口為:org.springframework.cache.annotation.CachingConfigurer

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * 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),默認的CacheErrorHandler實現(xiàn)類是org.springframework.cache.interceptor.SimpleCacheErrorHandler

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 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服務器異常后,直接就阻斷了正常業(yè)務

解決方案

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class RedisConfig extends CachingConfigurerSupport {
 
  /**
   * redis數(shù)據(jù)操作異常處理。該方法處理邏輯:在日志中打印出錯誤信息,但是放行。
   * 保證redis服務器出現(xià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);
  }
}

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

原文鏈接:https://juejin.cn/post/6930545205036875784

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99www| 欧美一级淫片免费播放口 | 欧美中文字幕一区二区三区亚洲 | 久久久国产精品成人免费 | 日韩黄色一区 | 亚洲精品久久久久久 | 99久久久国产 | 精品在线一区二区三区 | 88xx成人永久免费观看 | 成人羞羞在线观看网站 | 国产成人网| 久久综合精品视频 | 特级a欧美做爰片毛片 | 亚洲成人综合网站 | 美女黄污视频 | 天天色图片 | 另类亚洲孕妇分娩网址 | 久久久电影电视剧免费看 | 九九热精品视频在线 | 97超级碰碰人国产在线观看 | 久久思思爱 | 国产免费让你躁在线视频 | 国产精品夜色视频一级区 | 日本a v免费观看 | 青草久久av | 麻豆传传媒久久久爱 | 91麻豆精品国产91久久久无需广告 | 成人性生活视频在线播放 | av在线日韩| 国产乱淫a∨片免费视频 | 亚洲国产二区 | 国产精品久久久久久久娇妻 | 国产91av视频 | 色中色综合 | 国产女厕一区二区三区在线视 | 97香蕉超级碰碰久久免费软件 | 欧美一级黄色免费 | 91嫩草丨国产丨精品入口 | 国产精品久久久久久久av三级 | h视频免费观看 | 日本一区视频在线播放 |