refactor: 第一批低风险收敛——任务缓存服务泛型化 + 前端死代码清理与状态映射统一
This commit is contained in:
+282
@@ -0,0 +1,282 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 业务模块「任务缓存服务」泛型基类:心跳键 + 任务实体缓存(本地 LRU + Redis)+ 店铺维度载荷存取。
|
||||
*
|
||||
* <p><b>新增任务模块请继承本类,不要复制粘贴再改前缀。</b>各模块此前是逐行复制的
|
||||
* XxxTaskCacheService(约 200 行 × N 份),除以下三点外完全一致,现统一收敛到这里:
|
||||
* <ul>
|
||||
* <li>{@code moduleType}:店铺维度载荷在 task_scope 存储里的隔离键;</li>
|
||||
* <li>{@code keyPrefix}:Redis 键前缀(心跳 {@code <keyPrefix>:task:heartbeat:*}、
|
||||
* 实体 {@code <keyPrefix>:task:entity:*})与降级日志标签 {@code [<keyPrefix>-cache]};</li>
|
||||
* <li>{@code payloadType}:店铺合并载荷的具体类型。必须传 {@code Class<T>},
|
||||
* 不能用 {@code (Class<T>) T.class} 之类的擦除技巧,否则 JSON 反序列化会得到 LinkedHashMap。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>子类只需提供构造器把三个差异常量传给 {@code super};模块独有的缓存形态
|
||||
* (如 ShopMatch 的文件实体缓存、PatrolDelete 的分段清理)在子类覆写对应方法即可。
|
||||
* 公共实现统一为「taskId 非法直接返回」的守卫版:此前个别模块的 touch/delete 缺守卫,
|
||||
* 但全量调用方传入的都是已校验的任务 id,且被跳过的底层操作本身也有 null 守卫,行为等价。
|
||||
*
|
||||
* @param <T> 该模块店铺维度合并载荷的类型(taskScopePayloadStorageService 的反序列化目标)
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class AbstractModuleTaskCacheService<T> {
|
||||
|
||||
/** 心跳与任务实体缓存的 Redis TTL(与历史各模块实现一致的 24 小时)。 */
|
||||
private static final long PAYLOAD_TTL_HOURS = 24;
|
||||
|
||||
private final String moduleType;
|
||||
private final String keyPrefix;
|
||||
private final Class<T> payloadType;
|
||||
private final String logTag;
|
||||
|
||||
protected final StringRedisTemplate stringRedisTemplate;
|
||||
protected final ObjectMapper objectMapper;
|
||||
protected final TaskScopePayloadStorageService taskScopePayloadStorageService;
|
||||
protected final TaskEntityLocalCache taskEntityLocalCache;
|
||||
|
||||
protected AbstractModuleTaskCacheService(String moduleType,
|
||||
String keyPrefix,
|
||||
Class<T> payloadType,
|
||||
StringRedisTemplate stringRedisTemplate,
|
||||
ObjectMapper objectMapper,
|
||||
TaskScopePayloadStorageService taskScopePayloadStorageService,
|
||||
TaskEntityLocalCache taskEntityLocalCache) {
|
||||
this.moduleType = moduleType;
|
||||
this.keyPrefix = keyPrefix;
|
||||
this.payloadType = payloadType;
|
||||
this.logTag = keyPrefix + "-cache";
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
this.objectMapper = objectMapper;
|
||||
this.taskScopePayloadStorageService = taskScopePayloadStorageService;
|
||||
this.taskEntityLocalCache = taskEntityLocalCache;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 心跳
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
public void touchTaskHeartbeat(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
buildTaskHeartbeatKey(taskId),
|
||||
String.valueOf(Instant.now().toEpochMilli()),
|
||||
Duration.ofHours(PAYLOAD_TTL_HOURS));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] touch heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public long getTaskHeartbeatMillis(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return 0L;
|
||||
}
|
||||
String raw;
|
||||
try {
|
||||
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] get heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
return 0L;
|
||||
}
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return 0L;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(raw);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
|
||||
Map<Long, Long> result = new LinkedHashMap<>();
|
||||
if (taskIds == null || taskIds.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
List<Long> normalized = taskIds.stream()
|
||||
.filter(id -> id != null && id > 0)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (normalized.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
|
||||
List<String> values;
|
||||
try {
|
||||
values = stringRedisTemplate.opsForValue().multiGet(keys);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] batch get heartbeat degraded taskIds={} msg={}", logTag, normalized, ex.getMessage());
|
||||
return result;
|
||||
}
|
||||
for (int i = 0; i < normalized.size(); i++) {
|
||||
String raw = values != null && i < values.size() ? values.get(i) : null;
|
||||
if (raw == null || raw.isBlank()) {
|
||||
result.put(normalized.get(i), 0L);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
result.put(normalized.get(i), Long.parseLong(raw));
|
||||
} catch (NumberFormatException ignored) {
|
||||
result.put(normalized.get(i), 0L);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 店铺维度合并载荷
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
public T getShopMergedPayload(Long taskId, String shopKey) {
|
||||
return taskScopePayloadStorageService.getScopePayload(taskId, moduleType, shopKey, payloadType);
|
||||
}
|
||||
|
||||
public void saveShopMergedPayload(Long taskId, String shopKey, T payload) {
|
||||
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
|
||||
return;
|
||||
}
|
||||
taskScopePayloadStorageService.saveScopePayload(taskId, moduleType, shopKey, payload);
|
||||
touchTaskHeartbeat(taskId);
|
||||
}
|
||||
|
||||
public void removeShopMergedPayload(Long taskId, String shopKey) {
|
||||
taskScopePayloadStorageService.removeScopePayload(taskId, moduleType, shopKey);
|
||||
}
|
||||
|
||||
public Map<String, T> getAllShopMergedPayload(Long taskId) {
|
||||
return taskScopePayloadStorageService.getAllScopePayload(taskId, moduleType, payloadType);
|
||||
}
|
||||
|
||||
public boolean hasAnyShopMergedPayload(Long taskId) {
|
||||
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, moduleType);
|
||||
}
|
||||
|
||||
public long countShopMergedPayload(Long taskId) {
|
||||
return taskScopePayloadStorageService.countScopePayload(taskId, moduleType);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 任务实体缓存(本地 LRU + Redis)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 只清理心跳/实体缓存,不动店铺维度载荷。删除整任务用 {@link #deleteTaskCache(Long)}。
|
||||
*/
|
||||
public void evictTaskCacheOnly(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
taskEntityLocalCache.evict(taskId);
|
||||
try {
|
||||
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
|
||||
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] delete cache degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteTaskCache(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
evictTaskCacheOnly(taskId);
|
||||
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, moduleType);
|
||||
}
|
||||
|
||||
public void saveTaskCache(FileTaskEntity task) {
|
||||
if (task == null || task.getId() == null) {
|
||||
return;
|
||||
}
|
||||
taskEntityLocalCache.put(task.getId(), task);
|
||||
try {
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
buildTaskEntityKey(task.getId()),
|
||||
objectMapper.writeValueAsString(task),
|
||||
Duration.ofHours(PAYLOAD_TTL_HOURS)
|
||||
);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long, FileTaskEntity> getTaskCacheBatch(List<Long> taskIds) {
|
||||
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
|
||||
if (taskIds == null || taskIds.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
List<Long> normalized = taskIds.stream()
|
||||
.filter(id -> id != null && id > 0)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (normalized.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
List<Long> missingIds = new ArrayList<>();
|
||||
for (Long taskId : normalized) {
|
||||
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
|
||||
if (cached != null) {
|
||||
result.put(taskId, cached.task());
|
||||
} else {
|
||||
missingIds.add(taskId);
|
||||
}
|
||||
}
|
||||
if (missingIds.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
|
||||
List<String> values;
|
||||
try {
|
||||
values = stringRedisTemplate.opsForValue().multiGet(keys);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] batch get task cache degraded taskIds={} msg={}", logTag, missingIds, ex.getMessage());
|
||||
return result;
|
||||
}
|
||||
for (int i = 0; i < missingIds.size(); i++) {
|
||||
Long taskId = missingIds.get(i);
|
||||
String val = values != null && i < values.size() ? values.get(i) : null;
|
||||
if (val == null || val.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
|
||||
result.put(taskId, task);
|
||||
taskEntityLocalCache.put(taskId, task);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 键构造(子类如需自定义键形可覆写)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
protected String moduleType() {
|
||||
return moduleType;
|
||||
}
|
||||
|
||||
protected String buildTaskHeartbeatKey(Long taskId) {
|
||||
return keyPrefix + ":task:heartbeat:" + taskId;
|
||||
}
|
||||
|
||||
protected String buildTaskEntityKey(Long taskId) {
|
||||
return keyPrefix + ":task:entity:" + taskId;
|
||||
}
|
||||
}
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 「待处理行」型任务缓存服务泛型基类:Redis list 暂存待 LLM 检测的行 + 心跳键。
|
||||
*
|
||||
* <p><b>新增同类任务模块请继承本类,不要复制粘贴再改前缀。</b>此类形态与
|
||||
* {@link AbstractModuleTaskCacheService}(心跳 + 实体缓存 + 店铺维度载荷)不同:
|
||||
* 它没有任务实体缓存,也没有按店铺聚合的载荷,只有按任务组织的「待处理行」队列
|
||||
* (此前 AppearancePatentTaskCacheService / SimilarAsinTaskCacheService 逐行复制)。
|
||||
* 模块级差异只有三点:
|
||||
* <ul>
|
||||
* <li>{@code keyPrefix}:Redis 键前缀({@code <keyPrefix>:task:pending-rows:*}、
|
||||
* {@code <keyPrefix>:task:heartbeat:*})与降级日志标签 {@code [<keyPrefix>-cache]};</li>
|
||||
* <li>{@code rowType}:行 DTO 的具体类型。必须传 {@code Class<R>}——Redis list 里每条
|
||||
* JSON 反序列化目标形如 {@code PendingRow<R>},泛型擦除后必须显式给出行类型,
|
||||
* 否则 row 字段会退化成 LinkedHashMap;</li>
|
||||
* <li>任务级附加键:子类覆写 {@link #deleteExtraTaskCacheKeys(Long)} 清理自己的行级标记键。</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <R> 待处理行 DTO 类型
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class AbstractPendingRowTaskCacheService<R> {
|
||||
|
||||
/** Redis list 中一条待处理行:所属店铺、来源分块、行内容。 */
|
||||
public record PendingRow<ROW>(String scopeHash, Integer chunkIndex, ROW row) {
|
||||
}
|
||||
|
||||
/** pending-rows list、心跳与完成标记的 Redis TTL(与历史实现一致的 24 小时)。 */
|
||||
private static final long TTL_HOURS = 24;
|
||||
|
||||
protected final StringRedisTemplate stringRedisTemplate;
|
||||
protected final ObjectMapper objectMapper;
|
||||
|
||||
private final String keyPrefix;
|
||||
private final String logTag;
|
||||
private final JavaType pendingRowJavaType;
|
||||
|
||||
protected AbstractPendingRowTaskCacheService(String keyPrefix,
|
||||
Class<R> rowType,
|
||||
StringRedisTemplate stringRedisTemplate,
|
||||
ObjectMapper objectMapper) {
|
||||
this.keyPrefix = keyPrefix;
|
||||
this.logTag = keyPrefix + "-cache";
|
||||
this.pendingRowJavaType = objectMapper.getTypeFactory()
|
||||
.constructParametricType(PendingRow.class, rowType);
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public void appendPendingRow(Long taskId, String scopeHash, Integer chunkIndex, R row) {
|
||||
if (taskId == null || taskId <= 0 || scopeHash == null || scopeHash.isBlank() || chunkIndex == null || row == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForList().rightPush(
|
||||
pendingRowsKey(taskId),
|
||||
objectMapper.writeValueAsString(new PendingRow<>(scopeHash, chunkIndex, row))
|
||||
);
|
||||
stringRedisTemplate.expire(pendingRowsKey(taskId), Duration.ofHours(TTL_HOURS));
|
||||
touchTaskHeartbeat(taskId);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public long pendingRowCount(Long taskId) {
|
||||
Long size;
|
||||
try {
|
||||
size = stringRedisTemplate.opsForList().size(pendingRowsKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] pending row count degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
return 0L;
|
||||
}
|
||||
return size == null ? 0L : size;
|
||||
}
|
||||
|
||||
public List<PendingRow<R>> drainPendingRows(Long taskId, int limit) {
|
||||
if (taskId == null || taskId <= 0 || limit <= 0) {
|
||||
return List.of();
|
||||
}
|
||||
String key = pendingRowsKey(taskId);
|
||||
List<String> values;
|
||||
try {
|
||||
values = stringRedisTemplate.opsForList().range(key, 0, limit - 1L);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] drain range degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
if (values == null || values.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForList().trim(key, values.size(), -1);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] drain trim degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
List<PendingRow<R>> rows = new ArrayList<>();
|
||||
for (String value : values) {
|
||||
if (value == null || value.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
rows.add(objectMapper.readValue(value, pendingRowJavaType));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
touchTaskHeartbeat(taskId);
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void touchTaskHeartbeat(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
heartbeatKey(taskId),
|
||||
String.valueOf(Instant.now().toEpochMilli()),
|
||||
Duration.ofHours(TTL_HOURS)
|
||||
);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] touch heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public long getTaskHeartbeatMillis(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return 0L;
|
||||
}
|
||||
String raw;
|
||||
try {
|
||||
raw = stringRedisTemplate.opsForValue().get(heartbeatKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] get heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
return 0L;
|
||||
}
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return 0L;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(raw);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteTaskCache(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.delete(pendingRowsKey(taskId));
|
||||
stringRedisTemplate.delete(heartbeatKey(taskId));
|
||||
deleteExtraTaskCacheKeys(taskId);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[{}] delete degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** 任务级附加键清理钩子(如外观专利的行级完成标记),默认无。 */
|
||||
protected void deleteExtraTaskCacheKeys(Long taskId) {
|
||||
}
|
||||
|
||||
protected String pendingRowsKey(Long taskId) {
|
||||
return keyPrefix + ":task:pending-rows:" + taskId;
|
||||
}
|
||||
|
||||
protected String heartbeatKey(Long taskId) {
|
||||
return keyPrefix + ":task:heartbeat:" + taskId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user