diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java index 9be98d7667c..61eaf54d4ec 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java @@ -115,19 +115,24 @@ private AbstractServiceDiscovery(ApplicationModel applicationModel, String servi private void removeExpiredMetadataInfo(int metadataInfoCacheSize, int metadataInfoCacheExpireTime) { Long nextTime = null; + // Cache cleanup is only required when the cache size exceeds the cache limit. if (metadataInfos.size() > metadataInfoCacheSize) { List values = new ArrayList<>(metadataInfos.values()); + // Place the earliest data at the front values.sort(Comparator.comparingLong(MetadataInfoStat::getUpdateTime)); for (MetadataInfoStat v : values) { long time = System.currentTimeMillis() - v.getUpdateTime(); if (time > metadataInfoCacheExpireTime) { metadataInfos.remove(v.metadataInfo.getRevision(), v); } else { + // Calculate how long it will take for the next task to start nextTime = metadataInfoCacheExpireTime - time; break; } } } + // If there is no metadata to clean up this time, the next task will start within half of the cache expiration + // time. startRefreshCache( nextTime == null ? metadataInfoCacheExpireTime / 2 : nextTime, metadataInfoCacheSize,