1
votes

I have the following code:

final T savedEntity = repository.save(entity);
entityCacheService.putIntoCache(entity.getId(), savedEntity);

Now I made my repository reactive. My question is how to make the cache store now mono and flux.

final Mono<T> savedEntity = repository.save(entity);
entityCacheService.putIntoCache(entity.getId(), <<What here>>);

I came across the following Mono and Flux Caching but it's only for lookup and since I am also a beginner in reactive programming.

1

1 Answers

1
votes

The best way in your case is to rely on the fact that the save operation returns a Mono<T> that emits the saved entity.

You could thus use doOnNext to also put that entity in the cache whenever it is saved in DB:

final Mono<T> savedEntity = repository.save(entity)
    .doOnNext(entity -> entityCacheService.putIntoCache(entity.getId(), entity);

The resulting Mono<T> will both save then cache the entity whenever it is subscribed to.