Skip to content

Commit 041fa69

Browse files
Add cache for commit responses based on hashes, as they shouldn't change over time (#839)
1 parent 4733e8c commit 041fa69

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public class BitbucketCloudApiClient implements BitbucketApi {
143143
private static final Cache<String, BitbucketTeam> cachedTeam = new Cache<>(6, HOURS);
144144
private static final Cache<String, AvatarImage> cachedAvatar = new Cache<>(6, HOURS);
145145
private static final Cache<String, List<BitbucketCloudRepository>> cachedRepositories = new Cache<>(3, HOURS);
146+
private static final Cache<String, BitbucketCloudCommit> cachedCommits = new Cache<>(24, HOURS);
146147
private transient BitbucketRepository cachedRepository;
147148
private transient String cachedDefaultBranch;
148149

@@ -156,12 +157,14 @@ public static List<String> stats() {
156157
List<String> stats = new ArrayList<>();
157158
stats.add("Team: " + cachedTeam.stats().toString());
158159
stats.add("Repositories : " + cachedRepositories.stats().toString());
160+
stats.add("Commits: " + cachedCommits.stats().toString());
159161
return stats;
160162
}
161163

162164
public static void clearCaches() {
163165
cachedTeam.evictAll();
164166
cachedRepositories.evictAll();
167+
cachedCommits.evictAll();
165168
}
166169

167170
@Deprecated
@@ -519,22 +522,35 @@ public List<BitbucketCloudBranch> getBranchesByRef(String nodePath) throws IOExc
519522
@Override
520523
@CheckForNull
521524
public BitbucketCommit resolveCommit(@NonNull String hash) throws IOException, InterruptedException {
522-
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit/{hash}")
523-
.set("owner", owner)
524-
.set("repo", repositoryName)
525-
.set("hash", hash)
526-
.expand();
527-
String response;
525+
final String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit/{hash}")
526+
.set("owner", owner)
527+
.set("repo", repositoryName)
528+
.set("hash", hash)
529+
.expand();
530+
531+
Callable<BitbucketCloudCommit> request = () -> {
532+
String response;
533+
try {
534+
response = getRequest(url);
535+
} catch (FileNotFoundException e) {
536+
return null;
537+
}
538+
try {
539+
return JsonParser.toJava(response, BitbucketCloudCommit.class);
540+
} catch (IOException e) {
541+
throw new IOException("I/O error when parsing response from URL: " + url, e);
542+
}
543+
};
544+
528545
try {
529-
response = getRequest(url);
530-
} catch (FileNotFoundException e) {
546+
if (enableCache) {
547+
return cachedCommits.get(hash, request);
548+
} else {
549+
return request.call();
550+
}
551+
} catch (Exception ex) {
531552
return null;
532553
}
533-
try {
534-
return JsonParser.toJava(response, BitbucketCloudCommit.class);
535-
} catch (IOException e) {
536-
throw new IOException("I/O error when parsing response from URL: " + url, e);
537-
}
538554
}
539555

540556
/**

0 commit comments

Comments
 (0)