我正在尝试使用带有咖啡因和一些带@Cacheable注释的函数的Spring Boot CacheManager。在我们的测试中,对控制器端点的异步调用出现了问题,这些问题似乎与我们使用非异步缓存的事实有关。
在做一些研究时,我看到了很多手动在CompletableFuture中使用咖啡因的例子,但在AsyncCacheLoader和Spring Boot CacheManager和@Cacheable注释中什么也找不到。看起来Cache和AsyncCache有非常不同的API。是否可以异步使用默认的Spring Boot CacheManager?
谢谢!
发布于 2021-10-28 21:30:29
由Ben Manes在评论后更新答案。对于反应式Spring Webflux,您可以将@Cacheable与Spring Boot cache Manager一起使用,但在hoods下面,值存储在同步缓存中。下面是一个例子
@RestController
@SpringBootApplication
@EnableCaching
public class GatewayApplication {
@PostMapping(value ="/test", produces = "application/json")
public Flux<String> handleRequest(@RequestBody String body) {
return getData(body);
}
@Cacheable("cache")
private Flux<String> getData(String body) {
return WebClient.create().post()
.uri("http://myurl")
.body(BodyInserters.fromObject(body))
.retrieve().bodyToFlux(String.class).cache();
}
}正如你在上面的例子中看到的,它使用Spring Webflux(项目反应堆)和@Cacheable方法返回异步的Flux。
如果您需要反应式缓存,则可以直接使用AsyncCache,因为它存储CompleteableFutures
https://stackoverflow.com/questions/69760764
复制相似问题