|
|
@@ -40,7 +40,6 @@ import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.fs.common.constant.LiveKeysConstant.*;
|
|
|
-import static com.fs.common.constant.LiveKeysConstant.LIVE_COUPON_NUM;
|
|
|
import static com.fs.live.websocket.service.WebSocketServer.USER_ENTRY_TIME_KEY;
|
|
|
|
|
|
@Component
|
|
|
@@ -198,8 +197,9 @@ public class Task {
|
|
|
tagMarkInfo.put("startTime", live.getStartTime().atZone(java.time.ZoneId.systemDefault()).toInstant().toEpochMilli());
|
|
|
tagMarkInfo.put("videoDuration", videoDuration);
|
|
|
|
|
|
- String tagMarkKey = String.format(LiveKeysConstant.LIVE_TAG_MARK_CACHE, live.getLiveId());
|
|
|
+ String tagMarkKey = liveDelayedTaskRedisUtil.tagMarkKey(live.getLiveId());
|
|
|
redisCache.setCacheObject(tagMarkKey, JSON.toJSONString(tagMarkInfo), 24, TimeUnit.HOURS);
|
|
|
+ liveDelayedTaskRedisUtil.trackTagMark(live.getLiveId());
|
|
|
log.info("直播间开启,已加入打标签缓存: liveId={}, startTime={}, videoDuration={}",
|
|
|
live.getLiveId(), live.getStartTime(), videoDuration);
|
|
|
}
|
|
|
@@ -228,8 +228,9 @@ public class Task {
|
|
|
|
|
|
// 删除打标签缓存
|
|
|
try {
|
|
|
- String tagMarkKey = String.format(LiveKeysConstant.LIVE_TAG_MARK_CACHE, live.getLiveId());
|
|
|
+ String tagMarkKey = liveDelayedTaskRedisUtil.tagMarkKey(live.getLiveId());
|
|
|
redisCache.deleteObject(tagMarkKey);
|
|
|
+ liveDelayedTaskRedisUtil.untrackTagMark(live.getLiveId());
|
|
|
log.info("直播间结束,已删除打标签缓存: liveId={}", live.getLiveId());
|
|
|
} catch (Exception e) {
|
|
|
log.error("删除直播间打标签缓存失败: liveId={}, error={}", live.getLiveId(), e.getMessage(), e);
|
|
|
@@ -608,17 +609,23 @@ public class Task {
|
|
|
}
|
|
|
/*// 更新数据库
|
|
|
liveDataService.updateLiveData(liveData);*/
|
|
|
- Set<String> keys = redisCache.redisTemplate.keys(String.format(LIVE_COUPON_NUM, "*"));
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- for (String key : keys) {
|
|
|
- Object o = redisCache.redisTemplate.opsForValue().get(String.format(LIVE_COUPON_NUM, key));
|
|
|
- if (o != null) {
|
|
|
- LiveCouponIssue updateEntity = new LiveCouponIssue();
|
|
|
- updateEntity.setId(Long.valueOf(key));
|
|
|
- updateEntity.setRemainCount(Long.parseLong(o.toString()));
|
|
|
- liveCouponIssueService.updateLiveCouponIssue(updateEntity);
|
|
|
+ for (String key : liveDelayedTaskRedisUtil.listCouponNumKeys()) {
|
|
|
+ Object o = redisCache.getCacheObject(key);
|
|
|
+ if (o == null) {
|
|
|
+ Long issueId = LiveKeysConstant.parseCouponIssueIdFromKey(key);
|
|
|
+ if (issueId != null) {
|
|
|
+ liveDelayedTaskRedisUtil.untrackCouponNum(issueId);
|
|
|
}
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Long issueId = LiveKeysConstant.parseCouponIssueIdFromKey(key);
|
|
|
+ if (issueId == null) {
|
|
|
+ continue;
|
|
|
}
|
|
|
+ LiveCouponIssue updateEntity = new LiveCouponIssue();
|
|
|
+ updateEntity.setId(issueId);
|
|
|
+ updateEntity.setRemainCount(Long.parseLong(o.toString()));
|
|
|
+ liveCouponIssueService.updateLiveCouponIssue(updateEntity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -640,11 +647,8 @@ public class Task {
|
|
|
public void scanLiveTagMark() {
|
|
|
try {
|
|
|
|
|
|
- // 获取所有打标签缓存的key
|
|
|
- String pattern = String.format(LiveKeysConstant.LIVE_TAG_MARK_CACHE, "*");
|
|
|
- Set<String> keys = redisCache.redisTemplate.keys(pattern);
|
|
|
-
|
|
|
- if (keys == null || keys.isEmpty()) {
|
|
|
+ Set<String> keys = liveDelayedTaskRedisUtil.listTagMarkKeys();
|
|
|
+ if (keys.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -657,6 +661,10 @@ public class Task {
|
|
|
// 从Redis获取直播间信息
|
|
|
Object cacheValue = redisCache.getCacheObject(key);
|
|
|
if (cacheValue == null) {
|
|
|
+ Long staleLiveId = LiveKeysConstant.parseLiveIdFromTaskZSetKey(key);
|
|
|
+ if (staleLiveId != null) {
|
|
|
+ liveDelayedTaskRedisUtil.untrackTagMark(staleLiveId);
|
|
|
+ }
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -792,8 +800,9 @@ public class Task {
|
|
|
// 删除已处理的直播间缓存
|
|
|
for (Long liveId : processedLiveIds) {
|
|
|
try {
|
|
|
- String tagMarkKey = String.format(LiveKeysConstant.LIVE_TAG_MARK_CACHE, liveId);
|
|
|
+ String tagMarkKey = liveDelayedTaskRedisUtil.tagMarkKey(liveId);
|
|
|
redisCache.deleteObject(tagMarkKey);
|
|
|
+ liveDelayedTaskRedisUtil.untrackTagMark(liveId);
|
|
|
} catch (Exception e) {
|
|
|
log.error("删除直播间打标签缓存失败: liveId={}, error={}", liveId, e.getMessage(), e);
|
|
|
}
|
|
|
@@ -883,9 +892,10 @@ public class Task {
|
|
|
continue;
|
|
|
}
|
|
|
//更新最新用户活跃时间
|
|
|
- String liveUserWatchLogKey = String.format(LIVE_USER_WATCH_LOG_CACHE, liveId, userId,externalContactId,qwUserId);
|
|
|
+ String liveUserWatchLogKey = LiveKeysConstant.liveUserWatchLogKey(liveId, userId, externalContactId, qwUserId);
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
- redisCache.setCacheObject(liveUserWatchLogKey,formatter.format(now),5,TimeUnit.MINUTES);
|
|
|
+ redisCache.setCacheObject(liveUserWatchLogKey, formatter.format(now), 5, TimeUnit.MINUTES);
|
|
|
+ liveDelayedTaskRedisUtil.trackUserWatchLog(liveUserWatchLogKey);
|
|
|
// 使用 updateLiveWatchLogTypeByDuration 的逻辑更新观看记录状态
|
|
|
updateLiveWatchLogTypeByDuration(liveId, userId, qwUserId, externalContactId,
|
|
|
onlineSeconds, totalVideoDuration, updateLog);
|
|
|
@@ -986,14 +996,18 @@ public class Task {
|
|
|
@DistributeLock(key = "updateLiveWatchUserStatus", scene = "task")
|
|
|
public void updateLiveWatchUserStatus() {
|
|
|
try {
|
|
|
- Set<String> keys = redisCache.redisTemplate.keys("live:user:watch:log:*");
|
|
|
+ Set<String> keys = liveDelayedTaskRedisUtil.listUserWatchLogKeys();
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
List<LiveWatchLog> updateLog = new ArrayList<>();
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
+ if (!keys.isEmpty()) {
|
|
|
for (String key : keys) {
|
|
|
String[] split = key.split(":");
|
|
|
String cacheTime = redisCache.getCacheObject(key);
|
|
|
+ if (StringUtils.isBlank(cacheTime)) {
|
|
|
+ liveDelayedTaskRedisUtil.untrackUserWatchLog(key);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
//判断缓存的值是否已经距离现在超过一分钟
|
|
|
if (StringUtils.isNotBlank(cacheTime)) {
|
|
|
try {
|