ct hai 4 días
pai
achega
109bba8611

+ 41 - 18
fs-service/src/main/java/com/fs/course/service/impl/FsCourseWatchLogServiceImpl.java

@@ -835,13 +835,18 @@ public class FsCourseWatchLogServiceImpl extends ServiceImpl<FsCourseWatchLogMap
                 log.error("key中id为null:{}", key);
                 continue;
             }
-            String durationStr = redisCache.getCacheObject(key, String.class);
-            if (com.fs.common.utils.StringUtils.isEmpty(durationStr)) {
+            Object cached = redisCache.getCacheObject(key);
+            if (cached == null) {
                 redisCache.deleteObject(key);
                 log.error("key中数据为null:{}", key);
                 continue;  // 如果 Redis 中没有记录,跳过
             }
-            Long duration = Long.valueOf(durationStr);
+            Long duration;
+            if (cached instanceof Number) {
+                duration = ((Number) cached).longValue();
+            } else {
+                duration = Long.valueOf(cached.toString());
+            }
 
             FsCourseWatchLog watchLog = new FsCourseWatchLog();
             watchLog.setVideoId(videoId);
@@ -1133,20 +1138,25 @@ public class FsCourseWatchLogServiceImpl extends ServiceImpl<FsCourseWatchLogMap
     public Long getVideoDuration(Long videoId){
         //将视频时长也存到redis
         String videoRedisKey = "h5user:video:duration:" + videoId;
-        Long videoDuration=0L;
-        try {
-            videoDuration = redisCache.getCacheObject(videoRedisKey, Long.class);
-        }catch (Exception e){
-            String string = redisCache.getCacheObject(videoRedisKey, String.class);
-            videoDuration=Long.parseLong(string);
-            log.error("key中id为S:{}", videoDuration);
+        Long videoDuration = null;
+        Object cached = redisCache.getCacheObject(videoRedisKey);
+        if (cached instanceof Number) {
+            videoDuration = ((Number) cached).longValue();
+        } else if (cached != null) {
+            try {
+                videoDuration = Long.parseLong(cached.toString());
+            } catch (Exception e) {
+                log.warn("视频时长缓存格式异常 key={}, value={}", videoRedisKey, cached);
+            }
         }
 
-
-        if (videoDuration==null){
+        if (videoDuration == null) {
             FsUserCourseVideo video = courseVideoMapper.selectFsUserCourseVideoByVideoId(videoId);
-            videoDuration=video.getDuration();
-            redisCache.setCacheObject(videoRedisKey,video.getDuration());
+            if (video == null || video.getDuration() == null) {
+                return 0L;
+            }
+            videoDuration = video.getDuration();
+            redisCache.setCacheObject(videoRedisKey, video.getDuration());
         }
         return videoDuration;
     }
@@ -1154,11 +1164,24 @@ public class FsCourseWatchLogServiceImpl extends ServiceImpl<FsCourseWatchLogMap
     public Long getVideoDurationIsOpen(Long videoId){
         //将视频时长也存到redis
         String videoRedisKey = "h5OpenUser:video:duration:" + videoId;
-        Long videoDuration = redisCache.getCacheObject(videoRedisKey, Long.class);
-        if (videoDuration==null){
+        Long videoDuration = null;
+        Object cached = redisCache.getCacheObject(videoRedisKey);
+        if (cached instanceof Number) {
+            videoDuration = ((Number) cached).longValue();
+        } else if (cached != null) {
+            try {
+                videoDuration = Long.parseLong(cached.toString());
+            } catch (Exception e) {
+                log.warn("公开课视频时长缓存格式异常 key={}, value={}", videoRedisKey, cached);
+            }
+        }
+        if (videoDuration == null) {
             FsUserCourseVideo video = courseVideoMapper.selectFsUserCourseVideoByVideoId(videoId);
-            videoDuration=video.getDuration();
-            redisCache.setCacheObject(videoRedisKey,video.getDuration());
+            if (video == null || video.getDuration() == null) {
+                return 0L;
+            }
+            videoDuration = video.getDuration();
+            redisCache.setCacheObject(videoRedisKey, video.getDuration());
         }
         return videoDuration;
     }

+ 20 - 8
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -501,14 +501,18 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
 
         // 从Redis中获取观看时长
         String redisKey = "h5user:watch:duration:" + param.getQwUserId() + ":" + param.getQwExternalId() + ":" + param.getVideoId();
-//        log.info("看课redis-key:{}", redisKey);
         try {
-            String durationStr = redisCache.getCacheObject(redisKey, String.class);
-            Long duration = durationStr != null ? Long.parseLong(durationStr) : 0L;
+            Object cached = redisCache.getCacheObject(redisKey);
+            long duration = 0L;
+            if (cached instanceof Number) {
+                duration = ((Number) cached).longValue();
+            } else if (cached != null) {
+                duration = Long.parseLong(cached.toString());
+            }
 
-            // 更新Redis中的观看时长
+            // 更新Redis中的观看时长(仅当新时长更大时才覆盖)
             if (param.getDuration() != null && param.getDuration() > duration) {
-                //24小时过期
+                // 2小时过期,存字符串避免 Integer/Long 反序列化差异
                 redisCache.setCacheObject(redisKey, param.getDuration().toString(), 2, TimeUnit.HOURS);
             }
             updateHeartbeat(param);
@@ -3614,7 +3618,17 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
      */
     private Long getVideoDuration(Long videoId) {
         String redisKey = "h5user:video:duration:" + videoId;
-        Long duration = redisCache.getCacheObject(redisKey, Long.class);
+        Long duration = null;
+        Object cached = redisCache.getCacheObject(redisKey);
+        if (cached instanceof Number) {
+            duration = ((Number) cached).longValue();
+        } else if (cached != null) {
+            try {
+                duration = Long.parseLong(cached.toString());
+            } catch (Exception e) {
+                log.warn("视频时长缓存格式异常 key={}, value={}", redisKey, cached);
+            }
+        }
 
         if (duration == null) {
             FsUserCourseVideoQVO videoInfo = selectFsUserCourseVideoByVideoIdVO(videoId, null);
@@ -3622,8 +3636,6 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
                 throw new IllegalArgumentException("视频时长信息不存在");
             }
             duration = videoInfo.getDuration();
-
-            // 将查询结果缓存到Redis,设置适当过期时间
             redisCache.setCacheObject(redisKey, duration);
         }