1
This commit is contained in:
@@ -35,6 +35,15 @@ public interface IInstructionService {
|
||||
* @return 历史消息列表
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -142,12 +142,16 @@ public class InstructionServiceImpl implements IInstructionService {
|
||||
|
||||
@Override
|
||||
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) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
// 确定Redis键
|
||||
String key;
|
||||
if ("request".equalsIgnoreCase(type)) {
|
||||
key = "instruction:request";
|
||||
@@ -157,11 +161,23 @@ public class InstructionServiceImpl implements IInstructionService {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 确定获取数量,默认100条
|
||||
int count = (limit != null && limit > 0) ? Math.min(limit, 1000) : 1000;
|
||||
int maxReturn = (limit != null && limit > 0) ? Math.min(limit, 1000) : 200;
|
||||
boolean hasKeyword = keyword != null && !keyword.trim().isEmpty();
|
||||
String kwLower = hasKeyword ? keyword.trim().toLowerCase() : null;
|
||||
|
||||
// 从Redis获取历史消息(索引0到count-1)
|
||||
List<String> messages = stringRedisTemplate.opsForList().range(key, 0, count - 1);
|
||||
List<String> messages;
|
||||
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();
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user