有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使缓存的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
}

找到了一个类似的链接Caching and invalidating cached Mono,但这不是我要找的


共 (1) 个答案

  1. # 1 楼答案

    目前没有办法强制缓存的信号失效。然而,正如菲尔·克莱所提到的,还有一个悬而未决的问题:https://github.com/reactor/reactor-core/issues/2324

    我们目前正在考虑扩大该操作符的作用域,其中可以提供一个谓词或获取一个Disposable来手动使缓存无效

    虽然我们想在3.4中添加这一功能,但它不会立即可用。x线