到期时间@cacheable spring boot

新手上路,请多包涵

我已经实现了一个缓存,现在我想添加一个过期时间。

如何使用 @Cacheable 在 spring boot 中设置到期时间?

这是一段代码:

 @Cacheable(value="forecast",unless="#result == null")

原文由 nole 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 725
1 个回答

我这样使用生活黑客:

 @Configuration
@EnableCaching
@EnableScheduling
public class CachingConfig {

  public static final String GAMES = "GAMES";

  @Bean
  public CacheManager cacheManager() {
    ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(GAMES);
    return cacheManager;
  }

  @CacheEvict(allEntries = true, value = {GAMES})
  @Scheduled(fixedDelay = 10 * 60 * 1000 ,  initialDelay = 500)
  public void reportCacheEvict() {
    System.out.println("Flush Cache " + dateFormat.format(new Date()));
  }

}

原文由 Atum 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题