cache
fun <Identifier, Failure, Value> cache(transform: suspend (Identifier) -> Outcome<Failure, Value>): Cache<Identifier, 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, or about the type parameters, see Cache.
Example
object NegativeNumber
val squaredRoot = cache<Double, NegativeNumber, Double> {
if (it >= 0) {
sqrt(it).success()
} else {
NegativeInteger.failed()
}
}
println(squaredRoot[25.0].now()) // Success(5.0)
println(squaredRoot[-5.0].now()) // Failure(NegativeNumber)
Content copied to clipboard
fun <Identifier, Value> cache(transform: suspend (Identifier) -> Value): InfallibleCache<Identifier, Value>(source)
Cache implementation which calls a given transform suspending function.
The transform function is considered always successful. This allows to bypass the cache's error encoding. For more information, see InfallibleCache.
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, or about the type parameters, see Cache.
Example
val squared = cache<Int, Int> { it * 2 }
println(squared[5]) // Success(25)
Content copied to clipboard