Commit 7893c73d authored by 陈立彬's avatar 陈立彬

会话上下文

parent e4e5c1d9
...@@ -20,6 +20,7 @@ import org.springframework.util.CollectionUtils; ...@@ -20,6 +20,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
...@@ -50,6 +51,10 @@ public class ExamController { ...@@ -50,6 +51,10 @@ public class ExamController {
private static String EXAM_REDIS_SESSION_KEY = "EXAM_MASTER_SESSION:%d:%s"; private static String EXAM_REDIS_SESSION_KEY = "EXAM_MASTER_SESSION:%d:%s";
private static String USER_SESSION_CONTEXT_KEY = "USER_SESSION_CONTEXT:%d:%s:%s";
private static String USER_SESSION_CONTEXT_COUNT = "USER_SESSION_CONTEXT_COUNT";
@GetMapping(value = "/list") @GetMapping(value = "/list")
public String list() { public String list() {
LambdaQueryWrapper<ElehKnowledgeDo> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<ElehKnowledgeDo> queryWrapper = new LambdaQueryWrapper<>();
...@@ -270,4 +275,39 @@ public class ExamController { ...@@ -270,4 +275,39 @@ public class ExamController {
} }
return ApiResponse.fail("no cache data."); return ApiResponse.fail("no cache data.");
} }
@GetMapping(value = "/get_user_session_context")
public ApiResponse<String> getUserSessionContext(@RequestParam(value = "tenant") Integer tenant,
@RequestParam(value = "user_id") String userId,
@RequestParam(value = "session_id") String sessionId,
@RequestParam(value = "query") String query) {
List<String> contextList = Lists.newArrayList();
String redisKey = String.format(USER_SESSION_CONTEXT_KEY, tenant, sessionId, userId);
Long contextCount = redisTemplate.opsForList().size(redisKey);
// 最多获取5条上下文
long maxCount = 3;
String cacheMaxCount = redisTemplate.opsForValue().get(USER_SESSION_CONTEXT_COUNT);
if(StringUtils.isNotEmpty(cacheMaxCount)) {
maxCount = Long.valueOf(cacheMaxCount);
}
contextCount = contextCount > maxCount ? maxCount : contextCount;
if(contextCount > 0) {
contextList = redisTemplate.opsForList().range(redisKey, 0, contextCount-1);
Collections.reverse(contextList);
}
log.info("当前用户ID = {}, 会话历史 = {} ", userId, JSONObject.toJSONString(contextList));
// 存储当前用户提问
redisTemplate.opsForList().leftPush(redisKey, query);
return ApiResponse.ok(JSONObject.toJSONString(contextList));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment