Skip to content

Commit

Permalink
Make artifact cache threadsafe.
Browse files Browse the repository at this point in the history
Use singleton holder pattern for lazy initialisation of static cache, and make the cache concurrency safe as it can be read and updated simultaneously in multi module builds.
  • Loading branch information
timw committed May 12, 2023
1 parent f7036ba commit 589254a
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
import org.eclipse.aether.repository.RemoteRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -101,7 +102,12 @@ public class DefaultThirdPartyHelper
/**
* Cache of dependencies (as maven project) loaded.
*/
private static SortedMap<String, MavenProject> artifactCache;
private static final class ArtifactCacheHolder
{
private ArtifactCacheHolder() {}

private static final SortedMap<String, MavenProject> artifactCache = new ConcurrentSkipListMap<>();
}

/**
* Constructor of the helper.
Expand Down Expand Up @@ -136,11 +142,7 @@ public DefaultThirdPartyHelper( MavenProject project, String encoding, boolean v
*/
public SortedMap<String, MavenProject> getArtifactCache()
{
if ( artifactCache == null )
{
artifactCache = new TreeMap<>();
}
return artifactCache;
return ArtifactCacheHolder.artifactCache;
}

/**
Expand Down

0 comments on commit 589254a

Please sign in to comment.