浏览代码

召回内容历史表现增加缓存

wangyunpeng 7 月之前
父节点
当前提交
ccd9536556

+ 3 - 2
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/XxlJobService.java

@@ -43,6 +43,7 @@ import lombok.Getter;
 import lombok.Setter;
 import lombok.experimental.Accessors;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
@@ -586,8 +587,8 @@ public class XxlJobService {
             Map<String, Content> hisCacheMap = recallService.getArticleTitleHisCacheMap(paramList, type);
             for (String titleMd5 : titleMd5List) {
                 Content content = hisCacheMap.get(titleMd5);
-                ArticleTitleHisCache cache = titleMap.get(titleMd5);
-                if (content != null) {
+                if (Objects.nonNull(content) && CollectionUtils.isNotEmpty(content.getHisPublishArticleList())) {
+                    ArticleTitleHisCache cache = titleMap.get(titleMd5);
                     cache.setHisPublishArticleList(JSONObject.toJSONString(content.getHisPublishArticleList()));
                     cache.setUpdateTimestamp(System.currentTimeMillis());
                     articleTitleHisCacheRepository.save(cache);

+ 5 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/recall/RecallService.java

@@ -324,6 +324,7 @@ public class RecallService implements ApplicationContextAware {
         }).collect(Collectors.toList());
         Map<String, Content> hisArticleCacheMap = getArticleTitleHisCacheMap(paramList, type);
         List<Content> saveList = new ArrayList<>();
+        Set<String> titleMd5Set = new HashSet<>();
         for (Content content : contentList) {
             if (articleTitleHisCacheMap.containsKey(content.getTitleMd5())) {
                 ArticleTitleHisCache cache = articleTitleHisCacheMap.get(content.getTitleMd5());
@@ -344,7 +345,10 @@ public class RecallService implements ApplicationContextAware {
                 }
                 setT0Data(content);
             }
-            saveList.add(content);
+            if (!titleMd5Set.contains(content.getTitleMd5())) {
+                saveList.add(content);
+                titleMd5Set.add(content.getTitleMd5());
+            }
         }
         // 写入缓存
         saveArticleTitleHisCache(saveList, type);