Browse Source

过滤耗时超过100s 则删除redis缓存 全量过滤

wangyunpeng 1 day ago
parent
commit
3a8d9d5abe

+ 12 - 3
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/filter/FilterService.java

@@ -22,6 +22,7 @@ import java.util.*;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 import static com.tzld.longarticle.recommend.server.common.constant.SceneConstants.FWH_COLD_START;
@@ -46,11 +47,11 @@ public class FilterService {
         FilterResult result = new FilterResult();
 
         List<FilterStrategy> strategies = getAllStrategies(param);
+        String dateStr = DateUtils.getCurrentDateStr("yyyyMMdd");
+        String redisKey = "ContentPreFilterJob:" + dateStr + ":" + param.getPlanId() + "-" + param.getAccountId();
 
         // 已同步待发布内容 预过滤
         if (!preFilter) {
-            String dateStr = DateUtils.getCurrentDateStr("yyyyMMdd");
-            String redisKey = "ContentPreFilterJob:" + dateStr + ":" + param.getPlanId() + "-" + param.getAccountId();
             String value = redisTemplate.opsForValue().get(redisKey);
             if (StringUtils.hasText(value)) {
                 strategies = getUnPreFilterStrategies(param);
@@ -116,7 +117,15 @@ public class FilterService {
         }
         result.setContentIds(contentIds);
         result.setFilterContent(filterContents);
-        log.info("filter cost:{}", System.currentTimeMillis() - start);
+        Long cost = System.currentTimeMillis() - start;
+        log.info("filter cost:{}", cost);
+        // 过滤耗时超过100s 则删除redis缓存 全量过滤
+        if (cost > 100000) {
+            log.warn("filter cost is too long:{}", cost);
+            redisTemplate.delete(redisKey);
+        } else {
+            redisTemplate.opsForValue().set(redisKey, "1", 1, TimeUnit.DAYS);
+        }
         return result;
     }