Ver código fonte

Update: rename for better readability

StrayWarrior 7 meses atrás
pai
commit
19f8bb8409

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

@@ -582,8 +582,8 @@ public class XxlJobService {
                             Collectors.toMap(ArticleTitleHisCache::getSourceId, Function.identity())));
             for (Map.Entry<String, Map<String, ArticleTitleHisCache>> typeEntry : cacheMap.entrySet()) {
                 String type = typeEntry.getKey();
-                Map<String, ArticleTitleHisCache> sourceMap = typeEntry.getValue();
-                Set<String> sourceIdList = sourceMap.keySet();
+                Map<String, ArticleTitleHisCache> sourceIdToCacheMap = typeEntry.getValue();
+                Set<String> sourceIdList = sourceIdToCacheMap.keySet();
                 List<TitleHisCacheParam> paramList = sourceIdList.stream().map(sourceId -> {
                     ArticleTitleHisCache cache = cacheMap.get(type).get(sourceId);
                     TitleHisCacheParam cacheParam = new TitleHisCacheParam();
@@ -597,11 +597,11 @@ public class XxlJobService {
                     }
                     return cacheParam;
                 }).collect(Collectors.toList());
-                Map<String, Content> hisCacheMap = recallService.getArticleTitleHisCacheMap(paramList, type);
+                Map<String, Content> articlesWithHistory = recallService.getArticleWithHistory(paramList, type);
                 for (String sourceId : sourceIdList) {
-                    Content content = hisCacheMap.get(sourceId);
+                    Content content = articlesWithHistory.get(sourceId);
                     if (Objects.nonNull(content) && CollectionUtils.isNotEmpty(content.getHisPublishArticleList())) {
-                        ArticleTitleHisCache cache = sourceMap.get(sourceId);
+                        ArticleTitleHisCache cache = sourceIdToCacheMap.get(sourceId);
                         cache.setHisPublishArticleList(JSONObject.toJSONString(content.getHisPublishArticleList()));
                         if (CollectionUtil.isNotEmpty(content.getCategory())) {
                             cache.setCategory(JSONObject.toJSONString(content.getCategory()));

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

@@ -249,10 +249,10 @@ public class RecallService implements ApplicationContextAware {
         long start = System.currentTimeMillis();
         contentList.forEach(content -> content.setTitleMd5(Md5Util.encoderByMd5(content.getTitle())));
         List<String> sourceIdList = contentList.stream().map(Content::getSourceId).distinct().collect(Collectors.toList());
-        Map<String, Content> sourceContentMap = contentList.stream().collect(
+        Map<String, Content> sourceIdToContentMap = contentList.stream().collect(
                 Collectors.toMap(Content::getSourceId, Function.identity(), (o1, o2) -> o2));
         // 根据sourceId查询数据库获取数据
-        List<ArticleTitleHisCache> articleTitleHisCacheList = new ArrayList<>();
+        List<ArticleTitleHisCache> articleTitleHisCacheList = new ArrayList<>(sourceIdList.size());
         for (List<String> partition : Lists.partition(sourceIdList, 1000)) {
             articleTitleHisCacheList.addAll(articleTitleHisCacheRepository.getBySourceIdInAndType(partition, type));
         }
@@ -265,7 +265,7 @@ public class RecallService implements ApplicationContextAware {
         Map<String, Double> accountCorrelationMap = accountCorrelationList.stream().collect(
                 Collectors.toMap(AccountCorrelation::getRelGhId, AccountCorrelation::getCorrelation));
         List<TitleHisCacheParam> paramList = sourceIdList.stream().map(sourceId -> {
-            Content content = sourceContentMap.get(sourceId);
+            Content content = sourceIdToContentMap.get(sourceId);
             TitleHisCacheParam cacheParam = new TitleHisCacheParam();
             cacheParam.setSourceId(sourceId);
             cacheParam.setTitleMd5(content.getTitleMd5());
@@ -275,9 +275,9 @@ public class RecallService implements ApplicationContextAware {
             cacheParam.setCategory(content.getCategory());
             return cacheParam;
         }).collect(Collectors.toList());
-        Map<String, Content> hisArticleCacheMap = getArticleTitleHisCacheMap(paramList, type);
-        List<Content> saveList = new ArrayList<>();
-        Set<String> sourceSet = new HashSet<>();
+        Map<String, Content> articlesWithHistory = getArticleWithHistory(paramList, type);
+        List<Content> newCacheSaveList = new ArrayList<>();
+        Set<String> newCacheSourceIdSet = new HashSet<>();
         for (Content content : contentList) {
             if (articleTitleHisCacheMap.containsKey(content.getSourceId())) {
                 ArticleTitleHisCache cache = articleTitleHisCacheMap.get(content.getSourceId());
@@ -294,25 +294,25 @@ public class RecallService implements ApplicationContextAware {
                 setT0Data(content);
                 continue;
             }
-            if (hisArticleCacheMap.containsKey(content.getSourceId())) {
-                Content cache = hisArticleCacheMap.get(content.getSourceId());
-                content.setHisPublishArticleList(cache.getHisPublishArticleList());
-                if (CollectionUtils.isNotEmpty(cache.getCategory())) {
-                    content.setCategory(cache.getCategory());
+            if (articlesWithHistory.containsKey(content.getSourceId())) {
+                Content articleWithHistory = articlesWithHistory.get(content.getSourceId());
+                content.setHisPublishArticleList(articleWithHistory.getHisPublishArticleList());
+                if (CollectionUtils.isNotEmpty(articleWithHistory.getCategory())) {
+                    content.setCategory(articleWithHistory.getCategory());
                 }
-                content.setRootPublishTimestamp(cache.getRootPublishTimestamp());
+                content.setRootPublishTimestamp(articleWithHistory.getRootPublishTimestamp());
                 for (ContentHisPublishArticle article : content.getHisPublishArticleList()) {
                     article.setCorrelation(Optional.ofNullable(accountCorrelationMap.get(article.getGhId())).orElse(0.0));
                 }
                 setT0Data(content);
             }
-            if (!sourceSet.contains(content.getSourceId())) {
-                saveList.add(content);
-                sourceSet.add(content.getSourceId());
+            if (!newCacheSourceIdSet.contains(content.getSourceId())) {
+                newCacheSaveList.add(content);
+                newCacheSourceIdSet.add(content.getSourceId());
             }
         }
         // 写入缓存
-        saveArticleTitleHisCache(saveList, type);
+        saveArticleTitleHisCache(newCacheSaveList, type);
         log.info("setTitleAvgViewCount cost:{}", System.currentTimeMillis() - start);
     }
 
@@ -347,7 +347,7 @@ public class RecallService implements ApplicationContextAware {
         }
     }
 
-    public Map<String, Content> getArticleTitleHisCacheMap(List<TitleHisCacheParam> paramList, String type) {
+    public Map<String, Content> getArticleWithHistory(List<TitleHisCacheParam> paramList, String type) {
         Map<String, Content> result = new HashMap<>();
         List<String> titleMd5List = paramList.stream().map(TitleHisCacheParam::getTitleMd5).collect(Collectors.toList());
         // 获取历史已发布文章