每天在特定时间刷新Guava LoadingCache

编程入门 行业动态 更新时间:2024-10-24 16:25:48
本文介绍了每天在特定时间刷新Guava LoadingCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要每天在特定时间(例如我的午夜)刷新缓存。我有办法用Guava LoadingCache做到这一点吗? 到目前为止,我只需要在一天后使用以下代码更新缓存即可。

I need that my cache be refreshed everyday at a specific time, in my case, at midnight. I have way to do this with Guava LoadingCache? So far I only got the cache be renewed after a day, with the next code:

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的答案(Java 8 ):

Here's a code snipped that implements JB Nizeth's answer (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);

更多推荐

每天在特定时间刷新Guava LoadingCache

本文发布于:2023-11-25 18:41:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   在特定   LoadingCache   Guava

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!