04/10/2020, 12:48 PM
I'd like to implement a coroutine that implements a cache of values, indexed by ID (an Int). So far I have:
suspend fun <T> cache(requests: ReceiveChannel<Pair<Int
SomeKindOfFuture<T>>> {
  val cache = mutableMapOf<Int, T>()
for (request in requests) {
But now I'm stuck. What I would like is that if the value is not in the cache, an expensive calculation is started, but I don't want the `cache`function to block nor suspend: if the calculation is started, I still want the cache to be able to reply to other requests, so I can't do something like:
if (request.first in cache)
else {
  cache.add(request.first, launch { someCalculation(request.second) }.await())
I'm starting to think the best solution would be that the cache could receive either a ‘get' or ‘set' request, and when receiving a ‘get' that does not exists, it would create a new coroutine which would do the calculation and then send a ‘set' request, so the cache could continue processing requests in the meantime? What do you think?


04/10/2020, 2:13 PM

around 29minutes, I think it might be similar to your Problem have a look


04/10/2020, 6:08 PM
Thanks ^^ I'll do that