我试图在反应式编程中获得从 redis 读取的执行时间,在查找文档时我能够看到该elapsed()
方法将执行相同的操作并实现如下代码。
Flux.fromIterable(getActions(httpHeaders))
.parallel()
.runOn(Schedulers.parallel())
.flatMap(actionFact -> methodToReadFromCache(actionFact))
.sequential();
public Mono<ActionFact> methodToReadFromCache(actionFact) {
return Mono.fromCallable(() -> getKey(actionFact))
.flatMap(cacheKey ->
redisOperations.hasKey(key)
.flatMap(aBoolean -> {
if (aBoolean) {
return redisOperations.opsForValue().get(cacheKey);
}
return authzService.getRolePermissions(actionFact)
.flatMap(policySetResponse ->
//save in cache
);
})
.elapsed()
.flatMap(lambda -> {
LOG.info("cache/service processing key:{}, time:{}", key, lambda.getT1());
return Mono.just(lambda.getT2());
});
输出:
cache/service processing key:KEY1, time:3
cache/service processing key:KEY2, time:4
cache/service processing key:KEY3, time:18
cache/service processing key:KEY4, time:34
cache/service processing key:KEY5, time:46
cache/service processing key:KEY6, time:57
cache/service processing key:KEY7, time:70
cache/service processing key:KEY8, time:81
cache/service processing key:KEY9, time:91
cache/service processing key:KEY10, time:103
cache/service processing key:KEY11, time:112
cache/service processing key:KEY12, time:121
cache/service processing key:KEY13, time:134
cache/service processing key:KEY14, time:146
cache/service processing key:KEY15, time:159
我预计每个缓存请求所花费的时间将小于 5 毫秒,就像第一个和第二个请求一样,但情况并非如此。是否elapsed()
将当前获取时间添加到累积中?根据我的理解,从通量发出的每个项目都是独立的?