Browse Source

直播间进行点赞更新,数据优化

yuhongqi 16 hours ago
parent
commit
83ac67afdd

+ 0 - 6
fs-live-socket/src/main/java/com/fs/live/controller/LiveDataController.java

@@ -42,12 +42,6 @@ public class LiveDataController extends BaseController {
     public R like(@PathVariable("liveId") Long liveId) {
         //直播间总点赞数
         Long increment = redisCache.increment("live:like:" + liveId, 1);
-
-        SendMsgVo sendMsgVo = new SendMsgVo();
-        sendMsgVo.setLiveId(liveId);
-        sendMsgVo.setCmd("likeDetail");
-        sendMsgVo.setData(JSON.toJSONString(increment));
-        webSocketServer.broadcastLikeMessage(liveId, JSONObject.toJSONString(sendMsgVo));
         return R.ok().put("like",increment);
     }
 }

+ 1 - 0
fs-live-socket/src/main/java/com/fs/live/task/Task.java

@@ -194,6 +194,7 @@ public class Task {
                         redisCache.redisTemplate.opsForZSet().remove(key + live.getLiveId(), JSON.toJSONString(liveAutoTask),liveAutoTask.getAbsValue().getTime());
                     });
                 }
+                webSocketServer.removeLikeCountCache(live.getLiveId());
             }
         }
         if(!liveList.isEmpty()){

+ 37 - 0
fs-live-socket/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -38,6 +38,8 @@ import static com.fs.common.constant.LiveKeysConstant.*;
 @Slf4j
 public class WebSocketServer {
 
+    // 上次点赞数缓存
+    private final ConcurrentHashMap<Long, Integer> lastLikeCountCache = new ConcurrentHashMap<>();
     // 直播间用户session
     private final static ConcurrentHashMap<Long, ConcurrentHashMap<Long, Session>> rooms = new ConcurrentHashMap<>();
     // 管理端连接
@@ -514,4 +516,39 @@ public class WebSocketServer {
         String key = "live:auto_task:";
         redisCache.redisTemplate.opsForZSet().removeRangeByScore(key + liveId, data, data);
     }
+
+    public void removeLikeCountCache(Long liveId) {
+        lastLikeCountCache.remove(liveId);
+    }
+
+
+    @Scheduled(fixedRate = 300)// 每分钟执行一次
+    public void broadcastLikeMessage() {
+        Set<Long> activeLiveIds = new HashSet<>();
+        for (Map.Entry<Long, ConcurrentHashMap<Long, Session>> entry : rooms.entrySet()) {
+            Long liveId = entry.getKey();
+            activeLiveIds.add(liveId);
+            String likeKey = "live:like:" + liveId;
+            Object cacheObject = redisCache.getCacheObject(likeKey);
+            if(cacheObject == null) continue;
+            Integer current = null;
+            try {
+                String valueStr = cacheObject.toString().trim();
+                current = Integer.parseInt(valueStr);
+            } catch (NumberFormatException e) {
+                log.error("点赞数格式错误,liveId: {}, value: {}", liveId, cacheObject, e);
+                continue;
+            }
+            Integer last = lastLikeCountCache.getOrDefault(liveId, 0);
+            if (!current.equals(last)) {
+                SendMsgVo sendMsgVo = new SendMsgVo();
+                sendMsgVo.setLiveId(liveId);
+                sendMsgVo.setCmd("likeDetail");
+                sendMsgVo.setData(JSON.toJSONString(current));
+                broadcastLikeMessage(liveId, JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
+                lastLikeCountCache.put(liveId, current); // 更新上次值
+            }
+        }
+        lastLikeCountCache.keySet().removeIf(liveId -> !activeLiveIds.contains(liveId));
+    }
 }

+ 2 - 0
fs-service-system/src/main/java/com/fs/live/service/impl/LiveServiceImpl.java

@@ -36,6 +36,7 @@ import okhttp3.FormBody;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.Response;
+import org.apache.ibatis.javassist.tools.web.Webserver;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -821,6 +822,7 @@ public class LiveServiceImpl implements ILiveService
                 liveGoodsEntity.setGoodsId(null);
                 liveGoodsEntity.setLiveId(newLiveId);
                 liveGoodsEntity.setCreateTime(now);
+                liveGoodsEntity.setIsShow(false);
                 liveGoodsEntity.setCompanyId(live.getCompanyId());
                 liveGoodsEntity.setCompanyUserId(live.getCompanyUserId());
                 liveGoodsService.insertLiveGoods(liveGoodsEntity);