Explorar o código

优化视频详情查询缓存机制

wangyunpeng hai 13 horas
pai
achega
4ed83466a3

+ 29 - 15
api-module/src/main/java/com/tzld/piaoquan/api/job/contentplatform/ContentPlatformDemandVideoJob.java

@@ -378,24 +378,38 @@ public class ContentPlatformDemandVideoJob {
             return ReturnT.SUCCESS;
         }
         Long now = System.currentTimeMillis();
-        for (List<ContentPlatformDemandVideo> partition : Lists.partition(activeVideos, 20)) {
-            List<Long> videoIds = partition.stream()
-                    .filter(Objects::nonNull)
-                    .map(ContentPlatformDemandVideo::getVideoId)
-                    .filter(Objects::nonNull)
+        // 单次任务执行内复用 videoId -> VideoDetail 的查询结果,避免相同 videoId 跨批次重复请求
+        Map<Long, VideoDetail> videoDetailCache = new HashMap<>();
+        Set<Long> queriedVideoIds = new HashSet<>();
+
+        // 先按全量去重 videoId 分批拉取详情写入缓存
+        List<Long> distinctVideoIds = activeVideos.stream()
+                .filter(Objects::nonNull)
+                .map(ContentPlatformDemandVideo::getVideoId)
+                .filter(Objects::nonNull)
+                .distinct()
+                .collect(Collectors.toList());
+        for (List<Long> partition : Lists.partition(distinctVideoIds, 20)) {
+            List<Long> uncachedIds = partition.stream()
+                    .filter(id -> !queriedVideoIds.contains(id))
                     .collect(Collectors.toList());
-            if (CollectionUtils.isEmpty(videoIds)) {
+            if (CollectionUtils.isEmpty(uncachedIds)) {
                 continue;
             }
-            Map<Long, VideoDetail> videoDetailMap = messageAttachmentService.getVideoDetail(new HashSet<>(videoIds));
-            for (ContentPlatformDemandVideo video : partition) {
-                if (video == null || video.getVideoId() == null) {
-                    continue;
-                }
-                VideoDetail videoDetail = videoDetailMap.get(video.getVideoId());
-                if (videoDetail == null || videoDetail.getAuditStatus() != 5) {
-                    demandVideoMapperExt.updateStatusByVideoId(video.getVideoId(), dt, 0, now);
-                }
+            Set<Long> ids = new HashSet<>(uncachedIds);
+            videoDetailCache.putAll(messageAttachmentService.getVideoDetail(ids));
+            // 无论是否命中,都标记为已查询,避免后续再次发起请求
+            queriedVideoIds.addAll(ids);
+        }
+
+        // 基于缓存判断状态并更新
+        for (ContentPlatformDemandVideo video : activeVideos) {
+            if (video == null || video.getVideoId() == null) {
+                continue;
+            }
+            VideoDetail videoDetail = videoDetailCache.get(video.getVideoId());
+            if (videoDetail == null || videoDetail.getAuditStatus() != 5) {
+                demandVideoMapperExt.updateStatusByVideoId(video.getVideoId(), dt, 0, now);
             }
         }
         log.info("checkContentPlatformDemandVideoStatusJob done, dt={}", dt);