@@ -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 {
0 commit comments