瀏覽代碼

直播脚本提交;定时任务刷新点赞数

yuhongqi 6 天之前
父節點
當前提交
20234c3e96

+ 22 - 5
fs-live-socket/src/main/java/com/fs/live/task/Task.java

@@ -44,6 +44,7 @@ import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.annotation.PostConstruct;
 import java.math.BigDecimal;
 import java.text.ParseException;
 import java.time.Instant;
@@ -439,20 +440,36 @@ public class Task {
         }
     }
 
+    @PostConstruct
+    public void initLiveDatasOnStartup() {
+        log.info("项目启动,开始初始化直播点赞数据...");
+        try {
+            List<LiveData> liveDatas = liveDataService.getAllLiveDatas();
+            if (CollectionUtils.isNotEmpty(liveDatas)) {
+                for (LiveData liveData : liveDatas) {
+                    redisCache.deleteObject("live:like:" + liveData.getLiveId());
+                    redisCache.setCacheObject("live:like:" + liveData.getLiveId(), liveData.getLikes().intValue());
+                }
+            }
+        } catch (Exception e) {
+            log.error("项目启动时加载直播数据失败", e);
+        }
+    }
+
+
+
     /**
      *定期将缓存的数据写入数据库
      */
-    @Scheduled(cron = "0 0/1 * * * ?")// 每分钟执行一次
+    @Scheduled(cron = "0 0 0/1 * * ?")// 每分钟执行一次
     public void syncLiveDataToDB() {
         List<LiveData> liveDatas = liveDataService.getAllLiveDatas(); // 获取所有正在直播的直播间数据
         if(liveDatas == null)
             return;
         liveDatas.forEach(liveData ->{
 
-            Long likeCount = getAsLong(redisCache, "live:like:" + liveData.getLiveId());
-            long resultLikeCount = liveData.getLikes() + likeCount;
-            redisCache.setCacheObject("live:like:" + liveData.getLiveId(), resultLikeCount);
-            redisCache.expire("live:like:" + liveData.getLiveId(), 30, TimeUnit.MINUTES);
+            Long resultLikeCount = getAsLong(redisCache, "live:like:" + liveData.getLiveId());
+            redisCache.setCacheObject("live:like:" + liveData.getLiveId(), resultLikeCount.intValue());
             liveData.setLikes(
                     resultLikeCount
             );

+ 3 - 5
fs-service-system/src/main/java/com/fs/live/service/impl/LiveDataServiceImpl.java

@@ -438,8 +438,6 @@ public class LiveDataServiceImpl implements ILiveDataService {
         boolean firstLike = redisCache.setIfAbsent(key, 1, 1, TimeUnit.DAYS);
         //用户直播间第一次点赞
         if(firstLike) {
-            //直播间总点赞数
-
             liveUserLike = liveUserLikeService.selectLiveUserLikeByIds(liveId, userId);
             if (liveUserLike == null) {
                 liveUserLike = new LiveUserLike();
@@ -452,16 +450,16 @@ public class LiveDataServiceImpl implements ILiveDataService {
         }else{
             //用户直播间点赞数
             redisCache.increment(key,1);
-            //直播间总点赞数
         }
         try {
+            //直播间总点赞数
             redisCache.increment("live:like:" + liveId,1);
         } catch (Exception e) {
             log.error(e.getMessage());
-            redisCache.setCacheObject("live:like:" + liveId, 1L);
+            redisCache.setCacheObject("live:like:" + liveId, 1);
         }
 
-        return R.ok().put("like",redisCache.getCacheObject(key));
+        return R.ok().put("like",redisCache.getCacheObject("live:like:" + liveId));
     }
 
     @Override