yuhongqi 1 nedēļu atpakaļ
vecāks
revīzija
cf657331b4

+ 70 - 25
fs-user-app/src/main/java/com/fs/app/facade/impl/LiveFacadeServiceImpl.java

@@ -158,7 +158,7 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
         if (ObjectUtil.isEmpty(liveVo)) {
             return R.error("未找到直播");
         }
-        if(liveVo.getIsShow() == 2) {
+        if (Integer.valueOf(2).equals(liveVo.getIsShow())) {
             return R.error("直播未开放");
         }
         return R.ok().put("data", liveVo);
@@ -172,7 +172,7 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
         if (ObjectUtil.isEmpty(liveVo)) {
             return R.error("未找到直播");
         }
-        if(liveVo.getIsShow() == 2) {
+        if (Integer.valueOf(2).equals(liveVo.getIsShow())) {
             return R.error("直播未开放");
         }
 
@@ -240,40 +240,85 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
     }
 
     /**
-     * Redis 可能已是 LiveVo / JSONObject / JSON 字符串;禁止用 Lombok toString 再 parse
+     * 读取直播详情缓存:优先 StringRedis 原始 JSON,兼容旧 Object 序列化;
+     * 缓存为空/无效(无 liveId)时删 key 并回源 DB。
      */
     private LiveVo parseLiveDetailCache(Long id) {
-        String detailKey = String.format(LiveKeysConstant.LIVE_HOME_PAGE_DETAIL, id);
-        Object cached;
-        try {
-            cached = redisUtil.get(detailKey);
-        } catch (Exception e) {
-            log.warn("读取直播详情缓存失败,重建缓存 liveId={}, key={}, err={}", id, detailKey, e.getMessage());
-            redisUtil.delete(detailKey);
-            return liveService.asyncToCacheLiveDetail(id);
+//        String detailKey = String.format(LiveKeysConstant.LIVE_HOME_PAGE_DETAIL, id);
+//        try {
+//            LiveVo cached = readLiveDetailFromRedis(detailKey);
+//            if (isValidLiveDetail(cached)) {
+//                return cached;
+//            }
+//            if (ObjectUtil.isNotEmpty(redisUtil.getString(detailKey)) || redisUtil.hasKey(detailKey)) {
+//                log.warn("直播详情缓存为空或无效,删除并重建 liveId={}, key={}", id, detailKey);
+//                redisUtil.delete(detailKey);
+//            }
+//        } catch (Exception e) {
+//            log.warn("读取直播详情缓存失败,删除并重建 liveId={}, key={}, err={}", id, detailKey, e.getMessage());
+//            try {
+//                redisUtil.delete(detailKey);
+//            } catch (Exception ignore) {
+//                // ignore
+//            }
+//        }
+        return liveService.asyncToCacheLiveDetail(id);
+    }
+
+    private LiveVo readLiveDetailFromRedis(String detailKey) {
+        // 新格式:StringRedisTemplate 存的纯 JSON
+        LiveVo fromString = parseLiveDetailText(redisUtil.getString(detailKey));
+        if (isValidLiveDetail(fromString)) {
+            return fromString;
         }
+        // 旧格式:RedisTemplate + FastJson 对象/二次编码字符串
+        Object cached = redisUtil.get(detailKey);
         if (ObjectUtil.isEmpty(cached)) {
-            return liveService.asyncToCacheLiveDetail(id);
+            return null;
         }
-        try {
-            if (cached instanceof LiveVo) {
-                return (LiveVo) cached;
+        if (cached instanceof LiveVo) {
+            return (LiveVo) cached;
+        }
+        if (cached instanceof String) {
+            return parseLiveDetailText((String) cached);
+        }
+        return JSON.parseObject(JSON.toJSONString(cached), LiveVo.class);
+    }
+
+    private LiveVo parseLiveDetailText(String text) {
+        if (text == null) {
+            return null;
+        }
+        text = text.trim();
+        if (text.isEmpty()) {
+            return null;
+        }
+        // 兼容双重 JSON 编码: "{\"liveId\":1,...}"
+        if (text.startsWith("\"") && text.endsWith("\"")) {
+            try {
+                text = JSON.parseObject(text, String.class);
+            } catch (Exception e) {
+                return null;
             }
-            if (cached instanceof String) {
-                String text = ((String) cached).trim();
-                if (!text.startsWith("{")) {
-                    throw new IllegalArgumentException("invalid live detail cache text");
-                }
-                return JSON.parseObject(text, LiveVo.class);
+            if (text == null) {
+                return null;
             }
-            return JSON.parseObject(JSON.toJSONString(cached), LiveVo.class);
+            text = text.trim();
+        }
+        if (!text.startsWith("{")) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(text, LiveVo.class);
         } catch (Exception e) {
-            log.warn("解析直播详情缓存失败,删除并重建 liveId={}, key={}, err={}", id, detailKey, e.getMessage());
-            redisUtil.delete(detailKey);
-            return liveService.asyncToCacheLiveDetail(id);
+            return null;
         }
     }
 
+    private boolean isValidLiveDetail(LiveVo liveVo) {
+        return liveVo != null && liveVo.getLiveId() != null;
+    }
+
 
     @Override
     public R currentActivities(Long liveId, String userId) {