【发布时间】:2019-11-19 23:57:48
【问题描述】:
我正在尝试在反应式编程中获取从 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() 是否将当前获取时间添加到累积中?根据我的理解,从通量发出的每个项目都是独立的?
【问题讨论】:
标签: java spring-webflux project-reactor spring-data-redis-reactive