This commit is contained in:
van
2026-03-09 15:25:22 +08:00
parent dc8b0b2fcf
commit b4749f3516
3 changed files with 36 additions and 9 deletions

View File

@@ -34,15 +34,17 @@ public class InstructionController extends BaseController {
} }
/** /**
* 获取历史消息记录 * 获取历史消息记录(支持关键词搜索,在全部历史数据中匹配)
* @param type 消息类型request(请求) 或 response(响应) * @param type 消息类型request(请求) 或 response(响应)
* @param limit 获取数量默认100条 * @param limit 获取数量默认100条;有 keyword 时为返回匹配条数上限默认200
* @param keyword 可选,搜索关键词;不为空时在全部数据中过滤后返回
* @return 历史消息列表 * @return 历史消息列表
*/ */
@GetMapping("/history") @GetMapping("/history")
public AjaxResult getHistory(@RequestParam(required = false, defaultValue = "request") String type, public AjaxResult getHistory(@RequestParam(required = false, defaultValue = "request") String type,
@RequestParam(required = false, defaultValue = "100") Integer limit) { @RequestParam(required = false, defaultValue = "100") Integer limit,
java.util.List<String> history = instructionService.getHistory(type, limit); @RequestParam(required = false) String keyword) {
java.util.List<String> history = instructionService.getHistory(type, limit, keyword);
return AjaxResult.success(history); return AjaxResult.success(history);
} }
} }

View File

@@ -35,6 +35,15 @@ public interface IInstructionService {
* @return 历史消息列表 * @return 历史消息列表
*/ */
java.util.List<String> getHistory(String type, Integer limit); java.util.List<String> getHistory(String type, Integer limit);
/**
* 获取历史消息记录(支持关键词搜索,在全部数据中匹配)
* @param type 消息类型request(请求) 或 response(响应)
* @param limit 返回数量上限默认200条
* @param keyword 搜索关键词,为空则按 limit 取最近 N 条
* @return 历史消息列表
*/
java.util.List<String> getHistory(String type, Integer limit, String keyword);
} }

View File

@@ -142,12 +142,16 @@ public class InstructionServiceImpl implements IInstructionService {
@Override @Override
public List<String> getHistory(String type, Integer limit) { public List<String> getHistory(String type, Integer limit) {
return getHistory(type, limit, null);
}
@Override
public List<String> getHistory(String type, Integer limit, String keyword) {
if (stringRedisTemplate == null) { if (stringRedisTemplate == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
try { try {
// 确定Redis键
String key; String key;
if ("request".equalsIgnoreCase(type)) { if ("request".equalsIgnoreCase(type)) {
key = "instruction:request"; key = "instruction:request";
@@ -157,11 +161,23 @@ public class InstructionServiceImpl implements IInstructionService {
return Collections.emptyList(); return Collections.emptyList();
} }
// 确定获取数量默认100条 int maxReturn = (limit != null && limit > 0) ? Math.min(limit, 1000) : 200;
int count = (limit != null && limit > 0) ? Math.min(limit, 1000) : 1000; boolean hasKeyword = keyword != null && !keyword.trim().isEmpty();
String kwLower = hasKeyword ? keyword.trim().toLowerCase() : null;
// 从Redis获取历史消息索引0到count-1 List<String> messages;
List<String> messages = stringRedisTemplate.opsForList().range(key, 0, count - 1); if (hasKeyword) {
// 搜索模式:取全部数据后在内存中按关键词过滤
messages = stringRedisTemplate.opsForList().range(key, 0, -1);
if (messages == null) messages = Collections.emptyList();
messages = messages.stream()
.filter(msg -> msg != null && msg.toLowerCase().contains(kwLower))
.limit(maxReturn)
.collect(Collectors.toList());
} else {
// 普通模式:只取最近 N 条
messages = stringRedisTemplate.opsForList().range(key, 0, maxReturn - 1);
}
return messages != null ? messages : Collections.emptyList(); return messages != null ? messages : Collections.emptyList();
} catch (Exception e) { } catch (Exception e) {