Skip to content

Commit 713f5ff

Browse files
committed
change the get function signature
1 parent bd6d848 commit 713f5ff

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/main/kotlin/com/moumoux/quinine/Quinine.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.github.benmanes.caffeine.cache.Caffeine
44
import com.moumoux.quinine.impl.QuinineLocalCache
55
import com.moumoux.quinine.impl.QuinineLocalLoadingCache
66
import io.reactivex.Single
7+
import kotlinx.coroutines.GlobalScope
8+
import kotlinx.coroutines.launch
79

810
import com.moumoux.quinine.reactive.QuinineCache as rxQuinineCache
911
import com.moumoux.quinine.reactive.QuinineLoadingCache as rxQuinineLoadingCache
@@ -72,9 +74,11 @@ class Quinine<K: Any, V> private constructor() {
7274
*
7375
* @return [QuinineLoadingCache] instance
7476
*/
75-
fun <K1 : K, V1 : V?> build(mappingFunction: (K1) -> V1): QuinineLoadingCache<K1, V1> {
77+
fun <K1 : K, V1 : V?> build(mappingFunction: suspend (K1) -> V1): QuinineLoadingCache<K1, V1> {
7678
return QuinineLocalLoadingCache(caffeine.build {
77-
Single.create { emitter -> emitter.onSuccess(mappingFunction(it)) }
79+
Single.create<V1> { emitter ->
80+
GlobalScope.launch { emitter.onSuccess(mappingFunction(it)) }
81+
}.cache()
7882
})
7983
}
8084

src/main/kotlin/com/moumoux/quinine/QuinineCache.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ interface QuinineCache<K : Any, V> {
9898
* @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is
9999
* left unestablished
100100
*/
101-
suspend fun get(key: K, mappingFunction: (K) -> V): V
101+
suspend fun get(key: K, mappingFunction: suspend (K) -> V): V
102102

103103
/**
104104
* Returns the value associated with the [key] in this cache, or <pre>null</pre> if there is no

src/main/kotlin/com/moumoux/quinine/impl/QuinineLocalCache.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import com.github.benmanes.caffeine.cache.Cache
44
import com.moumoux.quinine.QuinineCache
55
import com.moumoux.quinine.QuinineCacheStats
66
import io.reactivex.Single
7+
import kotlinx.coroutines.*
78
import kotlinx.coroutines.rx2.await
8-
import java.util.concurrent.Executors
9+
import kotlin.coroutines.CoroutineContext
910

1011
internal open class QuinineLocalCache<K : Any, V>(private val cache: Cache<K, Single<V>>) :
11-
QuinineCache<K, V> {
12-
private val loaderExecutor = Executors.newSingleThreadExecutor()
12+
QuinineCache<K, V>, CoroutineScope {
13+
override val coroutineContext: CoroutineContext = Dispatchers.IO
1314

1415
override val estimatedSize: Long
1516
get() = cache.estimatedSize()
@@ -27,12 +28,14 @@ internal open class QuinineLocalCache<K : Any, V>(private val cache: Cache<K, Si
2728
map.forEach { (k, u) -> cache.put(k, Single.just(u).cache()) }
2829
}
2930

30-
override suspend fun get(key: K, mappingFunction: (K) -> V): V {
31-
return cache.get(key) {
32-
Single.create<V> { emitter ->
33-
loaderExecutor.execute { emitter.onSuccess(mappingFunction(it)) }
34-
}.cache()
35-
}!!.await()
31+
override suspend fun get(key: K, mappingFunction: suspend (K) -> V): V {
32+
return coroutineScope {
33+
cache.get(key) {
34+
Single.create<V> { emitter ->
35+
launch { emitter.onSuccess(mappingFunction(it)) }
36+
}.cache()
37+
}!!.await()
38+
}
3639
}
3740

3841
override suspend fun getIfPresent(key: K): V? = cache.getIfPresent(key)?.await()

0 commit comments

Comments
 (0)