'Kotlin mono cache error and empty checking

I have seen this implemented in Java, but I cannot seem to get the correct code for Kotlin.

Here is how I am implementing the cache in Kotlin:

//Mono import, version 3.2.10.RELEASE
import reactor.core.publisher.Mono

//This is the member variable where the cache is actually accessed and is set when first ran
private var cachedJson: Mono<JsonNode>? = null

cachedJson = client
.get
.retrieve
.bodyToMono(JsonNode::class.java)
.cache(Duration.ofMinutes(2))

The code already works and caches correctly for 2 minutes. I am just trying to add the cache function with mono that can set duration for error and empty checking.

Here is the Java code that is equivalent to what I am trying to do in Kotlin:

.cache(message -> Duration.ofMinutes(5),
throwable -> Duration.ZERO,
() -> Duration.ZERO

I have seen this example used in a few spots, but not in Kotlin. When I look at the method signatures for the cache function implementations I see what I need but just cannot write it correctly like the Java example. Here is what the hint looks like:

cache(ttlForValue: (JsonNode?) -> Duration!, ttlForError: (Throwable!) -> Duration!, ttlForEmpty: () -> Duration!) Mono<JsonNode!>

Again the code works fine and system works as a whole, it is just implementing this extra checking with the cache duration.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source