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
@@ -71,6 +71,14 @@ public interface AttachmentVariantMapper {
/// @param variant 变体
void insert(AttachmentVariant variant);
/// 延长缩略图变体的过期时间
///
/// @param id 变体 ID
/// @param now 当前时间
/// @param expireAt 新的过期时间
/// @return 更新记录数
int updateExpireAt(@Param("id") String id, @Param("now") long now, @Param("expireAt") long expireAt);
/// 删除变体
///
/// @param id 变体 ID
@@ -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,
@@ -98,6 +98,13 @@
)
</insert>
<update id="updateExpireAt">
UPDATE `attachment_variant`
SET `expire_at` = #{expireAt}
WHERE `id` = #{id}
AND `expire_at` &gt; #{now}
</update>
<delete id="delete">
DELETE FROM `attachment_variant`
WHERE `id` = #{id}