keep alive attachment variant
This commit is contained in:
@@ -71,6 +71,14 @@ public interface AttachmentVariantMapper {
|
|||||||
/// @param variant 变体
|
/// @param variant 变体
|
||||||
void insert(AttachmentVariant 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
|
/// @param id 变体 ID
|
||||||
|
|||||||
+26
-3
@@ -112,7 +112,10 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
|
|||||||
now
|
now
|
||||||
);
|
);
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
return cached;
|
Attachment refreshed = refreshCachedThumb(cached);
|
||||||
|
if (refreshed != null) {
|
||||||
|
return refreshed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String lockKey = buildThumbLockKey(source, width, height, fitMode);
|
String lockKey = buildThumbLockKey(source, width, height, fitMode);
|
||||||
@@ -131,7 +134,10 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
|
|||||||
Time.now()
|
Time.now()
|
||||||
);
|
);
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
return cached;
|
Attachment refreshed = refreshCachedThumb(cached);
|
||||||
|
if (refreshed != null) {
|
||||||
|
return refreshed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
AttachmentVariant oldVariant = variantMapper.selectByKey(
|
AttachmentVariant oldVariant = variantMapper.selectByKey(
|
||||||
source.getId(),
|
source.getId(),
|
||||||
@@ -197,7 +203,10 @@ public class AttachmentVariantServiceImplement implements AttachmentVariantServi
|
|||||||
Time.now()
|
Time.now()
|
||||||
);
|
);
|
||||||
if (concurrent != null) {
|
if (concurrent != null) {
|
||||||
return concurrent;
|
Attachment refreshed = refreshCachedThumb(concurrent);
|
||||||
|
if (refreshed != null) {
|
||||||
|
return refreshed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw e;
|
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) {
|
private String buildThumbLockKey(Attachment source, int width, int height, ThumbnailFitMode fitMode) {
|
||||||
return "%s%s:%s:%s:%s:%s".formatted(
|
return "%s%s:%s:%s:%s:%s".formatted(
|
||||||
THUMB_LOCK_KEY_PREFIX,
|
THUMB_LOCK_KEY_PREFIX,
|
||||||
|
|||||||
@@ -98,6 +98,13 @@
|
|||||||
)
|
)
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<update id="updateExpireAt">
|
||||||
|
UPDATE `attachment_variant`
|
||||||
|
SET `expire_at` = #{expireAt}
|
||||||
|
WHERE `id` = #{id}
|
||||||
|
AND `expire_at` > #{now}
|
||||||
|
</update>
|
||||||
|
|
||||||
<delete id="delete">
|
<delete id="delete">
|
||||||
DELETE FROM `attachment_variant`
|
DELETE FROM `attachment_variant`
|
||||||
WHERE `id` = #{id}
|
WHERE `id` = #{id}
|
||||||
|
|||||||
Reference in New Issue
Block a user