共计 989 个字符,预计需要花费 3 分钟才能阅读完成。
应用 LoadingCache 缓存每天数据库第一条数据并保留
private LoadingCache<String, Integer> minId = CacheBuilder.newBuilder().expireAfterWrite(1L, TimeUnit.DAYS).build(new CacheLoader<String, Integer>() {
@Override
public Integer load(String mixdate) throws Exception {Date date = LocalDate.parse(StringUtils.substringAfter(s, "@")).toDate();
// 在本地没有缓存的时候会调用该办法进行加载,将获取的值进行缓存并返回后果
if (ACTIVE_COUNTER.startsWith(mixdate)) {LoginLog loginLog = loginLogRepository.getTopByLoginTimeBeforeOrderByIdDesc(date);
if (loginLog != null) {return loginLog.getId();
}
} else if (PLAYED_COUNTER.startsWith(mixdate)) {ViewHistory viewHistory = viewHistoryRepository.getTopByViewtimeBeforeOrderByIdDesc(date);
if (viewHistory != null) {return viewHistory.getId();
}
} else if (ADCLICK_COUNTER.startsWith(mixdate)) {AdvClickHistory advClickHistory = advClickHistoryRepository.getTopByCreateTimeBeforeOrderByIdDesc(date);
if (advClickHistory != null) {return advClickHistory.getId();
}
}
return 0;
}
});
minId.getUnchecked(StringUtils.join(type, "@", date));
在这里取出当天的数据 key,因为每天 date 都不一样,所以会获取当天的第一条数据,并缓存起来!
正文完