ソースを参照

直播的开始 时间 优化

三七 1 週間 前
コミット
21b5dd3e74

+ 40 - 0
fs-service/src/main/java/com/fs/live/service/impl/LiveAutoTaskServiceImpl.java

@@ -4,6 +4,7 @@ import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.ZoneId;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson.JSON;
@@ -21,6 +22,7 @@ import com.fs.live.vo.LiveLotteryProductListVo;
 import org.checkerframework.checker.units.qual.A;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -393,6 +395,44 @@ public class LiveAutoTaskServiceImpl implements ILiveAutoTaskService {
         if (!updateList.isEmpty()) {
             baseMapper.batchUpdateLiveAutoTask(updateList);
         }
+
+        // 重新计算后,需要清理并重建Redis缓存
+        // 因为任务的触发时间(score)已经改变,旧的缓存已失效
+        try {
+            String cacheKey = "live:auto_task:" + live.getLiveId();
+
+            // 1. 删除旧的缓存
+            redisCache.deleteObject(cacheKey);
+
+            // 2. 如果直播间正在直播,重新构建缓存
+            if (live.getStatus() != null && live.getStatus() == 2) {
+                for (LiveAutoTask task : liveAutoTasks) {
+                    // 只缓存状态为启用且未完成的任务
+                    if (task.getStatus() != null && task.getStatus() == 1L
+                            && task.getFinishStatus() != null && task.getFinishStatus() == 0L) {
+                        LiveAutoTask cacheTask = new LiveAutoTask();
+                        BeanUtils.copyProperties(task, cacheTask);
+                        cacheTask.setCreateTime(null);
+                        cacheTask.setUpdateTime(null);
+
+                        redisCache.zSetAdd(cacheKey, JSON.toJSONString(cacheTask), task.getAbsValue().getTime());
+                    }
+                }
+
+                // 设置过期时间
+                if (!liveAutoTasks.isEmpty()) {
+                    redisCache.expire(cacheKey, 1, TimeUnit.DAYS);
+                }
+
+                log.info("直播间开始时间变化,已重新构建自动化任务缓存: liveId={}, 任务数={}",
+                        live.getLiveId(), liveAutoTasks.size());
+            } else {
+                log.info("直播间未开播,已清理自动化任务缓存: liveId={}", live.getLiveId());
+            }
+        } catch (Exception e) {
+            log.error("重新构建自动化任务缓存失败: liveId={}, error={}",
+                    live.getLiveId(), e.getMessage(), e);
+        }
     }
 
     @Override

+ 8 - 7
fs-service/src/main/java/com/fs/live/service/impl/LiveServiceImpl.java

@@ -1183,13 +1183,14 @@ public class LiveServiceImpl implements ILiveService
         baseMapper.updateLive(exist);
         // 清除缓存
         clearLiveCache(live.getLiveId());
-        List<LiveAutoTask> liveAutoTasks = liveAutoTaskService.selectNoActivedByLiveId(exist.getLiveId(), new Date());
-        liveAutoTasks.forEach(liveAutoTask -> {
-            liveAutoTask.setCreateTime(null);
-            liveAutoTask.setUpdateTime(null);
-            redisCache.redisTemplate.opsForZSet().add("live:auto_task:" + live.getLiveId(), JSON.toJSONString(liveAutoTask),liveAutoTask.getAbsValue().getTime());
-            redisCache.redisTemplate.expire("live:auto_task:"+live.getLiveId(), 1, TimeUnit.DAYS);
-        });
+//        List<LiveAutoTask> liveAutoTasks = liveAutoTaskService.selectNoActivedByLiveId(exist.getLiveId(), new Date());
+//        liveAutoTasks.forEach(liveAutoTask -> {
+//            liveAutoTask.setCreateTime(null);
+//            liveAutoTask.setUpdateTime(null);
+//            redisCache.redisTemplate.opsForZSet().add("live:auto_task:" + live.getLiveId(), JSON.toJSONString(liveAutoTask),liveAutoTask.getAbsValue().getTime());
+//            redisCache.redisTemplate.expire("live:auto_task:"+live.getLiveId(), 1, TimeUnit.DAYS);
+//        });
+        liveAutoTaskService.recalcLiveAutoTask(exist);
         String cacheKey = String.format(LiveKeysConstant.LIVE_DATA_CACHE, live.getLiveId());
         redisCache.deleteObject(cacheKey);
         String cacheKey2 = String.format(LiveKeysConstant.LIVE_FLAG_CACHE, live.getLiveId());