diff --git a/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/builders.kt b/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/builders.kt index b2e73a0..41a10d8 100644 --- a/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/builders.kt +++ b/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/builders.kt @@ -20,7 +20,7 @@ import java.util.concurrent.Executor * If the suspendable computation throws or computes a null value then the * entry will be automatically removed. */ -fun Caffeine.asCache(): Cache { +fun Caffeine.asCache(): Cache { val scope = CoroutineScope(Dispatchers.IO + CoroutineName("Aedile-AsyncLoadingCache-Scope") + SupervisorJob()) return asCache(scope) } @@ -34,7 +34,7 @@ fun Caffeine.asCache(): Cache { * If the suspendable computation throws or computes a null value then the * entry will be automatically removed. */ -fun Caffeine.asCache(scope: CoroutineScope): Cache { +fun Caffeine.asCache(scope: CoroutineScope): Cache { return Cache(scope, true, buildAsync()) } diff --git a/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/AsCacheTest.kt b/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/AsCacheTest.kt index 266350d..3df8b26 100644 --- a/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/AsCacheTest.kt +++ b/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/AsCacheTest.kt @@ -1,10 +1,12 @@ package com.sksamuel.aedile.core import com.github.benmanes.caffeine.cache.Caffeine +import com.github.benmanes.caffeine.cache.Expiry import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import kotlinx.coroutines.delay +import org.checkerframework.checker.index.qual.NonNegative class AsCacheTest : FunSpec() { init { @@ -110,5 +112,37 @@ class AsCacheTest : FunSpec() { cache.contains("wibble") shouldBe true cache.contains("bubble") shouldBe false } + + test("check invariants on expire after") { + val loggerExpiry = object : Expiry { + override fun expireAfterRead( + key: Int?, + value: String?, + currentTime: Long, + currentDuration: @NonNegative Long + ): Long { + return 0 + } + + override fun expireAfterCreate(key: Int?, value: String?, currentTime: Long): Long { + return 0 + } + + override fun expireAfterUpdate( + key: Int?, + value: String?, + currentTime: Long, + currentDuration: @NonNegative Long + ): Long { + return 0 + } + } + + Caffeine.newBuilder() + .maximumSize(2000) + .initialCapacity(500) + .expireAfter(loggerExpiry) + .asCache() + } } }