Skip to content

Commit 2268786

Browse files
LIZHUANGZHUANG001LIZHUANGZHUANG001
authored andcommitted
fix/修改固定频率注解格式&destroy时关闭线程池&对是否已经存在固定刷新的任务做判断
1 parent e28c7bb commit 2268786

4 files changed

Lines changed: 29 additions & 30 deletions

File tree

src/main/java/com/jarvis/cache/AutoLoadHandler.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class AutoLoadHandler {
3737
*/
3838
private final ConcurrentHashMap<CacheKeyTO, AutoLoadTO> autoLoadMap;
3939

40+
/**
41+
* 固定频率刷新队列
42+
*/
43+
static CopyOnWriteArrayList<CacheKeyTO> fixRateRefreshArrayList;
44+
4045
private final CacheHandler cacheHandler;
4146

4247
/**
@@ -62,7 +67,7 @@ public class AutoLoadHandler {
6267
private final AutoLoadConfig config;
6368

6469

65-
private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
70+
static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
6671

6772
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() {
6873
return scheduledThreadPoolExecutor;
@@ -79,6 +84,7 @@ public AutoLoadHandler(CacheHandler cacheHandler, AutoLoadConfig config) {
7984
this.running = true;
8085
this.threads = new Thread[this.config.getThreadCnt()];
8186
this.autoLoadMap = new ConcurrentHashMap<CacheKeyTO, AutoLoadTO>(this.config.getMaxElement());
87+
fixRateRefreshArrayList = new CopyOnWriteArrayList<CacheKeyTO>();
8288
this.autoLoadQueue = new LinkedBlockingQueue<AutoLoadTO>(this.config.getMaxElement());
8389
this.sortThread = new Thread(new SortRunnable());
8490
this.sortThread.setDaemon(true);
@@ -162,12 +168,18 @@ public AutoLoadTO putIfAbsent(CacheKeyTO cacheKey, CacheAopProxyChain joinPoint,
162168
}
163169

164170
// 如果fixRateUpdateCache注解字段不为空,则走固定刷新逻辑
165-
if(StringUtils.isNotEmpty(cache.fixRateUpdateCache())) {
171+
if(cache.fixRateUpdateCache() > 0) {
166172
AutoLoadTO autoLoadTO = autoLoadMap.get(cacheKey);
167-
if (null != autoLoadTO) {
173+
if (autoLoadTO != null) {
168174
autoLoadMap.remove(cacheKey);
169175
}
170176

177+
// 如果已经存在 则忽略
178+
if(fixRateRefreshArrayList.contains(cacheKey)) {
179+
return null;
180+
}
181+
182+
// 否则执行固定频率刷新
171183
DeepClone deepClone = new DeepClone(joinPoint, cache).invoke();
172184
if (deepClone.is()) return null;
173185

@@ -224,7 +236,7 @@ public AutoLoadTO putIfAbsent(CacheKeyTO cacheKey, CacheAopProxyChain joinPoint,
224236

225237
private boolean fixRateUpdateCacheIfNeeded(String methodName, AutoLoadTO autoLoadTO) {
226238
if (null == autoLoadTO || autoLoadTO.getCache() == null ||
227-
StringUtils.isEmpty(autoLoadTO.getCache().fixRateUpdateCache())) {
239+
autoLoadTO.getCache().fixRateUpdateCache() <= 0) {
228240
return false;
229241
}
230242

@@ -234,25 +246,10 @@ private boolean fixRateUpdateCacheIfNeeded(String methodName, AutoLoadTO autoLoa
234246
}
235247

236248
private void doExecute(String methodName, AutoLoadTO autoLoadTO) {
237-
// 解析fixRateUpdateCache Timer表达式
238-
String updateCacheCronExpression = autoLoadTO.getCache().fixRateUpdateCache();
239-
if (!updateCacheCronExpression.contains(",")) {
240-
log.error("不符合规则的频率表达式{}", updateCacheCronExpression);
241-
return;
242-
}
243-
long delay;
244-
long period;
245-
try {
246-
String[] split = updateCacheCronExpression.split(",");
247-
delay = Long.parseLong(split[0]);
248-
period = Long.parseLong(split[1]);
249-
} catch (Exception e) {
250-
log.error("not matched cron expression-{}", updateCacheCronExpression);
251-
return;
252-
}
253-
scheduledThreadPoolExecutor.scheduleWithFixedDelay(new FixRateUpdateCacheTask(autoLoadTO), delay,
249+
int period = autoLoadTO.getCache().fixRateUpdateCache();
250+
scheduledThreadPoolExecutor.scheduleWithFixedDelay(new FixRateUpdateCacheTask(autoLoadTO), 0,
254251
period, TimeUnit.SECONDS);
255-
log.info("register fix rate refresh task——method-{}, rate-{}", methodName, updateCacheCronExpression);
252+
log.info("register fix rate refresh task——method-{}, period-{}", methodName, period);
256253
}
257254

258255
class FixRateUpdateCacheTask implements Runnable{

src/main/java/com/jarvis/cache/CacheHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ public void writeCache(CacheAopProxyChain pjp, Object[] arguments, Cache cache,
463463
public void destroy() {
464464
autoLoadHandler.shutdown();
465465
refreshHandler.shutdown();
466+
AutoLoadHandler.scheduledThreadPoolExecutor.shutdown();
466467
log.trace("cache destroy ... ... ...");
467468
}
468469

src/main/java/com/jarvis/cache/RefreshHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public void removeTask(CacheKeyTO cacheKey) {
6969
}
7070

7171
public void doRefresh(CacheAopProxyChain pjp, Cache cache, CacheKeyTO cacheKey, CacheWrapper<Object> cacheWrapper) {
72+
// 如果已经交由固定频率刷新,则不做过期判断
73+
if (AutoLoadHandler.fixRateRefreshArrayList.contains(cacheKey)) {
74+
return;
75+
}
7276
int expire = cacheWrapper.getExpire();
7377
if (expire < REFRESH_MIN_EXPIRE) {// 如果过期时间太小了,就不允许自动加载,避免加载过于频繁,影响系统稳定性
7478
return;

src/main/java/com/jarvis/cache/annotation/Cache.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@
7373
boolean autoload() default false;
7474

7575
/**
76-
* 以固定频率的方式刷新缓存 (补充expire无限大时无法依靠alarmTime频繁刷新缓存的不足)
77-
* 格式 “initDelay,period” 默认单位s 【初始延迟时长,执行周期】
78-
* 如【5,10】意为5s后开始以10s为周期执行刷新任务
79-
* 为了向后兼容, 当alarmTime存在时优先解析alarmTime
80-
* 暂不支持自定义+扩展
81-
* @return 固定表达式 “initDelay,period”
82-
*/
83-
String fixRateUpdateCache() default "";
76+
* 固定频率刷新 默认是不开启的
77+
* 如果开启了此配置,则优先以此配置进行缓存刷新
78+
* @return 刷新间隔
79+
*/
80+
int fixRateUpdateCache() default 0;
8481

8582
/**
8683
* 自动缓存的条件,可以为空,返回 true 或者 false,如果设置了此值,autoload() 就失效,例如:null !=

0 commit comments

Comments
 (0)