-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caching update information in reports to increase performance
- Loading branch information
1 parent
fb28efc
commit f0bfd96
Showing
5 changed files
with
104 additions
and
25 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/codehaus/mojo/versions/api/ArtifactVersionsCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.codehaus.mojo.versions.api; | ||
|
||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
/** | ||
* Utility providing a cached {@link ArtifactVersions#getOldestUpdate(Optional)} API | ||
*/ | ||
public class ArtifactVersionsCache | ||
{ | ||
private BiFunction<AbstractVersionDetails, Optional<Segment>, ?> cachedFunction; | ||
|
||
private Map<Pair<? extends AbstractVersionDetails, Optional<Segment>>, Object> updateCache = new HashMap<>(); | ||
|
||
/** | ||
* Constructs a new instance given the concrete function for obtaining the details | ||
* | ||
* @param cachedFunction reference to the function computing the required information | ||
*/ | ||
public ArtifactVersionsCache( BiFunction<AbstractVersionDetails, Optional<Segment>, ?> | ||
cachedFunction ) | ||
{ | ||
this.cachedFunction = cachedFunction; | ||
} | ||
|
||
/** | ||
* Returns the required information for the given {@link ArtifactVersions} object and the given update scope. | ||
* If the information is already present in cache, the cached version is returned. Otherwise, | ||
* the {@code artifactVersions} object is queried and the response is cached. | ||
* | ||
* @param artifactVersions {@linkplain ArtifactVersions} object referring to the given dependency | ||
* @param updateScope update scope | ||
* @return last retrieved update information | ||
*/ | ||
@SuppressWarnings( "unchecked" ) | ||
public <V extends AbstractVersionDetails, R> R get( V artifactVersions, | ||
Optional<Segment> updateScope ) | ||
{ | ||
return (R) updateCache.computeIfAbsent( Pair.of( artifactVersions, updateScope ), | ||
pair -> cachedFunction.apply( pair.getLeft(), pair.getRight() ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters