Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ class LoadingCache<K, V>(
cache.synchronous().invalidateAll()
}

/**
* Loads a new value for the key, asynchronously. While the new value is loading the
* previous value (if any) will continue to be returned by get(key) unless it is evicted.
*
* See full docs at [com.github.benmanes.caffeine.cache.LoadingCache.refresh].
*/
fun refresh(key: K) {
cache.synchronous().refresh(key)
}

/**
* Loads a new value for each key, asynchronously. While the new value is loading the
* previous value (if any) will continue to be returned by get(key) unless it is evicted.
*
* See full docs at [com.github.benmanes.caffeine.cache.LoadingCache.refreshAll].
*/
fun refreshAll(keys: Collection<K>) {
Comment on lines +184 to +194
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the new methods be suspending, calling await and returning the value?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I don't know what I was thinking?!
2.1.1 coming out

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... sorry to bug again, but what about the return value? :-)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what happens when I do things after drinking :)
Try 2.1.2

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :-)

cache.synchronous().refreshAll(keys)
}

private suspend fun scope(): CoroutineScope {
return if (useCallingContext) CoroutineScope(coroutineContext) else defaultScope
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sksamuel.aedile.core

import com.github.benmanes.caffeine.cache.Caffeine
import com.github.benmanes.caffeine.cache.Expiry
import io.kotest.assertions.nondeterministic.eventually
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
Expand All @@ -13,6 +14,7 @@ import kotlinx.coroutines.yield
import org.checkerframework.checker.index.qual.NonNegative
import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

class AsLoadingCacheTest : FunSpec() {
init {
Expand Down Expand Up @@ -319,6 +321,34 @@ class AsLoadingCacheTest : FunSpec() {
cache.contains("bubble") shouldBe false
}

test("support refresh") {
var counter = 0
val cache = Caffeine.newBuilder().asLoadingCache<String, Int> {
counter++
counter
}
cache.get("foo") shouldBe 1
cache.refresh("foo")
eventually(5.seconds) {
cache.get("foo") shouldBe 2
}
}

test("support refresh all") {
var counter = 0
val cache = Caffeine.newBuilder().asLoadingCache<String, Int> {
counter++
counter
}
cache.get("foo") shouldBe 1
cache.get("bar") shouldBe 2
cache.refreshAll(setOf("foo", "bar"))
eventually(5.seconds) {
cache.get("foo") shouldBe 3
cache.get("bar") shouldBe 4
}
}

test("check invariants on expire after") {
val loggerExpiry = object : Expiry<Int, String> {
override fun expireAfterRead(
Expand Down