From e859dd20043080e1099279e80538e780c12c5f64 Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 31 May 2025 22:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=A8=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=C3=A5=E7=BC=93=E5=AD=98=EF=BC=8C=E4=BC=98=E5=8C=96=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/impl/AbstractBaseFileImporter.java | 49 ++++++++++++------- .../north/controller/LoginController.java | 4 +- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/doc/parser/domain/impl/AbstractBaseFileImporter.java b/src/main/java/com/doc/parser/domain/impl/AbstractBaseFileImporter.java index 5eaafc3..a43582d 100644 --- a/src/main/java/com/doc/parser/domain/impl/AbstractBaseFileImporter.java +++ b/src/main/java/com/doc/parser/domain/impl/AbstractBaseFileImporter.java @@ -1,6 +1,7 @@ package com.doc.parser.domain.impl; import com.doc.parser.domain.iface.DocumentImporter; +import com.doc.parser.infrastructure.util.LocalCacheUtil; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; @@ -43,47 +44,57 @@ public abstract class AbstractBaseFileImporter implements DocumentImporter { .filter(p -> p.toString().toLowerCase().endsWith(getFileSuffix())) .forEach(path -> { try { - String content = extractContent(path); - if (content == null || content.isBlank()) { - logger.warn("跳过空内容文件: {}", path); - return; - } - Path absPath = path.toAbsolutePath().normalize(); Path relativePathObj; - if (absPath.startsWith(excludeBase)) { relativePathObj = excludeBase.relativize(absPath); } else { logger.warn("路径未匹配 exclude.prefix,使用全路径: {}", absPath); relativePathObj = absPath; } - String relativePath = relativePathObj.toString().replace("\\", "/"); + Long localMTime = Files.getLastModifiedTime(path).toMillis(); logger.info("absPath: {}", absPath.toString().replace("\\", "/")); - logger.info("rerelativePath: {}", relativePath); + logger.info("rerelativePath: {}, localMTime: {}", relativePath, localMTime); + // 优先从缓存中加载数据,判断是否文件有修改 + Long lastModifiedTime = (Long)LocalCacheUtil.get(relativePath); + if(localMTime.equals(lastModifiedTime)) { + logger.info("文件未变动,跳过导入: {}", relativePath); + return; + } + + String content = extractContent(path); + if (content == null || content.isBlank()) { + logger.warn("跳过空内容文件: {}", path); + return; + } // 以 relativePath 作为文档 ID GetRequest getRequest = new GetRequest(INDEX_NAME, relativePath); - boolean skip = false; - long localMTime = Files.getLastModifiedTime(path).toMillis(); - + boolean fileHasChanged = false; if (esClient.exists(getRequest, RequestOptions.DEFAULT)) { GetResponse existing = esClient.get(getRequest, RequestOptions.DEFAULT); Map existingSource = existing.getSourceAsMap(); Object esMtime = existingSource.get("mtime"); if (esMtime != null && Long.parseLong(esMtime.toString()) == localMTime) { logger.info("文件未变动,跳过导入: {}", relativePath); - skip = true; + + if(LocalCacheUtil.get(relativePath) == null) { + // 缓存过期 + LocalCacheUtil.put(relativePath, localMTime); + } + return; } else { - // 删除旧版本 - DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, relativePath); - esClient.delete(deleteRequest, RequestOptions.DEFAULT); - logger.info("已删除旧版本文件: {}", relativePath); + fileHasChanged = true; } } - if (skip) return; + if (fileHasChanged) { + // 删除旧版本 + DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, relativePath); + esClient.delete(deleteRequest, RequestOptions.DEFAULT); + logger.info("已删除旧版本文件: {}", relativePath); + } // === 构建文档 === Map doc = new HashMap<>(); @@ -99,6 +110,8 @@ public abstract class AbstractBaseFileImporter implements DocumentImporter { esClient.index(request, RequestOptions.DEFAULT); logger.info("导入成功: {}", relativePath); + // 信息入缓存 + LocalCacheUtil.put(relativePath, localMTime); } catch (Exception e) { logger.error("导入失败: {}", path, e); } diff --git a/src/main/java/com/doc/parser/infrastructure/north/controller/LoginController.java b/src/main/java/com/doc/parser/infrastructure/north/controller/LoginController.java index bc980ab..17c3ea7 100644 --- a/src/main/java/com/doc/parser/infrastructure/north/controller/LoginController.java +++ b/src/main/java/com/doc/parser/infrastructure/north/controller/LoginController.java @@ -15,8 +15,8 @@ import java.util.concurrent.ConcurrentHashMap; public class LoginController { private static final Map USER_DB = Map.of( - "admin", "123456", - "user", "abc123" + "admin", "Admin@654321", + "lukeye", "lukeye" ); // token -> timestamp