From 4d725763a6f775b6acb6bb54487dd9771344eefd Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Wed, 4 Mar 2026 16:12:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=BF=9B=E5=BA=A6=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eredis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../task/RoomDailyTaskProgressUpdateExe.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/task/RoomDailyTaskProgressUpdateExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/task/RoomDailyTaskProgressUpdateExe.java index 59df96af..578e57e9 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/task/RoomDailyTaskProgressUpdateExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/task/RoomDailyTaskProgressUpdateExe.java @@ -1,5 +1,7 @@ package com.red.circle.other.app.command.task; +import com.fasterxml.jackson.core.type.TypeReference; +import com.red.circle.component.redis.service.RedisService; import com.red.circle.other.app.dto.cmd.task.RoomDailyTaskProgressUpdateCmd; import com.red.circle.other.infra.database.rds.entity.task.RoomDailyTaskConfig; import com.red.circle.other.infra.database.rds.entity.task.RoomDailyTaskProgress; @@ -15,6 +17,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Collections; import java.util.List; +import java.util.concurrent.TimeUnit; /** * 更新每日任务进度 @@ -24,7 +27,12 @@ import java.util.List; @RequiredArgsConstructor public class RoomDailyTaskProgressUpdateExe { + private static final String CACHE_KEY_CONFIG = "room:daily:task:config"; + private static final String CACHE_KEY_TIERS = "room:daily:task:tiers"; + private static final long CACHE_TTL_HOURS = 24; + private final RoomDailyTaskDatabaseService taskDatabaseService; + private final RedisService redisService; @Transactional(rollbackFor = Exception.class) public void execute(RoomDailyTaskProgressUpdateCmd cmd) { @@ -32,13 +40,13 @@ public class RoomDailyTaskProgressUpdateExe { String taskCode = cmd.getTaskCode(); Integer progressValue = cmd.getProgressValue(); - RoomDailyTaskConfig config = taskDatabaseService.getConfigByCode(taskCode); + RoomDailyTaskConfig config = getConfigFromCache(taskCode); if (config == null) { log.warn("任务配置不存在或已下线, taskCode={}", taskCode); return; } - List tiers = taskDatabaseService.listTiersByTaskCodes(Collections.singletonList(taskCode)); + List tiers = getTiersFromCache(taskCode); if (tiers.isEmpty()) { log.warn("任务档位未配置, taskCode={}", taskCode); return; @@ -60,7 +68,6 @@ public class RoomDailyTaskProgressUpdateExe { progress.setCreateTime(LocalDateTime.now()); } - // 所有档位均已领取,不再更新 if (progress.getMaxTierClaimed() >= tiers.size()) { log.info("所有档位已领取,无需更新进度, userId={}, taskCode={}", userId, taskCode); return; @@ -68,7 +75,6 @@ public class RoomDailyTaskProgressUpdateExe { progress.setCurrentValue(progressValue); - // 计算当前达到的最高档位 int maxTierCompleted = 0; for (RoomDailyTaskTier tier : tiers) { if (progressValue >= tier.getTargetValue()) { @@ -82,4 +88,28 @@ public class RoomDailyTaskProgressUpdateExe { log.info("任务进度更新成功, userId={}, taskCode={}, progressValue={}, maxTierCompleted={}", userId, taskCode, progressValue, maxTierCompleted); } + + private RoomDailyTaskConfig getConfigFromCache(String taskCode) { + RoomDailyTaskConfig config = redisService.hashGet(CACHE_KEY_CONFIG, taskCode, RoomDailyTaskConfig.class); + if (config != null) { + return config; + } + config = taskDatabaseService.getConfigByCode(taskCode); + if (config != null) { + redisService.hashPut(CACHE_KEY_CONFIG, taskCode, config, CACHE_TTL_HOURS, TimeUnit.HOURS); + } + return config; + } + + private List getTiersFromCache(String taskCode) { + List tiers = redisService.hashGet(CACHE_KEY_TIERS, taskCode, new TypeReference<>() {}); + if (tiers != null) { + return tiers; + } + tiers = taskDatabaseService.listTiersByTaskCodes(Collections.singletonList(taskCode)); + if (!tiers.isEmpty()) { + redisService.hashPut(CACHE_KEY_TIERS, taskCode, tiers, CACHE_TTL_HOURS, TimeUnit.HOURS); + } + return tiers; + } }