如何使缓存的 Mono 失效?

How to invalidate cached Mono?

有没有办法使缓存的 Mono 失效?手动还是有条件?

这是我的案例:

private Mono<String> cachedMono;

public MyService() {
  this.cachedMono = cacheData()
      .cache(value -> Duration.ofHours(1), ex -> Duration.ZERO, () -> Duration.ZERO);
}

private Mono<String> cacheData() {
  return webClient.get()
      .uri("/api/data")
      .retrieve()
      .bodyToMono(String.class);
}

public Mono<String> retrieveData() {
  return cachedMono;
}

一切正常,数据在 1 小时后失效。 但是我想要的是一旦我从外部收到数据已更改的通知并且我应该刷新缓存就使它无效。

public void dataHasBeenChangedNotification(String newData) {
  // how to update cache with the new data?
  // or at least force the cache to invalidate and not to wait 1 hour
}

找到了类似的 link 但这不是我要找的东西。

目前无法强制使缓存信号失效。但是,正如 Phil Clay 提到的那样,存在一个悬而未决的问题:https://github.com/reactor/reactor-core/issues/2324.

我们目前正在考虑扩大该运算符的范围,其中可以提供谓词或获取 Disposable 以手动使缓存无效。

这不会立即可用,但我们想在 3.4.x 行中添加该功能。