Spring 可缓​​存强制刷新

Spring cacheable force refresh

例如,我有一个向每个用户显示推荐电影的网络应用程序。服务的方法对另一个平台和 return 电影进行 API 调用。我必须实现以下逻辑:
用户请求他的电影推荐。如果 API 调用成功,将其放入缓存并 return。否则不是(如果方法 returned null)。但用户也可以按刷新按钮。在这种情况下,为该用户逐出缓存,执行 API 调用并将其再次放入缓存中。如何强制刷新缓存?

有一个 @CacheEvict 注释,您可以在触发 "refresh" 的方法上使用它。检查 the documentation for more details

您可以添加 <a href="https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#cache-annotations-cacheable-condition" rel="nofollow noreferrer">condition</a> 参数 @Cacheable。 如果评估值为 false,将调用该方法。