Skip to content

Commit

Permalink
Enable caching to avoid hitting github's rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhave committed Nov 5, 2024
1 parent 9b2164f commit 395f8f3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-graphql'
implementation 'com.azure.spring:spring-cloud-azure-starter-keyvault-secrets'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation 'com.github.ben-manes.caffeine:caffeine'
implementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
implementation 'org.apache.maven:maven-artifact:3.6.3'
testImplementation 'com.squareup.okhttp3:mockwebserver'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/spring/projectapi/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableCaching
@EnableConfigurationProperties(ApplicationProperties.class)
public class Application {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jetbrains.annotations.NotNull;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -94,6 +95,7 @@ private static int compare(ProjectDocumentation o1, ProjectDocumentation o2) {
return -version1.compareTo(version2);
}

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

@Cacheable("projects")
public List<Project> getProjects() {
List<Project> projects = new ArrayList<>();
try {
Expand All @@ -274,6 +277,7 @@ public List<Project> getProjects() {
return projects;
}

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

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

@Cacheable(value = "support_policy", key = "#projectSlug")
public String getProjectSupportPolicy(String projectSlug) {
ResponseEntity<Map<String, Object>> indexResponse = getFile(projectSlug, "index.md");
String indexContents = getFileContents(indexResponse);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ projects.github.org: spring-io
projects.github.team: spring-team
projects.github.accesstoken: ${projects-github-accessToken}

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

0 comments on commit 395f8f3

Please sign in to comment.