我是否可以像cron作业一样,将javaspring缓存安排在每小时的顶部过期?

8e2ybdfx  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(450)

我把它设置为12小时后过期。但是,它也会在每个缓存首次写入后12小时过期。我只想在早上12点和晚上12点刷新。这可能吗?在我的cacheconfig文件中,我有:

  1. @Component
  2. @EnableCaching
  3. public class CacheConfig {
  4. @Bean
  5. public Caffeine defaultCacheConfig() {
  6. return Caffeine.newBuilder()
  7. .expireAfterWrite(12, TimeUnit.HOURS);
  8. }
  9. }

我正在使用缓存库。

ykejflvf

ykejflvf1#

咖啡因支持可变过期,其中条目的持续时间必须独立计算。如果您希望所有条目同时过期,您可以编写,

  1. Caffeine.newBuilder()
  2. .expireAfter(new Expiry<K, V>() {
  3. public long expireAfterCreate(K key, V value, long currentTime) {
  4. var toMidnight = Duration.between(LocalDate.now(),
  5. LocalDate.now().plusDays(1).atStartOfDay());
  6. var toNoon = Duration.between(LocalTime.now(), LocalTime.NOON);
  7. return toNoon.isNegative() ? toMidnight.toNanos() : toNoon.toNanos();
  8. }
  9. public long expireAfterUpdate(K key, V value,
  10. long currentTime, long currentDuration) {
  11. return currentDuration;
  12. }
  13. public long expireAfterRead(K key, V value,
  14. long currentTime, long currentDuration) {
  15. return currentDuration;
  16. }
  17. }).build();

对于这样一个简单的任务来说,使用expiration可能有点过头了。相反,如果您想清除缓存,那么一个计划任务可以这样做,正如@alexzander zharkov所建议的那样。

  1. @Scheduled(cron = "0 0,12 * * *")
  2. public void clear() {
  3. cache.invalidateAll();
  4. }

由于这会清空缓存,因此在重新加载条目时会有性能损失。相反,您可以异步刷新缓存,以便在不惩罚任何调用者的情况下重新加载条目。

  1. @Scheduled(cron = "0 0,12 * * *")
  2. public void refresh() {
  3. cache.refreshAll(cache.asMap().keySet());
  4. }
展开查看全部
euoag5mw

euoag5mw2#

我相信咖啡因不支持这种安排。但是如果这是一个很强的需求,并且应该按如下方式实现-您可以使用spring的@scheduled注解,它允许使用cron config。您可以在这里阅读:https://www.baeldung.com/spring-scheduled-tasks
因此,对于我的愿景,它可以通过以下方式工作:
设置计划的spring服务并配置所需的cron。通过字段或构造函数自动连接cachemanager,并设置refreshcache()以清除cachemanager的所有缓存。我将留下一个代码示例,但不确定它是否100%有效:)

  1. @Component
  2. public class CacheRefreshService {
  3. @Autowired
  4. private CacheManager cacheManager;
  5. @Scheduled(cron = ...)
  6. public void refreshCache() {
  7. cacheManager.getCacheNames().stream()
  8. .map(CacheManager::getCache)
  9. .filter(Objects::nonNull)
  10. .forEach(cache -> cache.clear());
  11. }
  12. }

别忘了为@configuration-s设置@enablescheduling,如果您正在运行@springbootplication,也可以将它添加到@springbootplication。

展开查看全部

相关问题