Prechádzať zdrojové kódy

feat:Tab添加过滤逻辑

zhaohaipeng 1 týždeň pred
rodič
commit
71a1a463be

+ 5 - 1
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/RecommendService.java

@@ -37,6 +37,7 @@ import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
 import com.tzld.piaoquan.recommend.server.service.recall.RecallResult;
 import com.tzld.piaoquan.recommend.server.service.recall.RecallService;
 import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
+import com.tzld.piaoquan.recommend.server.service.tab.TabService;
 import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
 import com.tzld.piaoquan.recommend.server.util.FeatureUtils;
 import com.tzld.piaoquan.recommend.server.util.JSONUtils;
@@ -93,7 +94,8 @@ public class RecommendService {
     private FunnelLogService funnelLogService;
     @Autowired
     private FeatureService featureService;
-
+    @Autowired
+    private TabService tabService;
     @Autowired
     private GrowthManagerFeign growthManagerFeign;
 
@@ -800,6 +802,8 @@ public class RecommendService {
         updateFlowPoolCache(request, param, videos);
 
         updateDouHotVideoCache(request, param, videos);
+
+        tabService.updateUserTabCache(param, videos);
     }
 
     private void updateFlowPoolCache(RecommendRequest request, RecommendParam param,

+ 31 - 3
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/tab/TabService.java

@@ -9,7 +9,9 @@ import com.tzld.piaoquan.recommend.server.gen.common.Result;
 import com.tzld.piaoquan.recommend.server.gen.common.TabProto;
 import com.tzld.piaoquan.recommend.server.gen.tab.TabRequest;
 import com.tzld.piaoquan.recommend.server.gen.tab.TabResponse;
+import com.tzld.piaoquan.recommend.server.model.RecommendParam;
 import com.tzld.piaoquan.recommend.server.model.TabParam;
+import com.tzld.piaoquan.recommend.server.model.Video;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -51,7 +53,9 @@ public class TabService {
             "71", "81", "82"
     );
 
-    private static final String redisKeyPrefox = "personalized_tab_candidate:";
+    private static final String personalizationTabCondidateFormat = "personalized_tab_candidate:%s";
+
+    private static final String userInvalidTabCache = "user:invalid:tab:cache:%s";
 
     private static final String PERSONALIZATION_TAB_SEPARATOR = "&";
 
@@ -88,12 +92,13 @@ public class TabService {
             return tabProtos;
         }
 
-        String key = redisKeyPrefox + tabParam.getUnionId();
+        String key = String.format(personalizationTabCondidateFormat, tabParam.getUnionId());
         String value = redisTemplate.opsForValue().get(key);
         if (StringUtils.isBlank(value)) {
             log.error("unionId is not exist");
             return tabProtos;
         }
+
         List<List<String>> tabCollect = Arrays.stream(value.split("\t"))
                 .map(i -> Arrays.asList(i.split(PERSONALIZATION_TAB_SEPARATOR)))
                 .collect(Collectors.toList());
@@ -114,7 +119,14 @@ public class TabService {
         tabProtos.addAll(0, returnCate2Tab);
         tabProtos.addAll(0, shareCate2Tab);
         tabProtos.addAll(0, profileTab);
-        return tabProtos.subList(0, Math.min(tabProtos.size(), personalizationTabSize));
+
+        String invalidTabCacheKey = String.format(userInvalidTabCache, tabParam.getUnionId());
+        Set<String> invalidTabCache = Optional.ofNullable(redisTemplate.opsForSet().members(invalidTabCacheKey)).orElse(new HashSet<>());
+
+        return tabProtos
+                .stream().filter(i -> !invalidTabCache.contains(i.getKey()))
+                .limit(Math.min(tabProtos.size(), personalizationTabSize))
+                .collect(Collectors.toList());
     }
 
     private List<TabProto> buildTabProto(List<String> tabNames, TabSource source, TabType type) {
@@ -176,4 +188,20 @@ public class TabService {
         return CHINA_PROVINCE_CODE_PREFIXES.contains(provinceCode.substring(0, 2));
     }
 
+    public void updateUserTabCache(RecommendParam param, List<Video> videos) {
+        try {
+
+            if (Objects.isNull(param.getTab()) || StringUtils.isBlank(param.getTab().getKey())) {
+                return;
+            }
+            int size = param.getSize();
+            if (CollectionUtils.isEmpty(videos) || videos.size() < size) {
+                String key = String.format(userInvalidTabCache, param.getUnionId());
+                redisTemplate.opsForSet().add(key, param.getTab().getKey());
+            }
+        } catch (Exception e) {
+            log.error("updateUserTabCache error", e);
+        }
+    }
+
 }