在我的应用程序中,我使用的是spring网络流量,我使用webclient从一些第三方API中检索细节。现在,我想将第一次webClient响应存储在内存缓存中,以便第二次能够直接从缓存中获得这些响应。我试图在内存缓存机制和"caffine“中使用Spring引导。但没有一个像预期的那样起作用。application.yml:
spring:
cache:
cache-names: employee
caffiene:
spec: maximumSize=200, expireAfterAccess=5mEmployeeApplication.java:
@SpringBootApplication
@EnableCaching
public class EmployeeApplication{
public static void main(String[] args){
}
}EmployeeController.java:有一个rest端点employee/all,它从第三方Api中获取所有雇员。EmployeeService.java:
@Service
@Slf4j
public class EmployeeService{
@Autowired
private WebClient webClient;
@Autowired
private CacheManager cacheManager;
@Cacheable("employee")
public Mono<List<Employee>> getAllEmployee(){
log.info("inside employee service {}");
return webClient.get()
.uri("/employees/")
.retrieve()
.bodyToMono(Employee.class);
}
}虽然我已经配置了缓存名,但是当我第二次访问url时,它正在调用服务方法。需要使用什么缓存机制来缓存Mono响应?请建议一下。
发布于 2022-03-18 22:41:16
缓存反应性发布者有几种选择。
cache API在定义的持续时间内缓存MonoemployeeService.getAllEmployee()
.cache(Duration.ofMinutes(60))
.flatMap(employees -> {
// process data
})咖啡因支持基于CompletableFuture的异步缓存,可以很容易地适应反应性API。
AsyncLoadingCache<String, List<Employee>> cache = Caffeine.newBuilder()
.buildAsync((tenant, executor) ->
employeeService.getAllEmployee(tenant).toFuture()
);
Mono<List<Employee>> getEmployee(String tenant) {
return Mono.fromCompletionStage(clientCache.get(tenant));
}reactor-extra。如果需要根据不同的输入(例如,多租户环境)缓存结果,则此选项更适合。UPDATE:自反应堆-额外3.4.7以来,CacheMono已被废弃。最好使用#2与咖啡因一起使用外部缓存。
下面是番石榴的一个例子,但您可以将其修改为CacheManager
Cache<String, List<Employee>> cache = CacheBuilder.newBuilder()
.expireAfterWrite(cacheTtl)
.build();
Mono<List<Employee>> getEmployee(String tenant) {
return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(key)).map(Signal::next), tenant)
.onCacheMissResume(() -> employeeService.getAllEmployee(tenant))
.andWriteWith((key, signal) -> Mono.fromRunnable(() ->
Optional.ofNullable(signal.get())
.ifPresent(value -> cache.put(key, value))
)
);
}https://stackoverflow.com/questions/71532361
复制相似问题