I need to cache data coming from a ReactiveMongoRepository
. The data gets updated roughly twice a year, so I don't care about expiring the cache.
Since we can't use @Cacheable with a flux, I'd like to find a straightforward, easy way to store the data coming from Mongo to redis, and use that data if present, else store it and serve the original data.
Is there are more straightforward way to do so than
@GetMapping
public Flux<AvailableInspection> getAvailableInspectionsRedis() {
AtomicInteger ad = new AtomicInteger();
return availableInspectionReactiveRedisOperations.opsForZSet().range("availableInspections", Range.<Long>from(Range.Bound.inclusive(0L)).to(Range.Bound.inclusive(-1L)))
.switchIfEmpty(availableInspectionMongoRepository.findAll().map(e -> {
availableInspectionReactiveRedisOperations.opsForZSet().add("availableInspections", e, ad.getAndIncrement()).block();
return e;
}));
}
What I'm looking for explicitly is an option that lets me cache the data just as the @Cacheable annotation would do it. I'm looking for a generic solution to be able to cache any kind of flux.