https://kotlinlang.org logo
Title
a

Aaron Stacy

03/13/2019, 1:45 PM
👋 Hi all! Say I want something like
suspend fun request(url: URL): MyDataClass
, but I want to cache the HTTP results so I don’t make requests twice. Anyone know a good example of how to do that? @elizarov’s excellent post https://medium.com/@elizarov/deadlocks-in-non-hierarchical-csp-e5910d137cc has a nice example, but the only way I can think to wrap the channels in a suspend function is using `Deferred`’s. Is that the way to do it? It feels clunky.
w

withoutclass

03/13/2019, 2:21 PM
Perhaps you could use an
actor
where the
actor
caches the results, so if it receives a message asking for
MyDataClass
it will either fetch it or used the cached version?
a

Aaron Stacy

03/13/2019, 2:31 PM
Ah yes, so this approach seems to use `Deferred`s https://github.com/kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-core/test/guide/example-sync-07.kt so maybe that is the way to do it.
w

withoutclass

03/13/2019, 2:38 PM
That’s probably the cleanest way.