Skip to content

Commit 0b049c0

Browse files
authored
Add support for refresh/refresh all (#39)
* Add support for refresh/refresh all * docs * tests
1 parent 9657813 commit 0b049c0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

aedile-core/src/main/kotlin/com/sksamuel/aedile/core/LoadingCache.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ class LoadingCache<K, V>(
175175
cache.synchronous().invalidateAll()
176176
}
177177

178+
/**
179+
* Loads a new value for the key, asynchronously. While the new value is loading the
180+
* previous value (if any) will continue to be returned by get(key) unless it is evicted.
181+
*
182+
* See full docs at [com.github.benmanes.caffeine.cache.LoadingCache.refresh].
183+
*/
184+
fun refresh(key: K) {
185+
cache.synchronous().refresh(key)
186+
}
187+
188+
/**
189+
* Loads a new value for each key, asynchronously. While the new value is loading the
190+
* previous value (if any) will continue to be returned by get(key) unless it is evicted.
191+
*
192+
* See full docs at [com.github.benmanes.caffeine.cache.LoadingCache.refreshAll].
193+
*/
194+
fun refreshAll(keys: Collection<K>) {
195+
cache.synchronous().refreshAll(keys)
196+
}
197+
178198
private suspend fun scope(): CoroutineScope {
179199
return if (useCallingContext) CoroutineScope(coroutineContext) else defaultScope
180200
}

aedile-core/src/test/kotlin/com/sksamuel/aedile/core/AsLoadingCacheTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.sksamuel.aedile.core
22

33
import com.github.benmanes.caffeine.cache.Caffeine
44
import com.github.benmanes.caffeine.cache.Expiry
5+
import io.kotest.assertions.nondeterministic.eventually
56
import io.kotest.assertions.throwables.shouldNotThrowAny
67
import io.kotest.assertions.throwables.shouldThrow
78
import io.kotest.core.spec.style.FunSpec
@@ -13,6 +14,7 @@ import kotlinx.coroutines.yield
1314
import org.checkerframework.checker.index.qual.NonNegative
1415
import java.util.concurrent.atomic.AtomicInteger
1516
import kotlin.time.Duration.Companion.milliseconds
17+
import kotlin.time.Duration.Companion.seconds
1618

1719
class AsLoadingCacheTest : FunSpec() {
1820
init {
@@ -319,6 +321,34 @@ class AsLoadingCacheTest : FunSpec() {
319321
cache.contains("bubble") shouldBe false
320322
}
321323

324+
test("support refresh") {
325+
var counter = 0
326+
val cache = Caffeine.newBuilder().asLoadingCache<String, Int> {
327+
counter++
328+
counter
329+
}
330+
cache.get("foo") shouldBe 1
331+
cache.refresh("foo")
332+
eventually(5.seconds) {
333+
cache.get("foo") shouldBe 2
334+
}
335+
}
336+
337+
test("support refresh all") {
338+
var counter = 0
339+
val cache = Caffeine.newBuilder().asLoadingCache<String, Int> {
340+
counter++
341+
counter
342+
}
343+
cache.get("foo") shouldBe 1
344+
cache.get("bar") shouldBe 2
345+
cache.refreshAll(setOf("foo", "bar"))
346+
eventually(5.seconds) {
347+
cache.get("foo") shouldBe 3
348+
cache.get("bar") shouldBe 4
349+
}
350+
}
351+
322352
test("check invariants on expire after") {
323353
val loggerExpiry = object : Expiry<Int, String> {
324354
override fun expireAfterRead(

0 commit comments

Comments
 (0)