|
|
@@ -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));
|
|
|
+ }
|
|
|
}
|