spand
06/15/2018, 12:49 PMval cache = ConcurrentHashMap<Int, Deferred<String>>()
suspend fun getById(id: Int) : String {
return cache.computeIfAbsent(id) {
async { asyncAction(id) }
}.await()
}
suspend fun asyncAction(id: Int): String = TODO()
dave08
06/15/2018, 2:19 PMactor
?withoutclass
06/15/2018, 2:26 PMasync
only to immediately await
? may as well just use withContext
Daniel Tam
06/15/2018, 3:48 PMactor
better only because it doesn't block? ConcurrentHashMap
is less boilerplate imo, and reads are lock free anywaybj0
06/15/2018, 3:50 PMbj0
06/15/2018, 3:51 PMsuspend fun getById(id: Int) = withContext(CommonPool) { asyncAction(id) }
bj0
06/15/2018, 3:51 PMDaniel Tam
06/15/2018, 3:53 PMwithContext
rather than simply running asyncAction
?bj0
06/15/2018, 4:11 PMwithoutclass
06/15/2018, 4:22 PMval cache = ConcurrentHashMap<Int, String>()
suspend fun getById(id: Int) : String {
return cache.computeIfAbsent(id) {
withContext(IO_DISPATCHER) { asyncAction(id) }
}
}
suspend fun asyncAction(id: Int): String = "Some String"
withoutclass
06/15/2018, 4:22 PMDaniel Tam
06/15/2018, 4:35 PMcomputeIfAbsent
withoutclass
06/15/2018, 4:40 PMDaniel Tam
06/15/2018, 5:17 PMbj0
06/15/2018, 5:34 PMcache
, was focused on the coroutine stuff and assumed the logic was inside asyncAction
spand
06/16/2018, 11:41 AMspand
06/16/2018, 11:44 AMuli
06/16/2018, 5:22 PMwithoutclass
06/17/2018, 5:54 PM