[MNG-7236] The DefaultPluginVersionResolver should cache results for the session

This commit is contained in:
Guillaume Nodet 2021-09-09 16:25:21 +02:00
parent 6b6589bf1c
commit 23ffe3349f
1 changed files with 85 additions and 4 deletions

View File

@ -25,7 +25,10 @@ import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -51,6 +54,7 @@ import org.eclipse.aether.RepositoryListener;
import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.RequestTrace; import org.eclipse.aether.RequestTrace;
import org.eclipse.aether.SessionData;
import org.eclipse.aether.metadata.DefaultMetadata; import org.eclipse.aether.metadata.DefaultMetadata;
import org.eclipse.aether.repository.ArtifactRepository; import org.eclipse.aether.repository.ArtifactRepository;
import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.repository.RemoteRepository;
@ -75,6 +79,8 @@ public class DefaultPluginVersionResolver
private static final String REPOSITORY_CONTEXT = "plugin"; private static final String REPOSITORY_CONTEXT = "plugin";
private static final Object CACHE_KEY = new Object();
@Inject @Inject
private Logger logger; private Logger logger;
@ -92,6 +98,12 @@ public class DefaultPluginVersionResolver
{ {
PluginVersionResult result = resolveFromProject( request ); PluginVersionResult result = resolveFromProject( request );
if ( result == null )
{
ConcurrentMap<Key, PluginVersionResult> cache = getCache( request.getRepositorySession().getData() );
Key key = getKey( request );
result = cache.get( key );
if ( result == null ) if ( result == null )
{ {
result = resolveFromRepository( request ); result = resolveFromRepository( request );
@ -101,6 +113,14 @@ public class DefaultPluginVersionResolver
logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId() logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId()
+ " to " + result.getVersion() + " from repository " + result.getRepository() ); + " to " + result.getVersion() + " from repository " + result.getRepository() );
} }
cache.putIfAbsent( key, result );
}
else if ( logger.isDebugEnabled() )
{
logger.debug( "Reusing cached resolved plugin version for " + request.getGroupId() + ":"
+ request.getArtifactId() + " to " + result.getVersion() + " from POM " + request.getPom() );
}
} }
else if ( logger.isDebugEnabled() ) else if ( logger.isDebugEnabled() )
{ {
@ -387,6 +407,67 @@ public class DefaultPluginVersionResolver
return null; return null;
} }
@SuppressWarnings( "unchecked" )
private ConcurrentMap<Key, PluginVersionResult> getCache( SessionData data )
{
ConcurrentMap<Key, PluginVersionResult> cache =
( ConcurrentMap<Key, PluginVersionResult> ) data.get( CACHE_KEY );
while ( cache == null )
{
cache = new ConcurrentHashMap<>( 256 );
if ( data.set( CACHE_KEY, null, cache ) )
{
break;
}
cache = ( ConcurrentMap<Key, PluginVersionResult> ) data.get( CACHE_KEY );
}
return cache;
}
private static Key getKey( PluginVersionRequest request )
{
return new Key( request.getGroupId(), request.getArtifactId(), request.getRepositories() );
}
static class Key
{
final String groupId;
final String artifactId;
final List<RemoteRepository> repositories;
final int hash;
Key( String groupId, String artifactId, List<RemoteRepository> repositories )
{
this.groupId = groupId;
this.artifactId = artifactId;
this.repositories = repositories;
this.hash = Objects.hash( groupId, artifactId, repositories );
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
Key key = ( Key ) o;
return groupId.equals( key.groupId )
&& artifactId.equals( key.artifactId )
&& repositories.equals( key.repositories );
}
@Override
public int hashCode()
{
return hash;
}
}
static class Versions static class Versions
{ {