From f0466e84d489e56d669be126f030880813368322 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Wed, 4 Mar 2026 11:10:26 +0800 Subject: [PATCH] =?UTF-8?q?perf(aiot):=20=E6=88=AA=E5=9B=BE=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=E2=80=94=20DB=E5=85=9C=E5=BA=95=20+=20=E7=BC=93?= =?UTF-8?q?=E5=AD=98TTL=E5=A2=9E=E5=88=B030=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:Redis缓存5分钟过期后,每次都重新触发Edge截图(15秒) 优化: - Redis缓存TTL从5分钟增到30分钟(COS presigned URL有效1小时) - Redis未命中时先查DB的cos_key,用CosUtil生成新presigned URL - 只有DB也没有记录(首次截图)才触发Edge 效果: - 首次截图:仍需触发Edge(不可避免) - 后续访问:30分钟内走Redis缓存(毫秒级) - 30分钟后:走DB → COS presign(秒级,无需Edge) Co-Authored-By: Claude Opus 4.6 --- .../service/impl/AiScreenshotServiceImpl.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiScreenshotServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiScreenshotServiceImpl.java index 4a46aa391..2c22fcc9c 100644 --- a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiScreenshotServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiScreenshotServiceImpl.java @@ -33,8 +33,8 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService { private static final String SNAP_CACHE_KEY_PREFIX = "snap:cache:"; private static final String SNAP_RESULT_KEY_PREFIX = "snap:result:"; - /** 缓存 TTL(秒) */ - private static final long SNAP_CACHE_TTL = 300; + /** 缓存 TTL(秒)— 与 COS presigned URL 有效期(1小时)匹配,设为 30 分钟 */ + private static final long SNAP_CACHE_TTL = 1800; /** 最大等待时间(秒) */ private static final long MAX_WAIT_SECONDS = 15; /** 降级 Redis 结果 TTL(秒) */ @@ -80,6 +80,21 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService { log.warn("[AI截图] 缓存解析失败: {}", e.getMessage()); } } + + // 1.5 Redis 缓存未命中 → 从 DB 读取持久化的 cos_key,生成新 presigned URL + // 这样 Redis 过期后不需要重新触发 Edge 截图 + String cosKey = snapshotMapper.getCosKey(cameraCode); + if (cosKey != null && cosUtil.isAvailable()) { + String presignedUrl = cosUtil.generatePresignedUrl(cosKey); + if (presignedUrl != null) { + writeCache(cameraCode, presignedUrl); + result.put("status", "ok"); + result.put("url", presignedUrl); + result.put("cached", true); + log.info("[AI截图] DB兜底命中: cameraCode={}, cosKey={}", cameraCode, cosKey); + return result; + } + } } // 2. 生成 request_id 和 COS 路径