keep alive attachment variant

This commit is contained in:
Timi
2026-08-25 22:54:55 +08:00
parent 0239697a2b
commit 9f884e2b74
3 changed files with 41 additions and 3 deletions
@@ -112,7 +112,10 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
now
);
if (cached != null) {
return cached;
Attachment refreshed = refreshCachedThumb(cached);
if (refreshed != null) {
return refreshed;
}
}
String lockKey = buildThumbLockKey(source, width, height, fitMode);
@@ -131,7 +134,10 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
Time.now()
);
if (cached != null) {
return cached;
Attachment refreshed = refreshCachedThumb(cached);
if (refreshed != null) {
return refreshed;
}
}
AttachmentVariant oldVariant = variantMapper.selectByKey(
source.getId(),
@@ -197,7 +203,10 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
Time.now()
);
if (concurrent != null) {
return concurrent;
Attachment refreshed = refreshCachedThumb(concurrent);
if (refreshed != null) {
return refreshed;
}
}
throw e;
}
@@ -223,6 +232,20 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
}
}
/// 延长已命中缩略图缓存的有效期
///
/// @param cached 已命中的缩略图
/// @return 更新成功时返回刷新后的缩略图,记录已不存在或已过期时返回 `null`
private Attachment refreshCachedThumb(Attachment cached) {
long now = Time.now();
long expireAt = now + THUMB_CACHE_TTL;
if (!(0 < variantMapper.updateExpireAt(cached.getId(), now, expireAt))) {
return null;
}
cached.setDestroyAt(expireAt);
return cached;
}
private String buildThumbLockKey(Attachment source, int width, int height, ThumbnailFitMode fitMode) {
return "%s%s:%s:%s:%s:%s".formatted(
THUMB_LOCK_KEY_PREFIX,