spring - Spring Webflux - 延迟初始化的内存缓存
问题描述
有人可以帮助我在 SpringWebflux 中使用正确的有状态服务模式吗?我有一个 REST 服务,它与外部 API 通信,需要在第一次调用期间从该 API 获取身份验证令牌并将其缓存以在所有下一次调用中重用。目前我有一个有效的代码,但并发调用会导致多个令牌请求。有没有办法处理并发?
@Service
@RequiredArgsConstructor
public class ExternalTokenRepository {
private final WebClient webClient;
private Object cachedToken = null;
public Mono<Object> getToken() {
if (cachedToken != null) {
return Mono.just(cachedToken);
} else {
return webClient.post()
//...
.exchangeToMono(response -> {
//...
return response.bodyToMono(Object.class)
})
.doOnNext(token -> cachedToken = token)
}
}
}
更新:我收到的令牌有一些过期时间,我需要在一段时间后刷新它。刷新请求也应该只调用一次。
解决方案
Mono
您可以在构造函数中初始化并使用cache
运算符:
@Service
public class ExternalTokenRepository {
private final Mono<Object> cachedToken;
public ExternalTokenRepository(WebClient webClient) {
this.cachedToken = webClient.post()
//...
.exchangeToMono(response -> {
//...
return response.bodyToMono(Object.class);
})
.cache(); // this is the important part
}
public Mono<Object> getToken() {
return cachedToken;
}
}
更新: cache
运算符还支持基于返回值的 TTL:https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#cache-java.util.function.Function-java。 util.function.Function-java.util.function.Supplier-
推荐阅读
- python - Kubernetes:一个现有的连接被远程主机强行关闭
- amazon-web-services - AWS Elastic Beanstalk Go 文件权限问题
- ssl - Hyperledger Fabric 节点 TLS 证书中的密钥用法
- sql - Sql批量查询执行是否涉及服务器和客户端之间的多次数据交换?
- mysql - Laravel -request 使 mySQL 更新失败
- c# - PDF 上未显示有效性未知符号
- angular - 如果 filterModified 事件中的条件匹配,则停止在网格上应用过滤器
- microsoft-graph-api - 使用 Microsoft Graph API 或 BOT API 发送 MS Teams 消息
- python - 在python中将基目录更改为上层目录
- angular - ERR_CONNECTION_REFUSED 在 Docker 容器内的 nginx 上托管的 Angular 应用程序上?