|
|
@@ -20,8 +20,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.course.mapper.FsUserVideoCommentMapper;
|
|
|
+import com.fs.course.mapper.FsUserVideoMapper;
|
|
|
import com.fs.course.domain.FsUserVideoComment;
|
|
|
import com.fs.course.service.IFsUserVideoCommentService;
|
|
|
+import com.fs.course.utils.VideoCommentCountRedisUtil;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
/**
|
|
|
@@ -48,6 +50,12 @@ public class FsUserVideoCommentServiceImpl implements IFsUserVideoCommentService
|
|
|
@Autowired
|
|
|
private FsUserMapper fsUserMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private FsUserVideoMapper fsUserVideoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private VideoCommentCountRedisUtil videoCommentCountRedisUtil;
|
|
|
+
|
|
|
private static final String COMMENT_LIST_KEY_PREFIX = "comment:list:video:";
|
|
|
private static final String COMMENT_HASH_KEY_PREFIX = "comment:hash:video:";
|
|
|
private static final String REPLY_LIST_KEY_PREFIX = "reply:list:comment:";
|
|
|
@@ -58,8 +66,6 @@ public class FsUserVideoCommentServiceImpl implements IFsUserVideoCommentService
|
|
|
private static final String COMMENT_LIKE_KEY_PREFIX = "like:comment:";
|
|
|
private static final String COMMENT_LIKE_COUNT_KEY_PREFIX = "likecount:comment:";
|
|
|
private static final String COMMENT_REPLY_COUNT_KEY_PREFIX = "reply:count:comment:";
|
|
|
- private static final String VIDEO_COMMENT_COUNT_KEY_PREFIX = "comment:count:video:";
|
|
|
-
|
|
|
/**
|
|
|
* 查询课堂视频评论
|
|
|
*
|
|
|
@@ -170,12 +176,8 @@ public class FsUserVideoCommentServiceImpl implements IFsUserVideoCommentService
|
|
|
// redisTemplate.expire(listKey, 1, TimeUnit.DAYS);
|
|
|
// redisTemplate.expire(hashKey, 1, TimeUnit.DAYS);
|
|
|
//
|
|
|
-// // 增加视频的评论数
|
|
|
-// String videoCommentCountKey = VIDEO_COMMENT_COUNT_KEY_PREFIX + param.getVideoId();
|
|
|
-// if (redisTemplate.opsForValue().get(videoCommentCountKey) == null) {
|
|
|
-// redisTemplate.opsForValue().set(videoCommentCountKey, 0);
|
|
|
-// }
|
|
|
-// redisTemplate.opsForValue().increment(videoCommentCountKey, 1);
|
|
|
+// // 增加视频的评论数(须走 VideoCommentCountRedisUtil 以登记索引)
|
|
|
+// videoCommentCountRedisUtil.incrementCommentCount(param.getVideoId());
|
|
|
// return R.ok().put("data", comment);
|
|
|
// }
|
|
|
|
|
|
@@ -584,4 +586,40 @@ public class FsUserVideoCommentServiceImpl implements IFsUserVideoCommentService
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void syncCommentCountToDatabase() {
|
|
|
+ Set<String> keys = videoCommentCountRedisUtil.listKeys();
|
|
|
+ if (keys == null || keys.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String key : keys) {
|
|
|
+ try {
|
|
|
+ String videoIdStr = key.split(":")[3];
|
|
|
+ Long videoId = Long.parseLong(videoIdStr);
|
|
|
+ Object value = redisTemplate.opsForValue().get(key);
|
|
|
+ Integer commentCount = toIntegerCount(value);
|
|
|
+ if (commentCount != null) {
|
|
|
+ fsUserVideoMapper.updateCommentCount(videoId, commentCount);
|
|
|
+ redisTemplate.delete(key);
|
|
|
+ videoCommentCountRedisUtil.untrack(key);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 单条失败不影响其余 key
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Integer toIntegerCount(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return (Integer) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return ((Long) value).intValue();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|