Просмотр исходного кода

交易市场平台(供应商)

wwh дней назад: 6
Родитель
Сommit
255bc54f78

+ 37 - 5
baqing-admin/src/main/java/com/ruoyi/web/modules/screen/mallstats/MallStatsOpenApiClientImpl.java

@@ -27,7 +27,9 @@ public class MallStatsOpenApiClientImpl implements MallStatsOpenApiClient
27
 {
27
 {
28
     private static final Logger log = LoggerFactory.getLogger(MallStatsOpenApiClientImpl.class);
28
     private static final Logger log = LoggerFactory.getLogger(MallStatsOpenApiClientImpl.class);
29
 
29
 
30
-    private static final String CACHE_KEY_PREFIX = "bigscreen:mallstats:overview:";
30
+    private static final String CACHE_KEY_PREFIX = "bigscreen:mallstats:overview:v2:";
31
+
32
+    private final ObjectMapper objectMapper = new ObjectMapper();
31
 
33
 
32
     private final MallStatsOpenProperties properties;
34
     private final MallStatsOpenProperties properties;
33
 
35
 
@@ -37,8 +39,6 @@ public class MallStatsOpenApiClientImpl implements MallStatsOpenApiClient
37
 
39
 
38
     private final RedisCache redisCache;
40
     private final RedisCache redisCache;
39
 
41
 
40
-    private final ObjectMapper objectMapper = new ObjectMapper();
41
-
42
     public MallStatsOpenApiClientImpl(
42
     public MallStatsOpenApiClientImpl(
43
             MallStatsOpenProperties properties,
43
             MallStatsOpenProperties properties,
44
             @Qualifier("mallStatsRestTemplate") RestTemplate restTemplate,
44
             @Qualifier("mallStatsRestTemplate") RestTemplate restTemplate,
@@ -59,7 +59,7 @@ public class MallStatsOpenApiClientImpl implements MallStatsOpenApiClient
59
             return null;
59
             return null;
60
         }
60
         }
61
         String cacheKey = CACHE_KEY_PREFIX + statYear;
61
         String cacheKey = CACHE_KEY_PREFIX + statYear;
62
-        MallStatsOverviewVo cached = redisCache.getCacheObject(cacheKey);
62
+        MallStatsOverviewVo cached = readCachedOverview(cacheKey);
63
         if (cached != null)
63
         if (cached != null)
64
         {
64
         {
65
             return cached;
65
             return cached;
@@ -70,7 +70,7 @@ public class MallStatsOpenApiClientImpl implements MallStatsOpenApiClient
70
             if (overview != null)
70
             if (overview != null)
71
             {
71
             {
72
                 int ttl = Math.max(1, properties.getCacheTtlMinutes());
72
                 int ttl = Math.max(1, properties.getCacheTtlMinutes());
73
-                redisCache.setCacheObject(cacheKey, overview, ttl, TimeUnit.MINUTES);
73
+                writeCachedOverview(cacheKey, overview, ttl);
74
             }
74
             }
75
             return overview;
75
             return overview;
76
         }
76
         }
@@ -81,6 +81,38 @@ public class MallStatsOpenApiClientImpl implements MallStatsOpenApiClient
81
         }
81
         }
82
     }
82
     }
83
 
83
 
84
+    private MallStatsOverviewVo readCachedOverview(String cacheKey)
85
+    {
86
+        try
87
+        {
88
+            String json = redisCache.getCacheObject(cacheKey);
89
+            if (StringUtils.isEmpty(json))
90
+            {
91
+                return null;
92
+            }
93
+            return objectMapper.readValue(json, MallStatsOverviewVo.class);
94
+        }
95
+        catch (Exception ex)
96
+        {
97
+            log.warn("商城统计缓存反序列化失败,将重新拉取 {}: {}", cacheKey, ex.getMessage());
98
+            redisCache.deleteObject(cacheKey);
99
+            return null;
100
+        }
101
+    }
102
+
103
+    private void writeCachedOverview(String cacheKey, MallStatsOverviewVo overview, int ttlMinutes)
104
+    {
105
+        try
106
+        {
107
+            String json = objectMapper.writeValueAsString(overview);
108
+            redisCache.setCacheObject(cacheKey, json, ttlMinutes, TimeUnit.MINUTES);
109
+        }
110
+        catch (Exception ex)
111
+        {
112
+            log.warn("商城统计缓存写入失败 {}: {}", cacheKey, ex.getMessage());
113
+        }
114
+    }
115
+
84
     private MallStatsOverviewVo requestOverview(int statYear) throws Exception
116
     private MallStatsOverviewVo requestOverview(int statYear) throws Exception
85
     {
117
     {
86
         String base = trimTrailingSlash(properties.getBaseUrl());
118
         String base = trimTrailingSlash(properties.getBaseUrl());