丁云鹏 1 year ago
parent
commit
46c2795071

+ 35 - 7
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/RecommendStatusStrategy.java

@@ -1,18 +1,24 @@
 package com.tzld.piaoquan.recommend.server.service.filter.strategy;
 
+import com.tzld.piaoquan.recommend.server.common.ThreadPoolFactory;
 import com.tzld.piaoquan.recommend.server.repository.VideoRecommendStatus;
 import com.tzld.piaoquan.recommend.server.repository.VideoRecommendStatusRepository;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
-import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.math.NumberUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
+import org.springframework.data.redis.core.RedisOperations;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.SessionCallback;
+import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -27,7 +33,7 @@ public class RecommendStatusStrategy implements FilterStrategy {
 
     private String keyFormat = "video:recommend:status:%s";
 
-    private static final int RECOMMEND_STATUS = 1;
+    private static final int RECOMMEND_STATUS = -6;
 
     @Override
     public List<Long> filter(FilterParam param) {
@@ -41,8 +47,6 @@ public class RecommendStatusStrategy implements FilterStrategy {
                 .map(id -> String.format(keyFormat, id))
                 .collect(Collectors.toList());
 
-        Set<Long> retainVideoIds = new LinkedHashSet<>();
-
         List<String> recommendStatus = redisTemplate.opsForValue().multiGet(keys);
         List<Long> cacheMissVideoIds = new ArrayList<>();
         Map<Long, Integer> recommendStatusMap = new HashMap<>();
@@ -57,12 +61,36 @@ public class RecommendStatusStrategy implements FilterStrategy {
 
         if (CollectionUtils.isNotEmpty(cacheMissVideoIds)) {
             List<VideoRecommendStatus> status = videoRecommendStatusRepository.findAllByVideoId(cacheMissVideoIds);
-            Map<Long, Integer> statusMap = CommonCollectionUtils.toMap(status, VideoRecommendStatus::getVideoId,
-                    VideoRecommendStatus::getRecommendStatus);
-            //
+            if (CollectionUtils.isNotEmpty(status)) {
+                status.stream().forEach(v -> {
+                    recommendStatusMap.put(v.getVideoId(), v.getRecommendStatus());
+
+                    // TODO 异步更新缓存
+                    ThreadPoolFactory.defaultPool().execute(() -> {
+                        redisTemplate.executePipelined(new SessionCallback<String>() {
+                            @Override
+                            public <A, B> String execute(RedisOperations<A, B> redisOperations) throws DataAccessException {
+                                ValueOperations<String, String> operations =
+                                        (ValueOperations<String, String>) redisOperations.opsForValue();
+                                status.forEach(v -> {
+                                    operations.set(String.format(keyFormat, v.getVideoId()),
+                                            String.valueOf(v.getRecommendStatus()), RandomUtils.nextInt(30, 60),
+                                            TimeUnit.SECONDS);
+                                });
+
+                                return null;
+                            }
+                        });
+                    });
+
+                });
+            }
         }
 
 
+        return param.getVideoIds().stream()
+                .filter(id -> recommendStatusMap.get(id) == RECOMMEND_STATUS)
+                .collect(Collectors.toList());
     }
 
 }

+ 14 - 9
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/ViewedStrategy.java

@@ -1,5 +1,6 @@
 package com.tzld.piaoquan.recommend.server.service.filter.strategy;
 
+import com.tzld.piaoquan.recommend.server.common.ThreadPoolFactory;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
 import com.tzld.piaoquan.recommend.server.service.filter.VideoView;
@@ -55,15 +56,19 @@ public class ViewedStrategy implements FilterStrategy {
                 for (int i = list.size() - 1; i >= 0 && limit-- > 0; i--) {
                     viewedVideoIds.add(String.valueOf(list.get(i).getVideoId()));
                 }
-                redisTemplate.executePipelined(new SessionCallback<String>() {
-                    @Override
-                    public <A, B> String execute(RedisOperations<A, B> redisOperations) throws DataAccessException {
-                        SetOperations<String, String> operations =
-                                (SetOperations<String, String>) redisOperations.opsForSet();
-                        operations.add(key, viewedVideoIds.toArray(new String[viewedVideoIds.size()]));
-                        redisTemplate.expire(key, 360 * 3600, TimeUnit.SECONDS);
-                        return null;
-                    }
+
+                // 异步写Redis
+                ThreadPoolFactory.defaultPool().execute(() -> {
+                    redisTemplate.executePipelined(new SessionCallback<String>() {
+                        @Override
+                        public <A, B> String execute(RedisOperations<A, B> redisOperations) throws DataAccessException {
+                            SetOperations<String, String> operations =
+                                    (SetOperations<String, String>) redisOperations.opsForSet();
+                            operations.add(key, viewedVideoIds.toArray(new String[viewedVideoIds.size()]));
+                            redisTemplate.expire(key, 360 * 3600, TimeUnit.SECONDS);
+                            return null;
+                        }
+                    });
                 });
             }
         }