java - 如何缓存 Mono 对象

标签 java reactive-programming project-reactor

我有以下代码:

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

现在我使我的存储库成为响应式(Reactive)的。我的问题是如何使缓存存储现在为单声道和通量。

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

我遇到了以下Mono and Flux Caching但这仅用于查找,因为我也是响应式(Reactive)编程的初学者。

最佳答案

针对您的情况,最好的方法是依赖 save 的事实操作返回 Mono<T>发出保存的实体。

因此您可以使用doOnNext每当该实体保存在数据库中时,也将该实体放入缓存中:

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

结果Mono<T>每当实体被订阅时,都会保存然后缓存该实体。

关于java - 如何缓存 Mono 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49586959/

相关文章:

java - 如何将自定义类对象注入(inject)Junit环境?

java - 设计问题 - 应该为 GUI 使用单独的线程吗?

jquery - 有没有更好的方法来使用 Meteor.js 根据输入字段响应式更新 html?

spring-webflux - 我什么时候使用 Mono.flatMapIterable 和 Mono.flapMapMany?

apache-kafka - 如何在 Webflux 应用程序中制作 Spring Cloud Stream 消费者?

java - 如何在 Reactor 中使用 Context 和 flatMap()?

java - Hibernate搜索lucene : Updating index document by removing a child entity makes children entity as orphan in index document

java - 如何在 Windows 8 中的命令提示符下运行简单的 java 类

c# - 无法取消订阅 Rx

java - 响应式(Reactive) Mono API 多参数请求处理程序