gpt4 book ai didi

java - 每天在特定时间刷新 Guava LoadingCache

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:23 37 4
gpt4 key购买 nike

我需要在每天的特定时间刷新我的缓存,在我的例子中,是在午夜。我有办法用 Guava LoadingCache 做到这一点吗?到目前为止,我只在一天后使用下一个代码更新了缓存:

private final LoadingCache<String, Long> cache = CacheBuilder.newBuilder()
.refreshAfterWrite(1, TimeUnit.DAYS)
.build(new CacheLoader<String, Long>() {
public Long load(String key) {
return getMyData("load", key);
}
}

最佳答案

这是实现 JB Nizeth's 的代码片段答案(Java 8):

long millisUntilMidnight = Duration
.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MIDNIGHT))
.toMillis();
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(() -> cache.invalidateAll(), millisUntilMidnight,
TimeUnit.DAYS.toMillis(1), TimeUnit.MILLISECONDS);

关于java - 每天在特定时间刷新 Guava LoadingCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37642678/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com