cache

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)

@JvmName(name = "infallibleCache")
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)