Reputation: 1629
I have a costly computation cached via a Mono
this way:
class Store {
private final Mono<List<Directory>> directories;
public Store(DirectoriesGateway provider) {
directories = Mono.fromCallable(provider::list).cache(Duration.ofMinutes(15);
}
public void process(Request r) {
List<Directory> dirs = directories.block();
// ...
}
}
It works well in production, while there is a warming time in our tests.
Is there a way to only cache after a delay? (let say 10 seconds)
Upvotes: 1
Views: 208
Reputation: 72254
To answer the question directly, you can use takeUntilOther()
then switchIfEmpty()
to achieve what you're after:
directories = delayCache(Mono.fromCallable(provider::list))
...
public <T> Mono<T> delayCache(Mono<T> mono) {
return mono.takeUntilOther(Mono.delay(Duration.ofSeconds(10)).cache())
.switchIfEmpty(mono.cache(Duration.ofMinutes(15)));
}
However, a word of warning - this sounds like an XY problem. I'd strongly advise not changing code that works well in production just to get your tests to run well, as that defies the point of having tests in the first place.
It sounds instead as though your test framework needs some work (in one way or another) to remove the limitation around the "warming time" you speak of.
Upvotes: 3