-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add coroutine-based LoadingCache. (#232)
- Loading branch information
Showing
7 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
alias( | ||
name = "caffeine", | ||
actual = "@maven//:com_github_ben_manes_caffeine_caffeine", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/org/wfanet/measurement/common/LoadingCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2024 The Cross-Media Measurement Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.wfanet.measurement.common | ||
|
||
import com.github.benmanes.caffeine.cache.AsyncCache | ||
import com.github.benmanes.caffeine.cache.AsyncLoadingCache | ||
import java.util.concurrent.CompletableFuture | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.future.await | ||
import kotlinx.coroutines.future.future | ||
|
||
/** Coroutine wrapper around [asyncCache] which loads values using [load]. */ | ||
class LoadingCache<K, V>( | ||
private val asyncCache: AsyncCache<K, V>, | ||
private val load: suspend (K) -> V?, | ||
) { | ||
init { | ||
require(asyncCache !is AsyncLoadingCache) { "asyncCache already has an associated loader" } | ||
} | ||
|
||
/** Returns the value for [key], or `null` if no value could be loaded. */ | ||
suspend fun get(key: K): V? = coroutineScope { getAsync(key).await() } | ||
|
||
/** | ||
* Returns the value for [key]. | ||
* | ||
* @throws NoSuchElementException if no value could be loaded for [key] | ||
*/ | ||
suspend fun getValue(key: K): V { | ||
return get(key) ?: throw NoSuchElementException("No element with key $key") | ||
} | ||
|
||
private fun CoroutineScope.getAsync(key: K): CompletableFuture<V?> { | ||
return asyncCache.get(key) { k, executor -> | ||
future(executor.asCoroutineDispatcher()) { load(k) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/kotlin/org/wfanet/measurement/common/LoadingCacheTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 The Cross-Media Measurement Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.wfanet.measurement.common | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlin.test.assertFailsWith | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Test | ||
|
||
/** Test for [LoadingCache]. */ | ||
class LoadingCacheTest { | ||
@Test | ||
fun `ctor throws IllegalArgumentException when asyncCache has loader`() { | ||
val asyncCache = Caffeine.newBuilder().buildAsync<String, String> { key -> "$key-value" } | ||
assertFailsWith<IllegalArgumentException> { LoadingCache(asyncCache) { "$it-value" } } | ||
} | ||
|
||
@Test | ||
fun `get rethrows exception from load`() { | ||
val asyncCache = Caffeine.newBuilder().buildAsync<String, String>() | ||
val loadingCache = LoadingCache(asyncCache) { error("Loading error") } | ||
assertFailsWith<IllegalStateException> { runBlocking { loadingCache.get("foo") } } | ||
} | ||
|
||
@Test | ||
fun `get returns loaded value`() { | ||
val asyncCache = Caffeine.newBuilder().buildAsync<String, String>() | ||
val loadingCache = LoadingCache(asyncCache) { key -> "$key-value" } | ||
|
||
val value: String? = runBlocking { loadingCache.get("foo") } | ||
|
||
assertThat(value).isEqualTo("foo-value") | ||
} | ||
|
||
@Test | ||
fun `getValue returns loaded value`() { | ||
val asyncCache = Caffeine.newBuilder().buildAsync<String, String>() | ||
val loadingCache = LoadingCache(asyncCache) { key -> "$key-value" } | ||
|
||
val value: String = runBlocking { loadingCache.getValue("foo") } | ||
|
||
assertThat(value).isEqualTo("foo-value") | ||
} | ||
|
||
@Test | ||
fun `get returns null when load returns null`() { | ||
val asyncCache = Caffeine.newBuilder().buildAsync<String, String>() | ||
val loadingCache = LoadingCache(asyncCache) { null } | ||
|
||
val value: String? = runBlocking { loadingCache.get("foo") } | ||
|
||
assertThat(value).isNull() | ||
} | ||
|
||
@Test | ||
fun `getValue throws NoSuchElementException when load returns null`() { | ||
val asyncCache = Caffeine.newBuilder().buildAsync<String, String>() | ||
val loadingCache = LoadingCache(asyncCache) { null } | ||
|
||
assertFailsWith<NoSuchElementException> { runBlocking { loadingCache.getValue("foo") } } | ||
} | ||
} |