task-62: 本地任务实体缓存共享组件(容量 LRU/TTL 即时回收/定时清理)接入 8 模块

This commit is contained in:
2026-08-30 18:17:52 +08:00
parent d2f0f10232
commit 3b8c7b4948
11 changed files with 319 additions and 182 deletions
@@ -0,0 +1,101 @@
package com.nanri.aiimage.modules.task.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Comparator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 共享任务链路本地任务实体缓存:容量上限 LRU 淘汰、TTL 过期即时回收、
* 定时清理批量释放过期条目,保证本地缓存内存有界。
*/
@Component
@Slf4j
public class TaskEntityLocalCache {
private final TaskPressureProperties properties;
private final ObjectMapper objectMapper;
private final ConcurrentHashMap<Long, Entry> entries = new ConcurrentHashMap<>();
public TaskEntityLocalCache(TaskPressureProperties properties, ObjectMapper objectMapper) {
this.properties = properties;
this.objectMapper = objectMapper;
}
public void put(Long taskId, FileTaskEntity task) {
if (taskId == null || taskId <= 0 || task == null || task.getId() == null) {
return;
}
entries.put(taskId, new Entry(System.currentTimeMillis(),
objectMapper.convertValue(task, FileTaskEntity.class)));
int capacity = Math.max(1, properties.getLocalTaskEntityCacheCapacity());
if (entries.size() > capacity) {
entries.entrySet().stream()
.sorted(Map.Entry.comparingByValue(
Comparator.comparingLong(Entry::cachedAtMillis)
.thenComparingLong(e -> e.task().getId() == null ? 0L : e.task().getId())))
.limit(entries.size() - capacity)
.forEach(entryToEvict -> entries.remove(entryToEvict.getKey()));
}
}
/** 返回 TTL 内新鲜实体副本;过期条目读取时即时回收返回 null。 */
public Entry get(Long taskId) {
return get(taskId, System.currentTimeMillis());
}
public Entry get(Long taskId, long now) {
if (taskId == null || taskId <= 0) {
return null;
}
Entry cached = entries.get(taskId);
if (cached == null) {
return null;
}
if (now - cached.cachedAtMillis() > Math.max(0L, properties.getLocalTaskEntityCacheMillis())) {
entries.remove(taskId);
return null;
}
return new Entry(cached.cachedAtMillis(),
objectMapper.convertValue(cached.task(), FileTaskEntity.class));
}
public void evict(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
entries.remove(taskId);
}
/** 定时清理:批量移除全部过期条目,返回移除数量。 */
public int removeExpired(long now) {
int[] removed = {0};
entries.forEach((taskId, cached) -> {
if (now - cached.cachedAtMillis() > Math.max(0L, properties.getLocalTaskEntityCacheMillis())) {
entries.remove(taskId);
removed[0]++;
}
});
return removed[0];
}
@Scheduled(fixedDelayString = "${aiimage.task-pressure.local-task-entity-cache-cleanup-delay-ms:60000}")
public void cleanup() {
int removed = removeExpired(System.currentTimeMillis());
if (removed > 0) {
log.debug("[task-entity-cache] cleaned expired entries count={}", removed);
}
}
public int size() {
return entries.size();
}
public record Entry(long cachedAtMillis, FileTaskEntity task) {}
}