|
@@ -185,8 +185,10 @@ public class FlowPoolService {
|
|
|
log.info("[DouHot video size]: {}", allDouHotVideo.size());
|
|
|
|
|
|
// 获取省份与视频列表的映射,省份 -> [video]
|
|
|
- Map<String, List<FlowPoolVideoInfo>> provinceAndVideoMap = this.findAllVideoAndProvinceMapByAttribute(allDouHotVideo);
|
|
|
- provinceAndVideoMap.forEach((key, value) -> log.info("[DouHot province video size]: province: {}, video size: {}", key, value.size()));
|
|
|
+ Map<String, Map<String, List<FlowPoolVideoInfo>>> levelAndProvinceAndVideoMap = this.findAllVideoAndProvinceMapByAttribute(allDouHotVideo);
|
|
|
+ for (Map.Entry<String, Map<String, List<FlowPoolVideoInfo>>> levelEntry : levelAndProvinceAndVideoMap.entrySet()) {
|
|
|
+ levelAndProvinceAndVideoMap.forEach((key, value) -> log.info("[DouHot province video size]. level: {}, province: {}, video size: {}", levelEntry.getKey(), key, value.size()));
|
|
|
+ }
|
|
|
|
|
|
// 获取某个省份下所有视频的分数, 省份 -> video -> score
|
|
|
Map<String, Map<Long, Double>> videoInProvinceScore = this.findVideoInProvinceScore(allDouHotVideo);
|
|
@@ -195,7 +197,7 @@ public class FlowPoolService {
|
|
|
Map<String, Integer> videoDistributeCount = this.findDouHotVideoDistributeCount(allDouHotVideo);
|
|
|
log.info("[DouHot view distribute count size]: {}", videoDistributeCount.size());
|
|
|
|
|
|
- this.douHotVideoWriteRedis(provinceAndVideoMap, videoInProvinceScore, videoDistributeCount);
|
|
|
+ this.douHotVideoWriteRedis(levelAndProvinceAndVideoMap, videoInProvinceScore, videoDistributeCount);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -346,15 +348,18 @@ public class FlowPoolService {
|
|
|
}
|
|
|
|
|
|
// 根据属性获取流量池视频和省份的对应关系
|
|
|
- private Map<String, List<FlowPoolVideoInfo>> findAllVideoAndProvinceMapByAttribute(List<FlowPoolVideoInfo> allDouHotVideo) {
|
|
|
- Map<String, List<FlowPoolVideoInfo>> resultMap = new HashMap<>(allDouHotVideo.size());
|
|
|
+ private Map<String, Map<String, List<FlowPoolVideoInfo>>> findAllVideoAndProvinceMapByAttribute(List<FlowPoolVideoInfo> allDouHotVideo) {
|
|
|
+ Map<String, Map<String, List<FlowPoolVideoInfo>>> resultMap = new HashMap<>(16);
|
|
|
|
|
|
for (FlowPoolVideoInfo flowPoolVideoInfo : allDouHotVideo) {
|
|
|
if (MapUtils.isEmpty(flowPoolVideoInfo.getAttribute())) {
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
+ Map<String, List<FlowPoolVideoInfo>> provinceMap = resultMap.computeIfAbsent(String.valueOf(flowPoolVideoInfo.getLevel()), l -> new HashMap<>());
|
|
|
+
|
|
|
String province = flowPoolVideoInfo.getAttribute().getString("province");
|
|
|
- resultMap.computeIfAbsent(province, s -> new ArrayList<>()).add(flowPoolVideoInfo);
|
|
|
+ provinceMap.computeIfAbsent(province, s -> new ArrayList<>()).add(flowPoolVideoInfo);
|
|
|
}
|
|
|
|
|
|
return resultMap;
|
|
@@ -403,43 +408,50 @@ public class FlowPoolService {
|
|
|
return distributeCountMap;
|
|
|
}
|
|
|
|
|
|
- private void douHotVideoWriteRedis(Map<String, List<FlowPoolVideoInfo>> provinceAndVideoMap, Map<String, Map<Long, Double>> provinceAllVideoScore, Map<String, Integer> videoDistributeCount) {
|
|
|
- if (MapUtils.isEmpty(provinceAllVideoScore) || MapUtils.isEmpty(videoDistributeCount)) {
|
|
|
+ private void douHotVideoWriteRedis(Map<String, Map<String, List<FlowPoolVideoInfo>>> levelAndProvinceAndVideoMap, Map<String, Map<Long, Double>> provinceAllVideoScore, Map<String, Integer> videoDistributeCount) {
|
|
|
+ if (MapUtils.isEmpty(levelAndProvinceAndVideoMap) || MapUtils.isEmpty(videoDistributeCount)) {
|
|
|
return;
|
|
|
}
|
|
|
if (MapUtils.isEmpty(provinceAllVideoScore)) {
|
|
|
provinceAllVideoScore = Maps.newHashMap();
|
|
|
}
|
|
|
- for (Map.Entry<String, List<FlowPoolVideoInfo>> entry : provinceAndVideoMap.entrySet()) {
|
|
|
- String province = entry.getKey();
|
|
|
- List<FlowPoolVideoInfo> flowPoolVideoInfos = entry.getValue();
|
|
|
|
|
|
- String redisKey = String.format(RedisKeyConstants.DouHot.ITEM_REDIS_KEY_FORMAT, province, "1");
|
|
|
- log.info("[DouHot item redis key]: redisKey: {}, video size: {}", redisKey, flowPoolVideoInfos.size());
|
|
|
- redisTemplate.delete(redisKey);
|
|
|
+ for (Map.Entry<String, Map<String, List<FlowPoolVideoInfo>>> levelEntry : levelAndProvinceAndVideoMap.entrySet()) {
|
|
|
|
|
|
- Map<Long, Double> videoScoreInProvince = provinceAllVideoScore.getOrDefault(province, new HashMap<>());
|
|
|
+ String level = levelEntry.getKey();
|
|
|
+ Map<String, List<FlowPoolVideoInfo>> provinceAndVideoMap = levelEntry.getValue();
|
|
|
|
|
|
- // 将视频添加到Redis缓存中,并设置可分发数量
|
|
|
- for (FlowPoolVideoInfo flowPoolVideoInfo : flowPoolVideoInfos) {
|
|
|
- String item = String.format("%d-%s", flowPoolVideoInfo.getVideoId(), flowPoolVideoInfo.getFlowPool());
|
|
|
+ for (Map.Entry<String, List<FlowPoolVideoInfo>> entry : provinceAndVideoMap.entrySet()) {
|
|
|
+ String province = entry.getKey();
|
|
|
+ List<FlowPoolVideoInfo> flowPoolVideoInfos = entry.getValue();
|
|
|
|
|
|
- // 如果剩余的可分发数量不存在或者小于为 则不添加到Redis中
|
|
|
- if (!videoDistributeCount.containsKey(item) || videoDistributeCount.get(item) <= 0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ String redisKey = String.format(RedisKeyConstants.DouHot.ITEM_REDIS_KEY_FORMAT, province, level);
|
|
|
+ log.info("[DouHot item redis key]: redisKey: {}, video size: {}", redisKey, flowPoolVideoInfos.size());
|
|
|
+ redisTemplate.delete(redisKey);
|
|
|
|
|
|
- // 获取视频在这个省份里的分数
|
|
|
- double score = videoScoreInProvince.getOrDefault(flowPoolVideoInfo.getVideoId(), 0d);
|
|
|
+ Map<Long, Double> videoScoreInProvince = provinceAllVideoScore.getOrDefault(province, new HashMap<>());
|
|
|
|
|
|
- redisTemplate.opsForZSet().add(redisKey, item, score);
|
|
|
+ // 将视频添加到Redis缓存中,并设置可分发数量
|
|
|
+ for (FlowPoolVideoInfo flowPoolVideoInfo : flowPoolVideoInfos) {
|
|
|
+ String item = String.format("%d-%s", flowPoolVideoInfo.getVideoId(), flowPoolVideoInfo.getFlowPool());
|
|
|
|
|
|
- String distributeKey = String.format(RedisKeyConstants.DouHot.LOCAL_DISTRIBUTE_KEY_FORMAT, flowPoolVideoInfo.getVideoId(), flowPoolVideoInfo.getFlowPool());
|
|
|
- redisTemplate.opsForValue().set(distributeKey, String.valueOf(videoDistributeCount.get(item)));
|
|
|
- redisTemplate.expire(distributeKey, 24 * 60 * 60, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
+ // 如果剩余的可分发数量不存在或者小于为 则不添加到Redis中
|
|
|
+ if (!videoDistributeCount.containsKey(item) || videoDistributeCount.get(item) <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- redisTemplate.expire(redisKey, 24 * 60 * 60, TimeUnit.SECONDS);
|
|
|
+ // 获取视频在这个省份里的分数
|
|
|
+ double score = videoScoreInProvince.getOrDefault(flowPoolVideoInfo.getVideoId(), 0d);
|
|
|
+
|
|
|
+ redisTemplate.opsForZSet().add(redisKey, item, score);
|
|
|
+
|
|
|
+ String distributeKey = String.format(RedisKeyConstants.DouHot.LOCAL_DISTRIBUTE_KEY_FORMAT, flowPoolVideoInfo.getVideoId(), flowPoolVideoInfo.getFlowPool());
|
|
|
+ redisTemplate.opsForValue().set(distributeKey, String.valueOf(videoDistributeCount.get(item)));
|
|
|
+ redisTemplate.expire(distributeKey, 24 * 60 * 60, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ redisTemplate.expire(redisKey, 24 * 60 * 60, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|