Reputation: 6308
AsyncLoadingCache<String, String> cache = Caffeine.newBuilder().buildAsync(
(String k, Executor e) ->
doThingReturnsMono()
.subscribeOn(Schedulers.fromExecutor(e))
.toFuture()
);
cache.put("fail", Mono.error<String>(new RuntimeException()).toFuture());
cache.put("success", Mono.just("success").toFuture());
System.err.println(cache.asMap().getKeys()); // Prints ["success"]
How can we put a Mono.error into an AsyncLoadingCache?
Upvotes: 0
Views: 92
Reputation: 6308
The async cache handles values coming in asynchronously, it does not store raw Futures/Monos. It instead stores the underlying data, in this case a String.
A wrapper type is needed in this case:
class MaybeString {
// ...
String str;
boolean isError;
}
AsyncLoadingCache<String, String> cache = Caffeine.newBuilder().buildAsync(
(MaybeString k, Executor e) ->
doThingReturnsMono()
.subscribeOn(Schedulers.fromExecutor(e))
.toFuture()
);
cache.put("fail", Mono.just(new MaybeString("", true)).toFuture());
cache.put("success", Mono.just(new MaybeString("success", false)).toFuture());
System.err.println(cache.asMap().getKeys()); // Prints ["fail", "success"]
Upvotes: 2