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

添加Redis缓存以优化召回结果复用

wangyunpeng 1 день назад
Родитель
Сommit
2b81c09496
1 измененных файлов с 44 добавлено и 2 удалено
  1. 44 2
      core/src/main/java/com/tzld/videoVector/job/ChannelDemandMatchJob.java

+ 44 - 2
core/src/main/java/com/tzld/videoVector/job/ChannelDemandMatchJob.java

@@ -13,9 +13,11 @@ import com.tzld.videoVector.model.po.pgVector.ChannelDemandMatchResultExample;
 import com.tzld.videoVector.model.vo.RecallVideoScoreVO;
 import com.tzld.videoVector.model.vo.RecallVideoScoreVO;
 import com.tzld.videoVector.service.VideoSearchService;
 import com.tzld.videoVector.service.VideoSearchService;
 import com.tzld.videoVector.util.OdpsUtil;
 import com.tzld.videoVector.util.OdpsUtil;
+import com.tzld.videoVector.util.RedisUtils;
 import com.tzld.videoVector.util.VectorUtils;
 import com.tzld.videoVector.util.VectorUtils;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import com.xxl.job.core.handler.annotation.XxlJob;
+import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
@@ -55,6 +57,19 @@ public class ChannelDemandMatchJob {
     @Resource
     @Resource
     private VideoSearchService videoSearchService;
     private VideoSearchService videoSearchService;
 
 
+    @Resource
+    private RedisUtils redisUtils;
+
+    /**
+     * 召回结果Redis缓存前缀
+     */
+    private static final String RECALL_CACHE_PREFIX = "channel_demand:recall:";
+
+    /**
+     * 召回结果缓存过期时间(秒),默认2小时
+     */
+    private static final long RECALL_CACHE_EXPIRE = 2 * 60 * 60;
+
     /**
     /**
      * 需求匹配并发线程数(单个渠道内部的需求匹配)
      * 需求匹配并发线程数(单个渠道内部的需求匹配)
      */
      */
@@ -353,7 +368,7 @@ public class ChannelDemandMatchJob {
     }
     }
 
 
     /**
     /**
-     * 执行单次召回并构建结果行
+     * 执行单次召回并构建结果行(支持Redis缓存,相同参数复用匹配结果)
      */
      */
     private List<ChannelDemandMatchResult> doRecall(ChannelDemandMatchResult demand, String queryText, String configCode, int topN) {
     private List<ChannelDemandMatchResult> doRecall(ChannelDemandMatchResult demand, String queryText, String configCode, int topN) {
         List<ChannelDemandMatchResult> batchRows = new ArrayList<>();
         List<ChannelDemandMatchResult> batchRows = new ArrayList<>();
@@ -363,7 +378,7 @@ public class ChannelDemandMatchJob {
         param.setConfigCode(configCode);
         param.setConfigCode(configCode);
         param.setTopN(topN);
         param.setTopN(topN);
 
 
-        RecallVideoScoreVO scoreVO = videoSearchService.recallWithScore(param);
+        RecallVideoScoreVO scoreVO = getRecallResultWithCache(param);
         if (scoreVO == null || CollectionUtils.isEmpty(scoreVO.getItems())) {
         if (scoreVO == null || CollectionUtils.isEmpty(scoreVO.getItems())) {
             return batchRows;
             return batchRows;
         }
         }
@@ -390,6 +405,33 @@ public class ChannelDemandMatchJob {
         return batchRows;
         return batchRows;
     }
     }
 
 
+    /**
+     * 带Redis缓存的召回:相同queryText+configCode+topN直接复用缓存结果
+     */
+    private RecallVideoScoreVO getRecallResultWithCache(RecallVideoScoreParam param) {
+        String cacheKey = RECALL_CACHE_PREFIX + Md5Util.encoderByMd5(
+                param.getQueryText() + "|" + param.getConfigCode() + "|" + param.getTopN());
+        try {
+            String cached = redisUtils.get(cacheKey);
+            if (cached != null) {
+                return JSON.parseObject(cached, RecallVideoScoreVO.class);
+            }
+        } catch (Exception e) {
+            log.warn("读取召回缓存失败, key={}: {}", cacheKey, e.getMessage());
+        }
+
+        RecallVideoScoreVO scoreVO = videoSearchService.recallWithScore(param);
+
+        if (scoreVO != null && !CollectionUtils.isEmpty(scoreVO.getItems())) {
+            try {
+                redisUtils.set(cacheKey, JSON.toJSONString(scoreVO), RECALL_CACHE_EXPIRE);
+            } catch (Exception e) {
+                log.warn("写入召回缓存失败, key={}: {}", cacheKey, e.getMessage());
+            }
+        }
+        return scoreVO;
+    }
+
     /**
     /**
      * 生成确定性实验ID
      * 生成确定性实验ID
      * 格式: 渠道_线上动作_需求策略_特征点类型_视频ID_configCode_短哈希
      * 格式: 渠道_线上动作_需求策略_特征点类型_视频ID_configCode_短哈希