https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jakub Pi

03/29/2020, 10:46 PM
I have a
Map<FileKey, Deferred<String>>
where I want to cache (expensive-to-compute) SHA-512 results. My objective is to only calculate hash values once, on the first retrieval from the Map. So I know I want some kind of Future object. I know I can start the computation in the getter (unless it's already complete) and then just return the result normally. But is Deferred the best way to do this or is there a different construct/pattern I should be looking at instead? I probably want to block on the retrieval, so Deferred may not be the best choice.
I was overthinking. Swapped to Lazy<T> instead of Deferred
j

Jakub Pi

03/30/2020, 2:56 PM
Thanks. So the original idea is feasible. Will be worth doing for web requests but probably not local files.
2 Views