CLOVIS
04/10/2020, 12:48 PMsuspend 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)
request.second.complete(cache[request.first])
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?CLOVIS
04/10/2020, 6:08 PM