From 6ca29c0d1760a85b29fbf2c81022b73f6bebcf61 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Mon, 22 Jan 2018 22:30:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E7=BC=93=E5=AD=98Key?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E6=94=AF=E6=8C=81=E4=BB=A5=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=B8=BA=E7=B2=92=E5=BA=A6=E7=BC=93=E5=AD=98=EF=BC=9B?= =?UTF-8?q?=20LocalCache=E4=BC=98=E5=8C=96=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=A0=86=E7=A7=AF=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/admin/core/util/LocalCacheUtil.java | 22 +++++++++++++++++++ .../admin/service/impl/XxlJobServiceImpl.java | 5 +++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/LocalCacheUtil.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/LocalCacheUtil.java index 76bd23e4..3a0ec942 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/LocalCacheUtil.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/LocalCacheUtil.java @@ -61,6 +61,11 @@ public class LocalCacheUtil { * @return */ public static boolean set(String key, Object val, long cacheTime){ + + // clean timeout cache, before set new cache (avoid cache too much) + cleanTimeutCache(); + + // set new cache if (StringUtils.isBlank(key)) { return false; } @@ -109,4 +114,21 @@ public class LocalCacheUtil { } } + /** + * clean timeout cache + * + * @return + */ + public static boolean cleanTimeutCache(){ + if (!cacheRepository.keySet().isEmpty()) { + for (String key: cacheRepository.keySet()) { + LocalCacheData localCacheData = cacheRepository.get(key); + if (localCacheData!=null && System.currentTimeMillis()>=localCacheData.getTimeoutTime()) { + cacheRepository.remove(key); + } + } + } + return true; + } + } diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java index b31a42c0..0ad2e2df 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java @@ -325,7 +325,8 @@ public class XxlJobServiceImpl implements XxlJobService { @Override public ReturnT> triggerChartDate(Date startDate, Date endDate) { // get cache - Map triggerChartDateCache = (Map) LocalCacheUtil.get(TRIGGER_CHART_DATA_CACHE); + String cacheKey = TRIGGER_CHART_DATA_CACHE + "_" + startDate.getTime() + "_" + endDate.getTime(); + Map triggerChartDateCache = (Map) LocalCacheUtil.get(cacheKey); if (triggerChartDateCache != null) { return new ReturnT>(triggerChartDateCache); } @@ -376,7 +377,7 @@ public class XxlJobServiceImpl implements XxlJobService { result.put("triggerCountFailTotal", triggerCountFailTotal); // set cache - LocalCacheUtil.set(TRIGGER_CHART_DATA_CACHE, result, 60*1000); // cache 60s + LocalCacheUtil.set(cacheKey, result, 60*1000); // cache 60s return new ReturnT>(result); }