存在缓存时调用Reactor CacheMono onCacheMissResume

问题描述

我想使用cachemono并创建一个测试进行评估。因此,我创建了2个单声道并进行了串联。我注意到的是,oncachemissresume被两个Mono调用,但是在第二个Mono上,未写入缓存。我的考试有什么问题吗?我想使用oncachemissresume填充缓存,当前的测试结果对我没有帮助。

atomicreference<Context> storeRef = new atomicreference<>(Context.empty());

        String key = "myid";
        Mono<Integer> cachedMonoFirst = CacheMono
                .lookup(k -> Mono.justOrEmpty(storeRef.get().<Integer>getorEmpty(k))
                                .map(integer -> {
                                    log.error("first :: cache lookup result {}",integer);
                                    return Signal.next(integer);
                                }),key)
                .onCacheMissResume(() -> {
                    log.info("first :: cache missed ");
                    return Mono.just(123);})
                .andWriteWith((k,sig) -> {
                    log.info("first :: cache write");
                    return Mono.fromrunnable(() ->
                        storeRef.updateAndGet(ctx -> ctx.put(k,sig.get())));});

        Mono<Integer> cachedMonoSecond = CacheMono
                .lookup(k -> Mono.justOrEmpty(storeRef.get().<Integer>getorEmpty(k))
                                .map(integer -> {
                                    log.error("second :: cache lookup result {}",key)
                .onCacheMissResume(() -> {
                    log.error("second :: cache missed");
                    return Mono.just(456);})
                .andWriteWith((k,sig) -> {
                    log.info("second :: cache write");
                    return Mono.fromrunnable(() ->
                        storeRef.updateAndGet(ctx -> ctx.put(k,sig.get())));});

        Flux<Integer> cacheFlux = cachedMonoFirst.concatWith(cachedMonoSecond);
        StepVerifier
                .create(cacheFlux)
                .consumeNextWith(data -> {
                    assertthat(storeRef.get().<Integer>getorEmpty(key)).get().isEqualTo(data);
                    log.info(" first :: from cache {} {}",data,storeRef.get().<Integer>getorEmpty(key));
                })
                .consumeNextWith(data -> {
                    assertthat(storeRef.get().<Integer>getorEmpty(key)).get().isEqualTo(data);
                    log.info(" second :: from cache {} {}",storeRef.get().<Integer>getorEmpty(key));
                })
                .verifyComplete();        

日志如下

CacheTest - first :: cache missed 
CacheTest - first :: cache write
CacheTest -  first :: from cache 123 Optional[123]
CacheTest - second :: cache missed
CacheTest - second :: cache lookup result 123
CacheTest -  second :: from cache 123 Optional[123]

为什么second :: cache missedsecond :: cache lookup result 123之前被调用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...