Skip to content

Commit

Permalink
CID-2732: use spring provided cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedlajmileanix committed Jul 5, 2024
1 parent 53a164a commit f0f6d64
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
53 changes: 44 additions & 9 deletions src/main/kotlin/net/leanix/githubagent/services/CachingService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,19 +11,51 @@ import org.springframework.stereotype.Service
class CachingService(
private val githubEnterpriseProperties: GitHubEnterpriseProperties
) {
private val cache = HashMap<String, String?>()

@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<String, CacheValue> = Caffeine.newBuilder()
.maximumSize(100)
.expireAfter(object : Expiry<String, CacheValue> {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0f6d64

Please sign in to comment.