|
@@ -1,16 +1,22 @@
|
|
|
package com.tzld.piaoquan.recommend.server.service.filter.strategy;
|
|
|
|
|
|
+import com.tzld.piaoquan.recommend.server.common.ThreadPoolFactory;
|
|
|
import com.tzld.piaoquan.recommend.server.repository.VideoAppTypeStatus;
|
|
|
import com.tzld.piaoquan.recommend.server.repository.VideoAppTypeStatusRepository;
|
|
|
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.RandomUtils;
|
|
|
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.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.*;
|
|
@@ -65,6 +71,7 @@ public class AppletVideoStatusStrategy implements FilterStrategy {
|
|
|
}
|
|
|
|
|
|
List<Long> cacheMissVideoIds = new ArrayList<>();
|
|
|
+ Map<String, String> updateRedisStatus = new HashMap<>();
|
|
|
Map<Long, Integer> activeStatusMap = new HashMap<>();
|
|
|
for (int i = 0; i < idList.size(); i++) {
|
|
|
String value = activeStatusList.get(i);
|
|
@@ -76,17 +83,13 @@ public class AppletVideoStatusStrategy implements FilterStrategy {
|
|
|
}
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(cacheMissVideoIds)) {
|
|
|
- List<VideoAppTypeStatus> videoAppTypeStatusList =
|
|
|
- videoAppTypeStatusRepository.findAllByVideoIdInAndAppType(cacheMissVideoIds, appType);
|
|
|
+ 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);
|
|
|
-
|
|
|
+ updateRedisStatus.put(String.format(videoAppTypeStatusKeyFormat, appType, videoAppTypeStatus.getVideoId()), "1");
|
|
|
cacheMissVideoIdSet.remove(videoAppTypeStatus.getVideoId());
|
|
|
}
|
|
|
}
|
|
@@ -94,14 +97,23 @@ public class AppletVideoStatusStrategy implements FilterStrategy {
|
|
|
if (CollectionUtils.isNotEmpty(cacheMissVideoIdSet)) {
|
|
|
for (Long videoId : cacheMissVideoIdSet) {
|
|
|
// 设置默认值
|
|
|
- videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType,
|
|
|
- videoId), "1"
|
|
|
- , 15, TimeUnit.DAYS);
|
|
|
+ updateRedisStatus.put(String.format(videoAppTypeStatusKeyFormat, appType, videoId), "1");
|
|
|
activeStatusMap.put(videoId, 1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 异步更新缓存
|
|
|
+ ThreadPoolFactory.defaultPool().execute(() -> videoRedisTemplate.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();
|
|
|
+ updateRedisStatus.forEach((key, value) -> operations.set(key, value, RandomUtils.nextInt(10, 15),
|
|
|
+ TimeUnit.DAYS));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }));
|
|
|
+
|
|
|
return idList.stream()
|
|
|
.filter(id -> activeStatusMap.containsKey(id) && activeStatusMap.get(id) == 1)
|
|
|
.collect(Collectors.toList());
|