From 92b3853869a4db1329712718fa2ddd37784b1447 Mon Sep 17 00:00:00 2001 From: firefly <1633489380@qq.com> Date: Tue, 19 Aug 2025 22:54:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E9=A2=91=E7=B9=81?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E5=8E=86=E5=8F=B2=E8=81=8A=E5=A4=A9=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E9=A2=91=E7=B9=81=E8=B0=83=E7=94=A8dify?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E4=BC=9A=E8=AF=9Did=E5=91=A8=E6=9C=9F=E6=80=A7?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=9C=A8Redis=E4=B8=AD=EF=BC=8C=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E5=90=91dify=E5=8F=91=E9=80=81=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/aitutor/AiChatController.java | 126 +++++++++++++++--- 1 file changed, 109 insertions(+), 17 deletions(-) diff --git a/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java b/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java index 3eef491..7b3280e 100644 --- a/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java +++ b/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java @@ -16,6 +16,8 @@ import okhttp3.*; import com.srs.common.core.domain.AjaxResult; // ✅ RuoYi 的返回结果类 import okhttp3.RequestBody; import okhttp3.ResponseBody; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -83,6 +85,16 @@ public class AiChatController extends BaseController { */ private static final String DIFY_FILES_URL = "http://47.112.118.149:8100/v1/files/upload"; + /** + * Redis中会话ID的key前缀 + */ + private static final String CONVERSATION_ID_CACHE_PREFIX = "dify:conversation:id:"; + + /** + * Redis中会话ID的过期时间(小时) + */ + private static final int CONVERSATION_ID_CACHE_EXPIRE_HOURS = 1; + /** * HTTP客户端实例 * 配置了5分钟的读取超时时间,用于与Dify API进行通信 @@ -94,6 +106,12 @@ public class AiChatController extends BaseController { .retryOnConnectionFailure(true) .build(); + /** + * Redis模板,用于缓存会话ID + */ + @Autowired + private StringRedisTemplate stringRedisTemplate; + /** 为本次请求设置 “总超时”(含连接/读写),避免无限挂起 */ private Response execWithTimeouts(Request req, int callSecs, int readSecs, int writeSecs) throws IOException { OkHttpClient shortClient = client.newBuilder() @@ -486,19 +504,29 @@ public class AiChatController extends BaseController { @PreAuthorize("@ss.hasPermi('cph:teacher:list')") @GetMapping("/getMessagesToAdmin") public AjaxResult getMessagesToAdmin(@RequestParam String user, - @RequestParam(required = false) String firstId, - @RequestParam(defaultValue = "20") int limit, - @RequestParam(defaultValue = "-updated_at") String sortBy) { + @RequestParam(required = false) String firstId, + @RequestParam(defaultValue = "20") int limit, + @RequestParam(defaultValue = "-updated_at") String sortBy) { try { - List conversations = getConversations(user); + // 首先尝试从Redis缓存中获取会话ID + String conversationId = getCachedConversationId(user); - if (conversations == null || conversations.isEmpty()) { - return AjaxResult.error("暂无会话记录"); + // 如果Redis缓存中没有会话ID,则使用原来的方法获取 + if (conversationId == null) { + List conversations = getConversations(user); + + if (conversations == null || conversations.isEmpty()) { + return AjaxResult.error("暂无会话记录"); + } + + conversationId = conversations.get(0).getId(); + + // 将获取到的会话ID缓存到Redis中 + cacheConversationId(user, conversationId); } - String conversation_id = conversations.get(0).getId(); - Map result = getConversationHistoryMessages(conversation_id, user, firstId, limit); + Map result = getConversationHistoryMessages(conversationId, user, firstId, limit); AjaxResult successResult = success(result); return successResult; @@ -514,21 +542,30 @@ public class AiChatController extends BaseController { // 权限标识为学生 @GetMapping("/getMessagesToUser") public AjaxResult getMessagesToUser(@RequestParam(required = false) String firstId, - @RequestParam(defaultValue = "20") int limit, - @RequestParam(defaultValue = "-updated_at") String sortBy) { + @RequestParam(defaultValue = "20") int limit, + @RequestParam(defaultValue = "-updated_at") String sortBy) { try { String user = SecurityUtils.getLoginUser().getUsername(); - List conversations = getConversations(user); + // 首先尝试从Redis缓存中获取会话ID + String conversationId = getCachedConversationId(user); - if (conversations == null || conversations.isEmpty()) { - return AjaxResult.error("暂无会话记录"); + // 如果Redis缓存中没有会话ID,则使用原来的方法获取 + if (conversationId == null) { + List conversations = getConversations(user); + + if (conversations == null || conversations.isEmpty()) { + return AjaxResult.error("暂无会话记录"); + } + + conversationId = conversations.get(0).getId(); + + // 将获取到的会话ID缓存到Redis中 + cacheConversationId(user, conversationId); } - String conversation_id = conversations.get(0).getId(); - - Map result = getConversationHistoryMessages(conversation_id, user, firstId, limit); + Map result = getConversationHistoryMessages(conversationId, user, firstId, limit); AjaxResult successResult = success(result); return successResult; @@ -541,6 +578,62 @@ public class AiChatController extends BaseController { } } + /** + * 从Redis缓存中获取用户的会话ID + * + * @param user 用户名 + * @return 会话ID,如果不存在则返回null + */ + private String getCachedConversationId(String user) { + try { + // 参数校验 + if (user == null || user.trim().isEmpty()) { + return null; + } + + user = user.trim(); + String cacheKey = CONVERSATION_ID_CACHE_PREFIX + user; + + // 从Redis中获取会话ID + String conversationId = stringRedisTemplate.opsForValue().get(cacheKey); + + return conversationId; + } catch (Exception e) { + return null; + } + } + + /** + * 将会话ID与用户绑定并存储到Redis中 + * + * @param user 用户名 + * @param conversationId 会话ID + */ + private void cacheConversationId(String user, String conversationId) { + try { + // 参数校验 + if (user == null || user.trim().isEmpty()) { + return; + } + + if (conversationId == null || conversationId.trim().isEmpty()) { + return; + } + + user = user.trim(); + conversationId = conversationId.trim(); + + // 将用户与会话ID绑定存储到Redis中 + String cacheKey = CONVERSATION_ID_CACHE_PREFIX + user; + stringRedisTemplate.opsForValue().set( + cacheKey, + conversationId, + Duration.ofHours(CONVERSATION_ID_CACHE_EXPIRE_HOURS)); + } catch (Exception e) { + System.out.println("绑定会话ID时发生错误: " + e.getMessage()); + } + } + /* * 获取会话列表 */ @@ -570,7 +663,6 @@ public class AiChatController extends BaseController { // 读取响应体 String responseBodyString = response.body().string(); - System.out.println("Raw Response: " + responseBodyString); // 打印原始数据 JsonNode rootNode = mapper.readTree(responseBodyString); // 提取 data 数组