Просмотр исходного кода

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

wangyunpeng 22 часов назад
Родитель
Сommit
45903f9f92

+ 18 - 7
api-module/src/main/java/com/tzld/piaoquan/api/job/contentplatform/ContentPlatformDemandVideoJob.java

@@ -57,9 +57,13 @@ public class ContentPlatformDemandVideoJob {
         }
         log.info("syncContentPlatformDemandVideoJob start, dt={}", dt);
 
+        // 单次任务执行内复用 videoId -> VideoDetail 的查询结果,避免跨渠道重复请求
+        Map<Long, VideoDetail> videoDetailCache = new HashMap<>();
+        Set<Long> queriedVideoIds = new HashSet<>();
+
         for (String syncChannelName : syncChannelNames) {
             try {
-                syncByChannel(dt, syncChannelName);
+                syncByChannel(dt, syncChannelName, videoDetailCache, queriedVideoIds);
             } catch (Exception e) {
                 log.error("syncContentPlatformDemandVideoJob error, dt={}, channelName={}", dt, syncChannelName, e);
                 return ReturnT.FAIL;
@@ -122,7 +126,9 @@ public class ContentPlatformDemandVideoJob {
 
     private static final int PAGE_SIZE = 10000;
 
-    private void syncByChannel(String dt, String syncChannelName) throws Exception {
+    private void syncByChannel(String dt, String syncChannelName,
+                               Map<Long, VideoDetail> videoDetailCache,
+                               Set<Long> queriedVideoIds) throws Exception {
         log.info("syncByChannel start, dt={}, channelName={}", dt, syncChannelName);
 
         Long now = System.currentTimeMillis();
@@ -262,18 +268,23 @@ public class ContentPlatformDemandVideoJob {
             return;
         }
 
-        // 获取视频详情(标题、封面、视频URL)
+        // 获取视频详情(标题、封面、视频URL):仅请求当前缓存中尚未查询过的 videoId
         List<Long> videoIds = saveList.stream().map(ContentPlatformDemandVideo::getVideoId)
+                .filter(Objects::nonNull)
                 .distinct().collect(Collectors.toList());
-        Map<Long, VideoDetail> videoDetailMap = new HashMap<>();
-        for (List<Long> partition : Lists.partition(videoIds, 20)) {
+        List<Long> uncachedVideoIds = videoIds.stream()
+                .filter(id -> !queriedVideoIds.contains(id))
+                .collect(Collectors.toList());
+        for (List<Long> partition : Lists.partition(uncachedVideoIds, 20)) {
             Set<Long> ids = new HashSet<>(partition);
-            videoDetailMap.putAll(messageAttachmentService.getVideoDetail(ids));
+            videoDetailCache.putAll(messageAttachmentService.getVideoDetail(ids));
+            // 无论是否命中,都标记为已查询,避免后续渠道再次发起请求
+            queriedVideoIds.addAll(ids);
         }
 
         // 填充视频详情
         for (ContentPlatformDemandVideo demandVideo : saveList) {
-            VideoDetail detail = videoDetailMap.get(demandVideo.getVideoId());
+            VideoDetail detail = videoDetailCache.get(demandVideo.getVideoId());
             if (Objects.nonNull(detail)) {
                 demandVideo.setTitle(detail.getTitle());
                 String cover = detail.getCover();