✨Feat: 通过补偿任务确保temp数据及时处理

This commit is contained in:
2025-10-23 15:34:43 +08:00
parent 1cc3d4f5b3
commit c7375f3f99
2 changed files with 49 additions and 0 deletions
@@ -3,9 +3,11 @@ package cn.hezhaohui.thumb;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@MapperScan("cn.hezhaohui.thumb.mapper")
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
@@ -0,0 +1,47 @@
package cn.hezhaohui.thumb.job;
import cn.hezhaohui.thumb.constant.ThumbConstant;
import cn.hezhaohui.thumb.util.RedisKeyUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.Set;
/**
* 定时将 Redis 中的临时点赞数据同步到数据库的补偿措施
*
*/
@Component
@Slf4j
public class SyncThumb2DBCompensatoryJob {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private SyncThumb2DBJob syncThumb2DBJob;
@Scheduled(cron = "0 0 2 * * *")
public void run() {
log.info("开始补偿数据");
Set<String> thumbKeys = redisTemplate.keys(RedisKeyUtil.getTempThumbKey("") + "*");
Set<String> needHandleDataSet = new HashSet<>();
thumbKeys.stream().filter(ObjUtil::isNotNull).forEach(thumbKey -> needHandleDataSet.add(thumbKey.replace(ThumbConstant.TEMP_THUMB_KEY_PREFIX.formatted(""), "")));
if (CollUtil.isEmpty(needHandleDataSet)) {
log.info("没有需要补偿的临时数据");
return;
}
// 补偿数据
for (String date : needHandleDataSet) {
syncThumb2DBJob.syncThumb2DBByDate(date);
}
log.info("临时数据补偿完成");
}
}