cache
fun <Identifier, Context, Failure, Value> cache(transform: suspend (Identifier, Context) -> Outcome<Failure, Value>): ContextualCache<Identifier, Context, Failure, Value>(source)
Cache implementation which calls a given transform suspending function.
This adapter is meant to be used as the first layer in a layer chain. By itself, it does no caching (all calls to get call transform). To learn more about layer chaining, see Cache. To learn more about the type parameters, see ContextualCache.
Example
class User
class SearchFilters
data class Page(val number: Int)
data class Post(…)
suspend fun getTimeline(filters: SearchFilters, user: User, page: Page): List<Post> = …
val cachedTimeline = cache<SearchFilters, Pair<User, Page>, Nothing, List<Post>> { it, (user, page) ->
getTimeline(filters, user, page).success()
}
val me = User()
val filters = SearchFilters()
println(cachedTimeline[filters, me to Page(0)].now())
println(cachedTimeline[filters, me to Page(1)].now())
Content copied to clipboard
See also
Non-contextual equivalent