大统计redis优化

master
382696293@qq.com 2 years ago
parent 1283dffb9a
commit 90f277b303

@ -157,6 +157,8 @@ public class WxClockLogServiceImpl implements IWxClockLogService {
/* 保存redis构建排名 */
String redisKey = CLOCK_RANK_REDIS_KEY + LocalDate.now().getYear() + LocalDate.now().getMonthValue();
redisTemplate.opsForZSet().incrementScore(redisKey, wxUserMember.getId(), 1);
redisTemplate.expire(redisKey, LocalDate.now().getDayOfMonth() * 2, TimeUnit.DAYS);
// 保存打卡记录,不计算比例
List<Long> userIdList = Arrays.asList(wxUserMember.getId());
List<WxClockStatistics> wxClockStatisticsList = wxClockStatisticsMapper.selectByUserIdAndYearMonth(userIdList, LocalDate.now().getYear(), LocalDate.now().getMonthValue());
@ -316,15 +318,20 @@ public class WxClockLogServiceImpl implements IWxClockLogService {
year = localDate.getYear();
month = localDate.getMonthValue();
}
String redisKey = CLOCK_RANK_REDIS_KEY + year + month;
// 清理旧数据
redisTemplate.delete(redisKey);
// 添加假数据,来创建 redisKey并设置有效期
redisTemplate.opsForZSet().incrementScore(redisKey, 0, 1);
redisTemplate.expire(redisKey, LocalDate.now().getDayOfMonth() * 2, TimeUnit.DAYS);
// 重置新数据
List<WxClockStatistics> wxClockStatisticsList = wxClockStatisticsMapper.selectByUserIdAndYearMonth(userIdList, year, month);
if (wxClockStatisticsList != null && wxClockStatisticsList.size() > 0) {
String redisKey = CLOCK_RANK_REDIS_KEY + year + month;
for (WxClockStatistics wxClockStatistics : wxClockStatisticsList) {
redisTemplate.opsForZSet().add(redisKey, wxClockStatistics.getUserId(), wxClockStatistics.getClockNum());
}
}
if (wxClockStatisticsList != null && wxClockStatisticsList.size() > 0) {
String redisKey = CLOCK_RANK_REDIS_KEY + year + month;
WxClockStatistics update;
for (WxClockStatistics wxClockStatistics : wxClockStatisticsList) {
// 获取排名
@ -336,12 +343,10 @@ public class WxClockLogServiceImpl implements IWxClockLogService {
update = new WxClockStatistics();
update.setPercentage(percentage);
update.setId(wxClockStatistics.getId());
// update.setUpdateBy(SecurityUtils.getUsername());
update.setUpdateTime(DateUtils.getNowDate());
wxClockStatisticsMapper.updateWxClockStatistics(update);
}
}
redisTemplate.delete(CLOCK_RANK_REDIS_KEY + year + month);
}

Loading…
Cancel
Save