From c7375f3f9948962ccd78b2fb0d594967938f7702 Mon Sep 17 00:00:00 2001 From: Wonder Date: Thu, 23 Oct 2025 15:34:43 +0800 Subject: [PATCH] =?UTF-8?q?=20=E2=9C=A8Feat:=20=E9=80=9A=E8=BF=87=E8=A1=A5?= =?UTF-8?q?=E5=81=BF=E4=BB=BB=E5=8A=A1=E7=A1=AE=E4=BF=9Dtemp=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=8F=8A=E6=97=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hezhaohui/thumb/Application.java | 2 + .../job/SyncThumb2DBCompensatoryJob.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/cn/hezhaohui/thumb/job/SyncThumb2DBCompensatoryJob.java diff --git a/src/main/java/cn/hezhaohui/thumb/Application.java b/src/main/java/cn/hezhaohui/thumb/Application.java index 6e0f49a..6ed6737 100644 --- a/src/main/java/cn/hezhaohui/thumb/Application.java +++ b/src/main/java/cn/hezhaohui/thumb/Application.java @@ -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); diff --git a/src/main/java/cn/hezhaohui/thumb/job/SyncThumb2DBCompensatoryJob.java b/src/main/java/cn/hezhaohui/thumb/job/SyncThumb2DBCompensatoryJob.java new file mode 100644 index 0000000..188b463 --- /dev/null +++ b/src/main/java/cn/hezhaohui/thumb/job/SyncThumb2DBCompensatoryJob.java @@ -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 redisTemplate; + + @Resource + private SyncThumb2DBJob syncThumb2DBJob; + + @Scheduled(cron = "0 0 2 * * *") + public void run() { + log.info("开始补偿数据"); + Set thumbKeys = redisTemplate.keys(RedisKeyUtil.getTempThumbKey("") + "*"); + Set 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("临时数据补偿完成"); + } +}