|
|
@@ -461,27 +461,58 @@ public class Task {
|
|
|
return;
|
|
|
liveDatas.forEach(liveData ->{
|
|
|
|
|
|
- Long resultLikeCount = getAsLong(redisCache, "live:like:" + liveData.getLiveId());
|
|
|
- resultLikeCount = resultLikeCount > 0L ? resultLikeCount : liveData.getLikes();
|
|
|
- redisCache.setCacheObject("live:like:" + liveData.getLiveId(), resultLikeCount.intValue());
|
|
|
- liveData.setLikes(
|
|
|
- resultLikeCount
|
|
|
- );
|
|
|
-
|
|
|
- /* for (Long liveId : liveIds) {
|
|
|
- LiveData liveData = liveDataService.selectLiveDataByLiveId(liveId);
|
|
|
- if (liveData == null) {
|
|
|
- continue; // 防止空指针异常
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- // 从 redis 获取数据,并提供默认值,避免 NPE
|
|
|
- liveData.setPageViews(
|
|
|
- Math.max( liveData.getPageViews(), Optional.ofNullable(redisCache.incr(PAGE_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L))
|
|
|
- );
|
|
|
- liveData.setTotalViews(
|
|
|
- Math.max( liveData.getTotalViews(), Optional.ofNullable(redisCache.incr(TOTAL_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L))
|
|
|
- );
|
|
|
+ Map<String, Integer> flagMap = liveWatchUserService.getLiveFlagWithCache(liveData.getLiveId());
|
|
|
+ Integer liveFlag = flagMap.get("liveFlag");
|
|
|
+
|
|
|
+ // 判断是直播还是回放
|
|
|
+ if (liveFlag != null && liveFlag == 1) {
|
|
|
+ // 直播:更新 likes 和 totalViews
|
|
|
+ Long resultLikeCount = getAsLong(redisCache, "live:like:" + liveData.getLiveId());
|
|
|
+ resultLikeCount = resultLikeCount > 0L ? resultLikeCount : liveData.getLikes();
|
|
|
+ redisCache.setCacheObject("live:like:" + liveData.getLiveId(), resultLikeCount.intValue());
|
|
|
+ liveData.setLikes(resultLikeCount);
|
|
|
+
|
|
|
+ // 从 redis 获取数据,并提供默认值,避免 NPE
|
|
|
+ liveData.setPageViews(
|
|
|
+ Math.max( liveData.getPageViews(), Optional.ofNullable(redisCache.incr(PAGE_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L))
|
|
|
+ );
|
|
|
+ liveData.setTotalViews(
|
|
|
+ Math.max( liveData.getTotalViews(), Optional.ofNullable(redisCache.incr(TOTAL_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L))
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ // 回放:使用 Redis 中的数据减去直播的数据,得到回放的数据
|
|
|
+ String likeKey = "live:like:" + liveData.getLiveId();
|
|
|
+ String totalViewsKey = TOTAL_VIEWS_KEY + liveData.getLiveId();
|
|
|
+
|
|
|
+ // 从 Redis 获取总数据(直播+回放)
|
|
|
+ Long totalLikeCount = getAsLong(redisCache, likeKey);
|
|
|
+ Long totalViewCount = getAsLong(redisCache, totalViewsKey);
|
|
|
+
|
|
|
+ // 获取数据库中直播的数据
|
|
|
+ Long liveLikeCount = liveData.getLikes() != null ? liveData.getLikes() : 0L;
|
|
|
+ Long liveViewCount = liveData.getTotalViews() != null ? liveData.getTotalViews() : 0L;
|
|
|
+
|
|
|
+ // 回放数据 = Redis总数据 - 直播数据
|
|
|
+ Long replayLikeNum = totalLikeCount - liveLikeCount;
|
|
|
+ Long replayViewNum = totalViewCount - liveViewCount;
|
|
|
+
|
|
|
+ // 确保回放数据不为负数
|
|
|
+ if (replayLikeNum < 0L) {
|
|
|
+ replayLikeNum = 0L;
|
|
|
+ }
|
|
|
+ if (replayViewNum < 0L) {
|
|
|
+ replayViewNum = 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新回放数据
|
|
|
+ liveData.setReplayLikeNum(replayLikeNum);
|
|
|
+ liveData.setReplayViewNum(replayViewNum);
|
|
|
+
|
|
|
+ // 从 redis 获取数据,并提供默认值,避免 NPE
|
|
|
+ liveData.setPageViews(
|
|
|
+ Math.max( liveData.getPageViews(), Optional.ofNullable(redisCache.incr(PAGE_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L))
|
|
|
+ );
|
|
|
+ }
|
|
|
liveData.setUniqueVisitors(
|
|
|
/*Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VISITORS_KEY + liveId))
|
|
|
.map(Set::size) // 获取集合大小
|