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