This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new f087e8e  [MENFORCER-420] Cache the results of getDependenciesToCheck 
across rules.
f087e8e is described below

commit f087e8e832a36917dedaf959bef5a34e0193bd4e
Author: Joseph Walton <jwal...@atlassian.com>
AuthorDate: Sun May 29 22:57:31 2022 +1000

    [MENFORCER-420] Cache the results of getDependenciesToCheck across rules.
    
    Use an execution-scoped cache to store the results of the expensive call
    to getDependenciesToCheck so that it's not recomputed for each rule.
    
    Signed-off-by: Joseph Walton <jwal...@atlassian.com>
---
 .../maven/enforcer/rule/api/EnforcerRuleHelper.java      | 10 ++++++++++
 .../maven/plugins/enforcer/AbstractBanDependencies.java  | 16 ++++++++++++++--
 .../plugins/enforcer/DefaultEnforcementRuleHelper.java   | 13 +++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git 
a/enforcer-api/src/main/java/org/apache/maven/enforcer/rule/api/EnforcerRuleHelper.java
 
b/enforcer-api/src/main/java/org/apache/maven/enforcer/rule/api/EnforcerRuleHelper.java
index 403be73..727faef 100644
--- 
a/enforcer-api/src/main/java/org/apache/maven/enforcer/rule/api/EnforcerRuleHelper.java
+++ 
b/enforcer-api/src/main/java/org/apache/maven/enforcer/rule/api/EnforcerRuleHelper.java
@@ -21,6 +21,7 @@ package org.apache.maven.enforcer.rule.api;
 
 import java.util.List;
 import java.util.Map;
+import java.util.function.Supplier;
 
 import javax.annotation.Nonnull;
 
@@ -131,4 +132,13 @@ public interface EnforcerRuleHelper
      * @return the container
      */
     PlexusContainer getContainer();
+
+    /**
+     * Gets a cached value, or uses the provided producer to compute it.
+     *
+     * @param key a key to identify the value stored
+     * @param producer a supplier for the value if it's not already present
+     * @return a previously-cached or freshly-computed value
+     */
+    Object getCache( String key, Supplier<?> producer );
 }
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java
 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java
index 8a9735d..4148c6f 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java
@@ -77,7 +77,7 @@ public abstract class AbstractBanDependencies
 
         try
         {
-            graphBuilder = (DependencyGraphBuilder) helper.getComponent( 
DependencyGraphBuilder.class );
+            graphBuilder = helper.getComponent( DependencyGraphBuilder.class );
         }
         catch ( ComponentLookupException e )
         {
@@ -89,7 +89,7 @@ public abstract class AbstractBanDependencies
         buildingRequest.setProject( project );
 
         // get the correct list of dependencies
-        Set<Artifact> dependencies = getDependenciesToCheck( buildingRequest );
+        Set<Artifact> dependencies = getDependenciesToCheck( helper, 
buildingRequest );
 
         // look for banned dependencies
         Set<Artifact> foundExcludes = checkDependencies( dependencies, 
helper.getLog() );
@@ -120,6 +120,18 @@ public abstract class AbstractBanDependencies
         return "Found Banned Dependency: " + artifact.getId() + 
System.lineSeparator();
     }
 
+    private Set<Artifact> getDependenciesToCheck( EnforcerRuleHelper helper,
+            ProjectBuildingRequest buildingRequest )
+    {
+        String cacheKey = buildingRequest.getProject().getId() + "_" + 
searchTransitive;
+
+        // check in the cache
+        Set<Artifact> dependencies =
+                (Set<Artifact>) helper.getCache( cacheKey, () -> 
getDependenciesToCheck( buildingRequest ) );
+
+        return dependencies;
+    }
+
     protected Set<Artifact> getDependenciesToCheck( ProjectBuildingRequest 
buildingRequest )
     {
         Set<Artifact> dependencies = null;
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DefaultEnforcementRuleHelper.java
 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DefaultEnforcementRuleHelper.java
index e1c8858..7f80748 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DefaultEnforcementRuleHelper.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DefaultEnforcementRuleHelper.java
@@ -20,8 +20,10 @@ package org.apache.maven.plugins.enforcer;
  */
 
 import java.io.File;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Supplier;
 
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
 import org.apache.maven.execution.MavenSession;
@@ -50,6 +52,9 @@ public class DefaultEnforcementRuleHelper
     /** The container. */
     private PlexusContainer container;
 
+    /** A cache. */
+    private Map<String, Object> cache;
+
     /**
      * Instantiates a new default enforcement rule helper.
      *
@@ -71,6 +76,8 @@ public class DefaultEnforcementRuleHelper
         {
             this.container = session.getContainer();
         }
+
+        this.cache = new HashMap<>();
     }
 
     @Override
@@ -139,4 +146,10 @@ public class DefaultEnforcementRuleHelper
     {
         return container;
     }
+
+    @Override
+    public Object getCache( String key, Supplier<?> producer )
+    {
+        return cache.computeIfAbsent( key, ( x ) -> producer.get() );
+    }
 }

Reply via email to