Skip to content

Commit

Permalink
Add cache for commit responses based on hashes, as they shouldn't cha…
Browse files Browse the repository at this point in the history
…nge over time (#839)
  • Loading branch information
thomas-boehm-tractive committed Mar 28, 2024
1 parent 4733e8c commit 041fa69
Showing 1 changed file with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public class BitbucketCloudApiClient implements BitbucketApi {
private static final Cache<String, BitbucketTeam> cachedTeam = new Cache<>(6, HOURS);
private static final Cache<String, AvatarImage> cachedAvatar = new Cache<>(6, HOURS);
private static final Cache<String, List<BitbucketCloudRepository>> cachedRepositories = new Cache<>(3, HOURS);
private static final Cache<String, BitbucketCloudCommit> cachedCommits = new Cache<>(24, HOURS);
private transient BitbucketRepository cachedRepository;
private transient String cachedDefaultBranch;

Expand All @@ -156,12 +157,14 @@ public static List<String> stats() {
List<String> stats = new ArrayList<>();
stats.add("Team: " + cachedTeam.stats().toString());
stats.add("Repositories : " + cachedRepositories.stats().toString());
stats.add("Commits: " + cachedCommits.stats().toString());
return stats;
}

public static void clearCaches() {
cachedTeam.evictAll();
cachedRepositories.evictAll();
cachedCommits.evictAll();

Check warning on line 167 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 160-167 are not covered by tests
}

@Deprecated
Expand Down Expand Up @@ -519,22 +522,35 @@ public List<BitbucketCloudBranch> getBranchesByRef(String nodePath) throws IOExc
@Override
@CheckForNull
public BitbucketCommit resolveCommit(@NonNull String hash) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit/{hash}")
.set("owner", owner)
.set("repo", repositoryName)
.set("hash", hash)
.expand();
String response;
final String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit/{hash}")
.set("owner", owner)
.set("repo", repositoryName)
.set("hash", hash)
.expand();

Callable<BitbucketCloudCommit> request = () -> {
String response;
try {
response = getRequest(url);
} catch (FileNotFoundException e) {
return null;
}
try {
return JsonParser.toJava(response, BitbucketCloudCommit.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
};

try {
response = getRequest(url);
} catch (FileNotFoundException e) {
if (enableCache) {

Check warning on line 546 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 546 is only partially covered, one branch is missing
return cachedCommits.get(hash, request);

Check warning on line 547 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 547 is not covered by tests
} else {
return request.call();
}
} catch (Exception ex) {
return null;

Check warning on line 552 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 551-552 are not covered by tests
}
try {
return JsonParser.toJava(response, BitbucketCloudCommit.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}

/**
Expand Down

0 comments on commit 041fa69

Please sign in to comment.