diff --git a/lemminx-maven/src/main/java/org/eclipse/lemminx/extensions/maven/searcher/RemoteCentralRepositorySearcher.java b/lemminx-maven/src/main/java/org/eclipse/lemminx/extensions/maven/searcher/RemoteCentralRepositorySearcher.java index 632f9ff3..9457bed0 100644 --- a/lemminx-maven/src/main/java/org/eclipse/lemminx/extensions/maven/searcher/RemoteCentralRepositorySearcher.java +++ b/lemminx-maven/src/main/java/org/eclipse/lemminx/extensions/maven/searcher/RemoteCentralRepositorySearcher.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -56,8 +57,45 @@ public class RemoteCentralRepositorySearcher { public static boolean disableCentralSearch = Boolean .parseBoolean(System.getProperty(RemoteCentralRepositorySearcher.class.getName() + ".disableCentralSearch")); + public static long cacheDeprecationTimeoutMs = Long + .parseLong(System.getProperty(RemoteCentralRepositorySearcher.class.getName() + ".cacheDeprecationTimeoutMs", "-1"), 10); + private OkHttpClient client = new OkHttpClient(); + private static class Cache { + private static long DEFAULT_DEPRECATION_TIMEOUT = 15*60*1000; + private HashMap > cache = new HashMap<>(); + private long cacheDeprecationTimeout = DEFAULT_DEPRECATION_TIMEOUT; + + public Cache(long deprecationTimeoutMs) { + cacheDeprecationTimeout = deprecationTimeoutMs != -1 ? + deprecationTimeoutMs : DEFAULT_DEPRECATION_TIMEOUT; + } + + class ValueDescriptor { + T data; + long timestamp; + } + + void put (K key, V value) { + ValueDescriptor descriptor = new ValueDescriptor<>(); + descriptor.data = value; + descriptor.timestamp = System.currentTimeMillis(); + cache.put(key.toString(), descriptor); + } + + V get(K key) { + ValueDescriptor descriptor = cache.get(key.toString()); + if (descriptor != null + && (System.currentTimeMillis() - descriptor.timestamp + < cacheDeprecationTimeout)) { + return descriptor.data; + } + return null; + } + } + + public RemoteCentralRepositorySearcher(MavenLemminxExtension lemminxMavenPlugin) { } @@ -88,8 +126,14 @@ public Set getPluginGroupIds(Dependency artifactToSearch) { return disableCentralSearch ? Collections.emptySet() : internalGetGroupIds(artifactToSearch, PACKAGING_TYPE_MAVEN_PLUGIN); } + private static Cache> artifactsCache = new Cache<>(cacheDeprecationTimeoutMs); private Collection internalGetArtifacts(Dependency artifactToSearch, String packaging) { Request request = createArtifactIdsRequesty(artifactToSearch, packaging); + Collection cachedValue = artifactsCache.get(request); + if (cachedValue != null) { + return cachedValue; + } + JsonObject responseBody = getResponseBody(request, artifactToSearch); if (responseBody == null || responseBody.get("numFound").getAsInt() <= 0 ) { return Collections.emptyList(); @@ -99,15 +143,23 @@ private Collection internalGetArtifacts(Dependency artifactToSearch, S responseBody.get("docs").getAsJsonArray().forEach(d -> { artifactInfos.add(toArtifactInfo(d.getAsJsonObject())); }); + + artifactsCache.put(request, artifactInfos); return artifactInfos; } - + + private static Cache> artifactVersionsCache = new Cache<>(cacheDeprecationTimeoutMs); private Set internalGetArtifactVersions(Dependency artifactToSearch, String packaging) { if (isEmpty(artifactToSearch.getArtifactId())) { return Collections.emptySet(); } Request request = createArtifactVersionsRequest(artifactToSearch, packaging); + Set cachedValue = artifactVersionsCache.get(request); + if (cachedValue != null) { + return cachedValue; + } + JsonObject responseBody = getResponseBody(request, artifactToSearch); if (responseBody == null || responseBody.get("numFound").getAsInt() <= 0 ) { return Collections.emptySet(); @@ -117,15 +169,23 @@ private Set internalGetArtifactVersions(Dependency artifactToSe responseBody.get("docs").getAsJsonArray().forEach(d -> { artifactVersions.add(new DefaultArtifactVersion(d.getAsJsonObject().get(VERSION).getAsString())); }); + + artifactVersionsCache.put(request, artifactVersions); return artifactVersions; } + private static Cache> groupIdsCache = new Cache<>(cacheDeprecationTimeoutMs); private Set internalGetGroupIds(Dependency artifactToSearch, String packaging) { if (isEmpty(artifactToSearch.getGroupId())) { return Collections.emptySet(); } Request request = createGroupIdsRequest(artifactToSearch, packaging); + Set cachedValue = groupIdsCache.get(request); + if (cachedValue != null) { + return cachedValue; + } + JsonObject responseBody = getResponseBody(request, artifactToSearch); if (responseBody == null || responseBody.get("numFound").getAsInt() <= 0 ) { return Collections.emptySet(); @@ -135,6 +195,8 @@ private Set internalGetGroupIds(Dependency artifactToSearch, String pack responseBody.get("docs").getAsJsonArray().forEach(d -> { artifactGroupIds.add(d.getAsJsonObject().get(GROUP_ID).getAsString()); }); + + groupIdsCache.put(request, artifactGroupIds); return artifactGroupIds; }