我是spring webflux的初学者。在研究时,我发现了一些代码,例如:
Mono result = someMethodThatReturnMono().cache();
“缓存”这个名字告诉我缓存的东西,但是缓存在哪里以及如何检索缓存的东西?是咖啡因之类的吗?
我是spring webflux的初学者。在研究时,我发现了一些代码,例如:
Mono result = someMethodThatReturnMono().cache();
“缓存”这个名字告诉我缓存的东西,但是缓存在哪里以及如何检索缓存的东西?是咖啡因之类的吗?
它会缓存 Flux/Mono 前面步骤的结果,直到cache()
调用该方法,检查此代码的输出以查看它的运行情况:
import reactor.core.publisher.Mono;
public class CacheExample {
public static void main(String[] args) {
var mono = Mono.fromCallable(() -> {
System.out.println("Go!");
return 5;
})
.map(i -> {
System.out.println("Double!");
return i * 2;
});
var cached = mono.cache();
System.out.println("Using cached");
System.out.println("1. " + cached.block());
System.out.println("2. " + cached.block());
System.out.println("3. " + cached.block());
System.out.println("Using NOT cached");
System.out.println("1. " + mono.block());
System.out.println("2. " + mono.block());
System.out.println("3. " + mono.block());
}
}
输出:
Using cached
Go!
Double!
1. 10
2. 10
3. 10
Using NOT cached
Go!
Double!
1. 10
Go!
Double!
2. 10
Go!
Double!
3. 10