Java 如何获得反应堆堆芯';让我们开始工作吧

Java 如何获得反应堆堆芯';让我们开始工作吧,java,caching,asynchronous,reactive-programming,project-reactor,Java,Caching,Asynchronous,Reactive Programming,Project Reactor,我很难理解反应堆堆芯的通量缓存是如何工作的 然而,也许这个问题的答案可能微不足道,或者我的示例代码是彻头彻尾的白痴,但在我的研究之后,在代码示例/博客/教程中没有显示缓存如何与流量一起工作。可能任何人都可以帮助我运行它,这样其他人也有一个功能通量缓存的例子,如锅炉板 因此,我有以下代码: // The source with cache: Flux<Instant> values = Flux .generate((SynchronousSink<Ins

我很难理解反应堆堆芯的通量缓存是如何工作的

然而,也许这个问题的答案可能微不足道,或者我的示例代码是彻头彻尾的白痴,但在我的研究之后,在代码示例/博客/教程中没有显示缓存如何与流量一起工作。可能任何人都可以帮助我运行它,这样其他人也有一个功能通量缓存的例子,如锅炉板

因此,我有以下代码:

// The source with cache:
Flux<Instant> values
    = Flux
        .generate((SynchronousSink<Instant> it) -> {
                    it.next(Instant.now());
                    waitingForMillis(50);
        })
        .take(5)
        .cache(Duration.ofMillis(500)); // 500ms per item


// 1st time:
System.out.println("Printing data the 1st time, then the cache should be filled:");
values.subscribe(System.out::println);

System.out.printf("%nWaiting ...%n");
waitingForMillis(1000);

// 2nd time:
System.out.println("Printing data the 2nd time, still the data from the cache should be printed:");
values.subscribe(System.out::println);

System.out.printf("%nWaiting ...%n");
waitingForMillis(30_000);

// 3rd time:
System.out.println("Printing data the 3rd time, now, new data should be printed (be generated), because the cache timed out:");
values.subscribe(System.out::println);
//具有缓存的源:
通量值
=流量
.生成((SynchronousSink it)->{
it.next(Instant.now());
waitingForMillis(50);
})
.采取(5)
.cache(持续时间.百万(500));//每件500毫秒
//第一次:
System.out.println(“第一次打印数据时,应填充缓存:”);
value.subscribe(System.out::println);
System.out.printf(“%n等待…%n”);
waitingForMillis(1000);
//第二次:
System.out.println(“第二次打印数据时,仍然应该打印缓存中的数据:”;
value.subscribe(System.out::println);
System.out.printf(“%n等待…%n”);
waitingForMillis(30000);
//第三次:
System.out.println(“第三次打印数据,现在,应该打印(生成)新数据,因为缓存超时:”;
value.subscribe(System.out::println);
我心目中的情景是:

实际上,我认为缓存的流量值将在第一次和第二次为其订阅者提供缓存值。-这是第三次,在30000ms之后,我认为,这些值将为订阅者提供新的值,因为缓存超时了

在我解释之后实际发生了什么:

缓存从不超时,并且始终将初始值提供给以下订阅服务器

问题(是,多个问题):


我做错了什么?该示例中的思考/解释错误是什么?在我想到的场景之后,如何运行此代码以实现缓存?

以下代码按照您的预期工作。我将生成器函数中的内部等待更改为使用
delayElements
方法,并延长了缓存时间。(不是每个元素,而是每个元素的生存期。)我测试了
缓存和
take
两种方法,两种方法都可以

Flux<Instant> values =
Flux.generate(
    (SynchronousSink<Instant> it) -> {
        it.next(Instant.now());
    })
    .delayElements(Duration.ofMillis(50))
    .cache(Duration.ofMillis(5000))
    .take(5);

// 1st time:
System.out.println("Printing data the 1st time, then the cache should be filled:");
values.subscribe(System.out::println);

System.out.printf("%nWaiting ...%n");
waitingForMillis(1000);

// 2nd time:
System.out.println(
    "Printing data the 2nd time, still the data from the cache should be printed:");
values.subscribe(System.out::println);

System.out.printf("%nWaiting ...%n");
waitingForMillis(30_000);

// 3rd time:
System.out.println(
    "Printing data the 3rd time, now, new data should be printed (be generated), because the cache timed out:");
values.subscribe(System.out::println);
Printing data the 1st time, then the cache should be filled:

Waiting ...
2018-05-07T10:03:16.316Z
2018-05-07T10:03:16.412Z
2018-05-07T10:03:16.465Z
2018-05-07T10:03:16.517Z
2018-05-07T10:03:16.569Z
Printing data the 2nd time, still the data from the cache should be printed:
2018-05-07T10:03:16.316Z
2018-05-07T10:03:16.412Z
2018-05-07T10:03:16.465Z
2018-05-07T10:03:16.517Z
2018-05-07T10:03:16.569Z

Waiting ...
Printing data the 3rd time, now, new data should be printed (be generated), because the cache timed out:
2018-05-07T10:03:47.352Z
2018-05-07T10:03:47.403Z
2018-05-07T10:03:47.458Z
2018-05-07T10:03:47.511Z
2018-05-07T10:03:47.565Z