diff --git a/build.gradle.kts b/build.gradle.kts index 7cc88ac..d71accb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,6 +30,8 @@ dependencyManagement { dependencies { implementation("org.springframework.boot:spring-boot-starter") + implementation("org.springframework.boot:spring-boot-starter-cache") + implementation("com.github.ben-manes.caffeine:caffeine:2.8.8") implementation("org.springframework.cloud:spring-cloud-starter-openfeign") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("com.fasterxml.jackson.core:jackson-annotations:2.17.1") diff --git a/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt b/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt index 26566e6..900934b 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt @@ -1,5 +1,8 @@ package net.leanix.githubagent.services +import com.github.benmanes.caffeine.cache.Cache +import com.github.benmanes.caffeine.cache.Caffeine +import com.github.benmanes.caffeine.cache.Expiry import jakarta.annotation.PostConstruct import net.leanix.githubagent.config.GitHubEnterpriseProperties import org.springframework.stereotype.Service @@ -8,19 +11,51 @@ import org.springframework.stereotype.Service class CachingService( private val githubEnterpriseProperties: GitHubEnterpriseProperties ) { - private val cache = HashMap() - @PostConstruct - private fun init() { - cache["baseUrl"] = githubEnterpriseProperties.baseUrl - cache["githubAppId"] = githubEnterpriseProperties.githubAppId + data class CacheValue(val value: Any, val expiry: Long?) + + private val cache: Cache = Caffeine.newBuilder() + .maximumSize(100) + .expireAfter(object : Expiry { + override fun expireAfterCreate( + key: String, + value: CacheValue, + currentTime: Long + ): Long { + return value.expiry ?: Long.MAX_VALUE + } + + override fun expireAfterUpdate( + key: String, + value: CacheValue, + currentTime: Long, + currentDuration: Long + ): Long { + return value.expiry ?: Long.MAX_VALUE + } + + override fun expireAfterRead( + key: String, + value: CacheValue, + currentTime: Long, + currentDuration: Long + ): Long { + return currentDuration + } + }) + .build() + + fun set(key: String, value: Any, expiry: Long?) { + cache.put(key, CacheValue(value, expiry)) } - fun set(key: String, value: String) { - cache[key] = value + fun get(key: String): Any? { + return cache.getIfPresent(key)?.value } - fun get(key: String): String? { - return cache[key] + @PostConstruct + private fun init() { + set("baseUrl", githubEnterpriseProperties.baseUrl, null) + set("githubAppId", githubEnterpriseProperties.githubAppId, null) } } diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt index c621bd8..c76c573 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt @@ -42,7 +42,7 @@ class GitHubAuthenticationService( val keySpec = PKCS8EncodedKeySpec(Base64.getDecoder().decode(rsaPrivateKey)) val privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec) val jwt = createJwtToken(privateKey) - cachingService.set("jwtToken", jwt.getOrThrow()) + cachingService.set("jwtToken", jwt.getOrThrow(), JWT_EXPIRATION_DURATION) gitHubEnterpriseService.verifyJwt(jwt.getOrThrow()) }.onFailure { logger.error("Failed to generate/validate JWT token", it) @@ -59,7 +59,7 @@ class GitHubAuthenticationService( Jwts.builder() .setIssuedAt(Date()) .setExpiration(Date(System.currentTimeMillis() + JWT_EXPIRATION_DURATION)) - .setIssuer(cachingService.get("githubAppId")) + .setIssuer(cachingService.get("githubAppId").toString()) .signWith(privateKey, SignatureAlgorithm.RS256) .compact() }.onFailure { diff --git a/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt index be6fbde..23b6882 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt @@ -27,7 +27,7 @@ class GitHubAuthenticationServiceTest { @Test fun `generateJwtToken with valid data should not throw exception`() { every { cachingService.get(any()) } returns "dummy-value" - every { cachingService.set(any(), any()) } returns Unit + every { cachingService.set(any(), any(), any()) } returns Unit every { githubEnterpriseProperties.pemFile } returns "valid-private-key.pem" every { resourceLoader.getResource(any()) } returns ClassPathResource("valid-private-key.pem") every { gitHubEnterpriseService.verifyJwt(any()) } returns Unit