|
|
@@ -10,6 +10,7 @@ import java.util.Comparator;
|
|
10
|
10
|
import java.util.HashMap;
|
|
11
|
11
|
import java.util.List;
|
|
12
|
12
|
import java.util.Map;
|
|
|
13
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
13
|
14
|
import java.util.concurrent.TimeUnit;
|
|
14
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
15
|
16
|
import org.springframework.stereotype.Service;
|
|
|
@@ -42,6 +43,8 @@ public class MallStatsOpenServiceImpl implements IMallStatsOpenService
|
|
42
|
43
|
{
|
|
43
|
44
|
private static final ZoneId ZONE_SHANGHAI = ZoneId.of("Asia/Shanghai");
|
|
44
|
45
|
|
|
|
46
|
+ private final ConcurrentHashMap<Integer, Object> yearlyRefreshLocks = new ConcurrentHashMap<>();
|
|
|
47
|
+
|
|
45
|
48
|
@Autowired
|
|
46
|
49
|
private MallStatsOpenMapper statsMapper;
|
|
47
|
50
|
|
|
|
@@ -55,110 +58,161 @@ public class MallStatsOpenServiceImpl implements IMallStatsOpenService
|
|
55
|
58
|
public CategorySalesVO getCategorySales(String statYear)
|
|
56
|
59
|
{
|
|
57
|
60
|
int year = resolveStatYear(statYear);
|
|
58
|
|
- String cacheKey = StatsCacheKeys.categorySales(year);
|
|
59
|
|
- CategorySalesVO cached = redisCache.getCacheObject(cacheKey);
|
|
60
|
|
- if (cached != null)
|
|
61
|
|
- {
|
|
62
|
|
- return cached;
|
|
63
|
|
- }
|
|
64
|
|
- List<CategoryQtyRow> rows = safeList(statsMapper.selectCategoryQtyByFinishYear(year));
|
|
65
|
|
- CategorySalesVO vo = buildCategorySales(year, rows);
|
|
66
|
|
- redisCache.setCacheObject(cacheKey, vo, OpenStatsConstants.CACHE_TTL_YEARLY_HOUR, TimeUnit.HOURS);
|
|
67
|
|
- return vo;
|
|
|
61
|
+ ensureYearlyStatsCached(year);
|
|
|
62
|
+ return redisCache.getCacheObject(StatsCacheKeys.categorySales(year));
|
|
68
|
63
|
}
|
|
69
|
64
|
|
|
70
|
65
|
@Override
|
|
71
|
66
|
public HotCategoryRankVO getHotCategoryRank(String statYear)
|
|
72
|
67
|
{
|
|
73
|
68
|
int year = resolveStatYear(statYear);
|
|
74
|
|
- String cacheKey = StatsCacheKeys.hotCategory(year);
|
|
75
|
|
- HotCategoryRankVO cached = redisCache.getCacheObject(cacheKey);
|
|
76
|
|
- if (cached != null)
|
|
77
|
|
- {
|
|
78
|
|
- return cached;
|
|
79
|
|
- }
|
|
80
|
|
- List<CategoryQtyRow> rows = safeList(statsMapper.selectCategoryQtyByFinishYear(year));
|
|
81
|
|
- HotCategoryRankVO vo = buildHotCategoryRank(year, rows);
|
|
82
|
|
- redisCache.setCacheObject(cacheKey, vo, OpenStatsConstants.CACHE_TTL_YEARLY_HOUR, TimeUnit.HOURS);
|
|
83
|
|
- return vo;
|
|
|
69
|
+ ensureYearlyStatsCached(year);
|
|
|
70
|
+ return redisCache.getCacheObject(StatsCacheKeys.hotCategory(year));
|
|
84
|
71
|
}
|
|
85
|
72
|
|
|
86
|
73
|
@Override
|
|
87
|
74
|
public OrderTrendVO getOrderTrend(String statYear)
|
|
88
|
75
|
{
|
|
89
|
76
|
int year = resolveStatYear(statYear);
|
|
90
|
|
- String cacheKey = StatsCacheKeys.orderTrend(year);
|
|
91
|
|
- OrderTrendVO cached = redisCache.getCacheObject(cacheKey);
|
|
92
|
|
- if (cached != null)
|
|
93
|
|
- {
|
|
94
|
|
- return cached;
|
|
95
|
|
- }
|
|
96
|
|
- List<MonthCountRow> rows = safeList(statsMapper.selectOrderCountByYear(year));
|
|
97
|
|
- OrderTrendVO vo = buildOrderTrend(year, rows);
|
|
98
|
|
- redisCache.setCacheObject(cacheKey, vo, OpenStatsConstants.CACHE_TTL_YEARLY_HOUR, TimeUnit.HOURS);
|
|
99
|
|
- return vo;
|
|
|
77
|
+ ensureYearlyStatsCached(year);
|
|
|
78
|
+ return redisCache.getCacheObject(StatsCacheKeys.orderTrend(year));
|
|
100
|
79
|
}
|
|
101
|
80
|
|
|
102
|
81
|
@Override
|
|
103
|
82
|
public ShopEntryVO getShopEntry(String statYear)
|
|
104
|
83
|
{
|
|
105
|
84
|
int year = resolveStatYear(statYear);
|
|
106
|
|
- String cacheKey = StatsCacheKeys.shopEntry(year);
|
|
107
|
|
- ShopEntryVO cached = redisCache.getCacheObject(cacheKey);
|
|
108
|
|
- if (cached != null)
|
|
109
|
|
- {
|
|
110
|
|
- return cached;
|
|
111
|
|
- }
|
|
112
|
|
- List<MonthCountRow> rows = safeList(statsMapper.selectShopCountByYear(year));
|
|
113
|
|
- ShopEntryVO vo = buildShopEntry(year, rows);
|
|
114
|
|
- redisCache.setCacheObject(cacheKey, vo, OpenStatsConstants.CACHE_TTL_YEARLY_HOUR, TimeUnit.HOURS);
|
|
115
|
|
- return vo;
|
|
|
85
|
+ ensureYearlyStatsCached(year);
|
|
|
86
|
+ return redisCache.getCacheObject(StatsCacheKeys.shopEntry(year));
|
|
116
|
87
|
}
|
|
117
|
88
|
|
|
118
|
89
|
@Override
|
|
119
|
90
|
public RegionRankVO getRegionRank(String statYear)
|
|
120
|
91
|
{
|
|
121
|
92
|
int year = resolveStatYear(statYear);
|
|
122
|
|
- String cacheKey = StatsCacheKeys.regionRank(year);
|
|
123
|
|
- RegionRankVO cached = redisCache.getCacheObject(cacheKey);
|
|
124
|
|
- if (cached != null)
|
|
125
|
|
- {
|
|
126
|
|
- return cached;
|
|
127
|
|
- }
|
|
128
|
|
- List<RegionAmountRow> rows = safeList(statsMapper.selectRegionAmountByYear(year));
|
|
129
|
|
- RegionRankVO vo = buildRegionRank(year, rows);
|
|
130
|
|
- redisCache.setCacheObject(cacheKey, vo, OpenStatsConstants.CACHE_TTL_YEARLY_HOUR, TimeUnit.HOURS);
|
|
131
|
|
- return vo;
|
|
|
93
|
+ ensureYearlyStatsCached(year);
|
|
|
94
|
+ return redisCache.getCacheObject(StatsCacheKeys.regionRank(year));
|
|
132
|
95
|
}
|
|
133
|
96
|
|
|
134
|
97
|
@Override
|
|
135
|
98
|
public ReviewWordCloudVO getReviewWordCloud()
|
|
136
|
99
|
{
|
|
137
|
|
- String cacheKey = StatsCacheKeys.wordCloud();
|
|
138
|
|
- ReviewWordCloudVO cached = redisCache.getCacheObject(cacheKey);
|
|
139
|
|
- if (cached != null)
|
|
140
|
|
- {
|
|
141
|
|
- return cached;
|
|
142
|
|
- }
|
|
143
|
|
- List<String> contents = safeList(statsMapper.selectReviewContentsForWordCloud());
|
|
144
|
|
- ReviewWordCloudVO vo = wordCloudSupport.buildTopWords(contents);
|
|
145
|
|
- redisCache.setCacheObject(cacheKey, vo, OpenStatsConstants.CACHE_TTL_WORDCLOUD_HOUR, TimeUnit.HOURS);
|
|
146
|
|
- return vo;
|
|
|
100
|
+ ensureWordCloudCached();
|
|
|
101
|
+ return redisCache.getCacheObject(StatsCacheKeys.wordCloud());
|
|
147
|
102
|
}
|
|
148
|
103
|
|
|
149
|
104
|
@Override
|
|
150
|
105
|
public StatsOverviewVO getOverview(String statYear)
|
|
151
|
106
|
{
|
|
|
107
|
+ int year = resolveStatYear(statYear);
|
|
|
108
|
+ ensureOverviewStatsCached(year);
|
|
152
|
109
|
StatsOverviewVO overview = new StatsOverviewVO();
|
|
153
|
|
- overview.setCategorySales(getCategorySales(statYear));
|
|
154
|
|
- overview.setHotCategoryRank(getHotCategoryRank(statYear));
|
|
155
|
|
- overview.setOrderTrend(getOrderTrend(statYear));
|
|
156
|
|
- overview.setShopEntry(getShopEntry(statYear));
|
|
157
|
|
- overview.setRegionRank(getRegionRank(statYear));
|
|
158
|
|
- overview.setReviewWordCloud(getReviewWordCloud());
|
|
|
110
|
+ overview.setCategorySales(redisCache.getCacheObject(StatsCacheKeys.categorySales(year)));
|
|
|
111
|
+ overview.setHotCategoryRank(redisCache.getCacheObject(StatsCacheKeys.hotCategory(year)));
|
|
|
112
|
+ overview.setOrderTrend(redisCache.getCacheObject(StatsCacheKeys.orderTrend(year)));
|
|
|
113
|
+ overview.setShopEntry(redisCache.getCacheObject(StatsCacheKeys.shopEntry(year)));
|
|
|
114
|
+ overview.setRegionRank(redisCache.getCacheObject(StatsCacheKeys.regionRank(year)));
|
|
|
115
|
+ overview.setReviewWordCloud(redisCache.getCacheObject(StatsCacheKeys.wordCloud()));
|
|
159
|
116
|
return overview;
|
|
160
|
117
|
}
|
|
161
|
118
|
|
|
|
119
|
+ private void ensureOverviewStatsCached(int year)
|
|
|
120
|
+ {
|
|
|
121
|
+ if (isYearlyStatsCached(year) && isWordCloudCached())
|
|
|
122
|
+ {
|
|
|
123
|
+ return;
|
|
|
124
|
+ }
|
|
|
125
|
+ Object lock = yearlyRefreshLocks.computeIfAbsent(year, key -> new Object());
|
|
|
126
|
+ synchronized (lock)
|
|
|
127
|
+ {
|
|
|
128
|
+ if (!isYearlyStatsCached(year))
|
|
|
129
|
+ {
|
|
|
130
|
+ refreshYearlyStatsCache(year);
|
|
|
131
|
+ }
|
|
|
132
|
+ if (!isWordCloudCached())
|
|
|
133
|
+ {
|
|
|
134
|
+ refreshWordCloudCache();
|
|
|
135
|
+ }
|
|
|
136
|
+ }
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ private void ensureYearlyStatsCached(int year)
|
|
|
140
|
+ {
|
|
|
141
|
+ if (isYearlyStatsCached(year))
|
|
|
142
|
+ {
|
|
|
143
|
+ return;
|
|
|
144
|
+ }
|
|
|
145
|
+ Object lock = yearlyRefreshLocks.computeIfAbsent(year, key -> new Object());
|
|
|
146
|
+ synchronized (lock)
|
|
|
147
|
+ {
|
|
|
148
|
+ if (!isYearlyStatsCached(year))
|
|
|
149
|
+ {
|
|
|
150
|
+ refreshYearlyStatsCache(year);
|
|
|
151
|
+ }
|
|
|
152
|
+ }
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ private void ensureWordCloudCached()
|
|
|
156
|
+ {
|
|
|
157
|
+ if (isWordCloudCached())
|
|
|
158
|
+ {
|
|
|
159
|
+ return;
|
|
|
160
|
+ }
|
|
|
161
|
+ synchronized (MallStatsOpenServiceImpl.class)
|
|
|
162
|
+ {
|
|
|
163
|
+ if (!isWordCloudCached())
|
|
|
164
|
+ {
|
|
|
165
|
+ refreshWordCloudCache();
|
|
|
166
|
+ }
|
|
|
167
|
+ }
|
|
|
168
|
+ }
|
|
|
169
|
+
|
|
|
170
|
+ private boolean isYearlyStatsCached(int year)
|
|
|
171
|
+ {
|
|
|
172
|
+ for (String cacheKey : StatsCacheKeys.yearlyStatKeys(year))
|
|
|
173
|
+ {
|
|
|
174
|
+ if (redisCache.getCacheObject(cacheKey) == null)
|
|
|
175
|
+ {
|
|
|
176
|
+ return false;
|
|
|
177
|
+ }
|
|
|
178
|
+ }
|
|
|
179
|
+ return true;
|
|
|
180
|
+ }
|
|
|
181
|
+
|
|
|
182
|
+ private boolean isWordCloudCached()
|
|
|
183
|
+ {
|
|
|
184
|
+ return redisCache.getCacheObject(StatsCacheKeys.wordCloud()) != null;
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ private void refreshYearlyStatsCache(int year)
|
|
|
188
|
+ {
|
|
|
189
|
+ List<CategoryQtyRow> categoryRows = safeList(statsMapper.selectCategoryQtyByFinishYear(year));
|
|
|
190
|
+ CategorySalesVO categorySales = buildCategorySales(year, categoryRows);
|
|
|
191
|
+ HotCategoryRankVO hotCategory = buildHotCategoryRank(year, new ArrayList<>(categoryRows));
|
|
|
192
|
+ OrderTrendVO orderTrend = buildOrderTrend(year, safeList(statsMapper.selectOrderCountByYear(year)));
|
|
|
193
|
+ ShopEntryVO shopEntry = buildShopEntry(year, safeList(statsMapper.selectShopCountByYear(year)));
|
|
|
194
|
+ RegionRankVO regionRank = buildRegionRank(year, safeList(statsMapper.selectRegionAmountByYear(year)));
|
|
|
195
|
+
|
|
|
196
|
+ cacheYearlyStat(StatsCacheKeys.categorySales(year), categorySales);
|
|
|
197
|
+ cacheYearlyStat(StatsCacheKeys.hotCategory(year), hotCategory);
|
|
|
198
|
+ cacheYearlyStat(StatsCacheKeys.orderTrend(year), orderTrend);
|
|
|
199
|
+ cacheYearlyStat(StatsCacheKeys.shopEntry(year), shopEntry);
|
|
|
200
|
+ cacheYearlyStat(StatsCacheKeys.regionRank(year), regionRank);
|
|
|
201
|
+ }
|
|
|
202
|
+
|
|
|
203
|
+ private void refreshWordCloudCache()
|
|
|
204
|
+ {
|
|
|
205
|
+ List<String> contents = safeList(statsMapper.selectReviewContentsForWordCloud());
|
|
|
206
|
+ ReviewWordCloudVO vo = wordCloudSupport.buildTopWords(contents);
|
|
|
207
|
+ redisCache.setCacheObject(StatsCacheKeys.wordCloud(), vo,
|
|
|
208
|
+ OpenStatsConstants.CACHE_TTL_WORDCLOUD_HOUR, TimeUnit.HOURS);
|
|
|
209
|
+ }
|
|
|
210
|
+
|
|
|
211
|
+ private void cacheYearlyStat(String cacheKey, Object value)
|
|
|
212
|
+ {
|
|
|
213
|
+ redisCache.setCacheObject(cacheKey, value, OpenStatsConstants.CACHE_TTL_YEARLY_HOUR, TimeUnit.HOURS);
|
|
|
214
|
+ }
|
|
|
215
|
+
|
|
162
|
216
|
private CategorySalesVO buildCategorySales(int statYear, List<CategoryQtyRow> rows)
|
|
163
|
217
|
{
|
|
164
|
218
|
CategorySalesVO vo = new CategorySalesVO();
|