Skip to content

Commit

Permalink
feat(server): replace cache library with Aedile (#1835)
Browse files Browse the repository at this point in the history
In order to have a better monitoring of our cache, a library that easily
 exposes metrics is a requirement.

This commit replaces the current cache implementation with Aedile, a
wrapper to Caffeine.

Caffeine supports monitoring natively, while aedile allows us to access
it without losing the kotlin-like style.
  • Loading branch information
LeoColman authored Feb 24, 2025
1 parent 126a135 commit dd4182e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions jit-binding-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ dependencies {
implementation("io.ktor:ktor-server-call-id")
implementation("io.ktor:ktor-server-metrics-micrometer")
implementation("io.micrometer:micrometer-registry-prometheus:1.14.4")
implementation("io.github.reactivecircus.cache4k:cache4k:0.14.0")

implementation("com.sksamuel.aedile:aedile-core:2.0.3")
implementation("io.github.oshai:kotlin-logging:7.0.4")
implementation(platform("org.apache.logging.log4j:log4j-bom:2.24.3"))
implementation("org.apache.logging.log4j:log4j-jul")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.typesafegithub.workflows.jitbindingserver

import com.github.benmanes.caffeine.cache.Caffeine
import com.sksamuel.aedile.core.asCache
import com.sksamuel.aedile.core.expireAfterWrite
import io.github.oshai.kotlinlogging.KotlinLogging.logger
import io.github.reactivecircus.cache4k.Cache
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrint
import io.github.typesafegithub.workflows.mavenbinding.Artifact
Expand All @@ -20,16 +22,24 @@ import io.ktor.server.routing.get
import io.ktor.server.routing.head
import io.ktor.server.routing.route
import io.micrometer.core.instrument.Tag
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import kotlin.time.Duration.Companion.hours

private val logger = logger { }

typealias ArtifactResult = Result<Map<String, Artifact>>

private val bindingsCache = Cache.Builder<ActionCoords, ArtifactResult>().expireAfterWrite(1.hours).build()
private val bindingsCache =
Caffeine
.newBuilder()
.expireAfterWrite(1.hours)
.recordStats()
.asCache<ActionCoords, ArtifactResult>()

fun Routing.artifactRoutes(prometheusRegistry: PrometheusMeterRegistry) {
CaffeineCacheMetrics.monitor(prometheusRegistry, bindingsCache.underlying(), "bindings_cache")

route("{owner}/{name}/{version}/{file}") {
artifact(prometheusRegistry, refresh = false)
}
Expand Down

0 comments on commit dd4182e

Please sign in to comment.