wangyunpeng před 1 týdnem
rodič
revize
6b6dd501ec

+ 20 - 10
core/src/main/java/com/tzld/videoVector/service/impl/VideoSearchServiceImpl.java

@@ -783,7 +783,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
     }
 
     /**
-     * 从 Redis 获取视频基础信息并填充到匹配结果中
+     * 从 Redis 批量获取视频基础信息并填充到匹配结果中
      * 数据来源:syncVideoDetailJob 定时任务从大数据表同步到 Redis
      */
     private void enrichVideoDetail(List<VideoMatchResult> results) {
@@ -791,17 +791,27 @@ public class VideoSearchServiceImpl implements VideoSearchService {
             return;
         }
 
-        for (VideoMatchResult matchResult : results) {
-            try {
-                String redisKey = VectorConstants.VIDEO_DETAIL_KEY_PREFIX + matchResult.getVideoId();
-                String detailJson = redisUtils.get(redisKey);
-                if (detailJson != null) {
-                    Map<String, Object> detail = JSONObject.parseObject(detailJson, Map.class);
-                    matchResult.setVideoDetail(detail);
+        try {
+            // 构建 Redis keys 列表,使用 mGet 批量获取
+            List<String> keys = results.stream()
+                    .map(r -> VectorConstants.VIDEO_DETAIL_KEY_PREFIX + r.getVideoId())
+                    .collect(Collectors.toList());
+
+            List<String> values = redisUtils.mGet(keys);
+
+            for (int i = 0; i < results.size(); i++) {
+                try {
+                    String detailJson = values.get(i);
+                    if (detailJson != null) {
+                        Map<String, Object> detail = JSONObject.parseObject(detailJson, Map.class);
+                        results.get(i).setVideoDetail(detail);
+                    }
+                } catch (Exception e) {
+                    log.error("解析视频详情失败,videoId={}: {}", results.get(i).getVideoId(), e.getMessage());
                 }
-            } catch (Exception e) {
-                log.error("获取视频详情失败,videoId={}: {}", matchResult.getVideoId(), e.getMessage());
             }
+        } catch (Exception e) {
+            log.error("批量获取视频详情失败: {}", e.getMessage(), e);
         }
     }
 }