Skip to content

Commit 395f8f3

Browse files
committed
Enable caching to avoid hitting github's rate limit
1 parent 9b2164f commit 395f8f3

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ dependencies {
3737
implementation 'org.springframework.boot:spring-boot-starter-web'
3838
implementation 'org.springframework.boot:spring-boot-starter-webflux'
3939
implementation 'org.springframework.boot:spring-boot-starter-validation'
40+
implementation 'org.springframework.boot:spring-boot-starter-cache'
4041
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
4142
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
4243
implementation 'org.springframework.boot:spring-boot-starter-graphql'
4344
implementation 'com.azure.spring:spring-cloud-azure-starter-keyvault-secrets'
4445
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
46+
implementation 'com.github.ben-manes.caffeine:caffeine'
4547
implementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
4648
implementation 'org.apache.maven:maven-artifact:3.6.3'
4749
testImplementation 'com.squareup.okhttp3:mockwebserver'

src/main/java/io/spring/projectapi/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import org.springframework.boot.autoconfigure.SpringBootApplication;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2626
import org.springframework.boot.web.client.RestTemplateBuilder;
27+
import org.springframework.cache.annotation.EnableCaching;
2728
import org.springframework.context.annotation.Bean;
2829

2930
@SpringBootApplication
31+
@EnableCaching
3032
@EnableConfigurationProperties(ApplicationProperties.class)
3133
public class Application {
3234

src/main/java/io/spring/projectapi/github/GithubOperations.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jetbrains.annotations.NotNull;
3838

3939
import org.springframework.boot.web.client.RestTemplateBuilder;
40+
import org.springframework.cache.annotation.Cacheable;
4041
import org.springframework.core.ParameterizedTypeReference;
4142
import org.springframework.http.HttpStatusCode;
4243
import org.springframework.http.MediaType;
@@ -94,6 +95,7 @@ private static int compare(ProjectDocumentation o1, ProjectDocumentation o2) {
9495
return -version1.compareTo(version2);
9596
}
9697

98+
@Cacheable(value = "documentation", key = "#projectSlug")
9799
public List<ProjectDocumentation> getProjectDocumentations(String projectSlug) {
98100
ResponseEntity<Map<String, Object>> response = getFile(projectSlug, "documentation.json");
99101
String content = getFileContents(response);
@@ -250,6 +252,7 @@ private String getFileSha(ResponseEntity<Map<String, Object>> exchange) {
250252
return (String) exchange.getBody().get("sha");
251253
}
252254

255+
@Cacheable("projects")
253256
public List<Project> getProjects() {
254257
List<Project> projects = new ArrayList<>();
255258
try {
@@ -274,6 +277,7 @@ public List<Project> getProjects() {
274277
return projects;
275278
}
276279

280+
@Cacheable(value = "project", key = "#projectSlug")
277281
public Project getProject(String projectSlug) {
278282
ResponseEntity<Map<String, Object>> response = getFile(projectSlug, "index.md");
279283
String contents = getFileContents(response);
@@ -283,6 +287,7 @@ public Project getProject(String projectSlug) {
283287
return this.objectMapper.convertValue(frontMatter, Project.class);
284288
}
285289

290+
@Cacheable(value = "support", key = "#projectSlug")
286291
public List<ProjectSupport> getProjectSupports(String projectSlug) {
287292
ResponseEntity<Map<String, Object>> response = getFile(projectSlug, "support.json");
288293
if (response == null) {
@@ -299,6 +304,7 @@ public List<ProjectSupport> getProjectSupports(String projectSlug) {
299304
}
300305
}
301306

307+
@Cacheable(value = "support_policy", key = "#projectSlug")
302308
public String getProjectSupportPolicy(String projectSlug) {
303309
ResponseEntity<Map<String, Object>> indexResponse = getFile(projectSlug, "index.md");
304310
String indexContents = getFileContents(indexResponse);

src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ projects.github.org: spring-io
55
projects.github.team: spring-team
66
projects.github.accesstoken: ${projects-github-accessToken}
77

8+
spring.cache.cache-names=projects,project,support,support_policy,documentation
9+
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=1800s
10+

0 commit comments

Comments
 (0)