Skip to content

Commit

Permalink
Maven Central search results should be cached #263
Browse files Browse the repository at this point in the history
Fixes: #263
  • Loading branch information
vrubezhny committed Jul 25, 2023
1 parent d20ffcf commit d8ac4c5
Showing 1 changed file with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<K,V> {
private static long DEFAULT_DEPRECATION_TIMEOUT = 15*60*1000;
private HashMap <Object,ValueDescriptor< V>> cache = new HashMap<>();
private long cacheDeprecationTimeout = DEFAULT_DEPRECATION_TIMEOUT;

public Cache(long deprecationTimeoutMs) {
cacheDeprecationTimeout = deprecationTimeoutMs != -1 ?
deprecationTimeoutMs : DEFAULT_DEPRECATION_TIMEOUT;
}

class ValueDescriptor<T> {
T data;
long timestamp;
}

void put (K key, V value) {
ValueDescriptor<V> descriptor = new ValueDescriptor<>();
descriptor.data = value;
descriptor.timestamp = System.currentTimeMillis();
cache.put(key.toString(), descriptor);
}

V get(K key) {
ValueDescriptor<V> descriptor = cache.get(key.toString());
if (descriptor != null
&& (System.currentTimeMillis() - descriptor.timestamp
< cacheDeprecationTimeout)) {
return descriptor.data;
}
return null;
}
}


public RemoteCentralRepositorySearcher(MavenLemminxExtension lemminxMavenPlugin) {
}

Expand Down Expand Up @@ -88,8 +126,14 @@ public Set<String> getPluginGroupIds(Dependency artifactToSearch) {
return disableCentralSearch ? Collections.emptySet() : internalGetGroupIds(artifactToSearch, PACKAGING_TYPE_MAVEN_PLUGIN);
}

private static Cache<Request, Collection<Artifact>> artifactsCache = new Cache<>(cacheDeprecationTimeoutMs);
private Collection<Artifact> internalGetArtifacts(Dependency artifactToSearch, String packaging) {
Request request = createArtifactIdsRequesty(artifactToSearch, packaging);
Collection<Artifact> 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();
Expand All @@ -99,15 +143,23 @@ private Collection<Artifact> internalGetArtifacts(Dependency artifactToSearch, S
responseBody.get("docs").getAsJsonArray().forEach(d -> {
artifactInfos.add(toArtifactInfo(d.getAsJsonObject()));
});

artifactsCache.put(request, artifactInfos);
return artifactInfos;
}


private static Cache<Request, Set<ArtifactVersion>> artifactVersionsCache = new Cache<>(cacheDeprecationTimeoutMs);
private Set<ArtifactVersion> internalGetArtifactVersions(Dependency artifactToSearch, String packaging) {
if (isEmpty(artifactToSearch.getArtifactId())) {
return Collections.emptySet();
}

Request request = createArtifactVersionsRequest(artifactToSearch, packaging);
Set<ArtifactVersion> 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();
Expand All @@ -117,15 +169,23 @@ private Set<ArtifactVersion> 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<Request, Set<String>> groupIdsCache = new Cache<>(cacheDeprecationTimeoutMs);
private Set<String> internalGetGroupIds(Dependency artifactToSearch, String packaging) {
if (isEmpty(artifactToSearch.getGroupId())) {
return Collections.emptySet();
}

Request request = createGroupIdsRequest(artifactToSearch, packaging);
Set<String> 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();
Expand All @@ -135,6 +195,8 @@ private Set<String> 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;
}

Expand Down

0 comments on commit d8ac4c5

Please sign in to comment.