Browse Source

ADD: AppletVideoStatusStrategy

sunxy 1 year ago
parent
commit
9ffce553d0

+ 1 - 1
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/repository/VideoAppTypeStatusRepository.java

@@ -10,5 +10,5 @@ import java.util.List;
  */
 @Repository
 public interface VideoAppTypeStatusRepository extends JpaRepository<VideoAppTypeStatus, Long> {
-    List<VideoAppTypeStatus> findAllByVideoIdAndAppType(Long videoId, Integer appType);
+    List<VideoAppTypeStatus> findAllByVideoIdInAndAppType(List<Long> videoIdList, Integer appType);
 }

+ 52 - 36
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/AppletVideoStatusStrategy.java

@@ -6,16 +6,16 @@ import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 /**
  * @author dyp
@@ -40,10 +40,7 @@ public class AppletVideoStatusStrategy implements FilterStrategy {
                 || CollectionUtils.isEmpty(param.getVideoIds())) {
             return Collections.emptyList();
         }
-        filterActiveStatusVideoId(param.getVideoIds(), param.getAppType());
-
-        return param.getVideoIds();
-
+        return filterActiveStatusVideoId(param.getVideoIds(), param.getAppType());
     }
 
     /**
@@ -55,41 +52,60 @@ public class AppletVideoStatusStrategy implements FilterStrategy {
      * @param idList  videoId列表
      * @param appType appType
      */
-    private void filterActiveStatusVideoId(List<Long> idList, Integer appType) {
+    private List<Long> filterActiveStatusVideoId(List<Long> idList, Integer appType) {
         if (CollectionUtils.isEmpty(idList)) {
-            return;
+            return idList;
         }
-        Iterator<Long> iterator = idList.iterator();
-        while (iterator.hasNext()) {
-            Long videoId = iterator.next();
-            if (Objects.isNull(videoId)) {
-                iterator.remove();
-                continue;
+        List<String> keys = idList.stream()
+                .map(id -> String.format(videoAppTypeStatusKeyFormat, appType, id))
+                .collect(Collectors.toList());
+        List<String> activeStatusList = videoRedisTemplate.opsForValue().multiGet(keys);
+        if (activeStatusList == null) {
+            activeStatusList = new ArrayList<>();
+        }
+
+        List<Long> cacheMissVideoIds = new ArrayList<>();
+        Map<Long, Integer> activeStatusMap = new HashMap<>();
+        for (int i = 0; i < idList.size(); i++) {
+            String value = activeStatusList.get(i);
+            if (StringUtils.isBlank(value)) {
+                cacheMissVideoIds.add(idList.get(i));
+            } else {
+                activeStatusMap.put(idList.get(i), NumberUtils.toInt(value));
             }
-            String activeStatus = videoRedisTemplate.opsForValue().get(String.format(videoAppTypeStatusKeyFormat, appType, videoId));
-            if (Objects.isNull(activeStatus)) {
-                //查库
-                List<VideoAppTypeStatus> videoAppTypeVideoStatusList =
-                        videoAppTypeStatusRepository.findAllByVideoIdAndAppType(videoId, appType);
-                if (Objects.isNull(videoAppTypeVideoStatusList) || videoAppTypeVideoStatusList.isEmpty()) {
-                    //无数据 刷1 进redis
-                    videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType, videoId), "1"
-                            , 15, TimeUnit.DAYS);
-                    activeStatus = "1";
-                } else {
-                    VideoAppTypeStatus videoAppTypeStatus = videoAppTypeVideoStatusList.get(0);
-                    if (Objects.nonNull(videoAppTypeStatus) && Objects.nonNull(videoAppTypeStatus.getVideoStatus())) {
-                        String videoStatus = videoAppTypeStatus.getVideoStatus().toString();
-                        videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType, videoId), videoStatus
-                                , 15, TimeUnit.DAYS);
-                        activeStatus = videoStatus;
-                    }
+        }
+
+        if (CollectionUtils.isNotEmpty(cacheMissVideoIds)) {
+            List<VideoAppTypeStatus> videoAppTypeStatusList =
+                    videoAppTypeStatusRepository.findAllByVideoIdInAndAppType(cacheMissVideoIds, appType);
+            Set<Long> cacheMissVideoIdSet = new HashSet<>(cacheMissVideoIds);
+            if (CollectionUtils.isNotEmpty(videoAppTypeStatusList)) {
+                // 数据库有数据
+                for (VideoAppTypeStatus videoAppTypeStatus : videoAppTypeStatusList) {
+                    activeStatusMap.put(videoAppTypeStatus.getVideoId(), videoAppTypeStatus.getVideoStatus());
+
+                    videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType, videoAppTypeStatus.getVideoId())
+                            , videoAppTypeStatus.getVideoStatus().toString(), 15, TimeUnit.DAYS);
+
+                    cacheMissVideoIdSet.remove(videoAppTypeStatus.getVideoId());
                 }
             }
-            if (Objects.equals("0", activeStatus)) {
-                iterator.remove();
+            // 数据库无数据的videoId
+            if (CollectionUtils.isNotEmpty(cacheMissVideoIdSet)) {
+                for (Long videoId : cacheMissVideoIdSet) {
+                    // 设置默认值
+                    videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType,
+                                    videoId), "1"
+                            , 15, TimeUnit.DAYS);
+                    activeStatusMap.put(videoId, 1);
+                }
             }
         }
+
+        return idList.stream()
+                .filter(id -> activeStatusMap.containsKey(id) && activeStatusMap.get(id) == 1)
+                .collect(Collectors.toList());
+
     }
 
 }