feat(note):优化笔记详情查询性能
- 引入 CompletableFuture 实现 RPC 调用异步化 - 并行调用用户服务与内容服务提升响应速度 - 使用 allOf 统一处理多个异步任务结果 - 保留原有缓存逻辑及异常处理机制 - 调整代码结构提高可读性和维护性
This commit is contained in:
@@ -39,6 +39,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@@ -167,6 +168,7 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Response<FindNoteDetailRspVO> findNoteDetail(FindNoteDetailReqVO findNoteDetailReqVO) {
|
||||
// 查询笔记ID
|
||||
Long noteId = findNoteDetailReqVO.getId();
|
||||
@@ -224,13 +226,22 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
|
||||
|
||||
// RPC调用用户服务,获取用户信息
|
||||
Long creatorId = noteDO.getCreatorId();
|
||||
FindUserByIdRspDTO findUserByIdRspDTO = userRpcService.findById(creatorId);
|
||||
CompletableFuture<FindUserByIdRspDTO> userResultFuture = CompletableFuture
|
||||
.supplyAsync(() -> userRpcService.findById(creatorId), threadPoolTaskExecutor);
|
||||
|
||||
// RPC: 调用 K-V 存储服务获取内容
|
||||
String content = null;
|
||||
CompletableFuture<String> contentResultFuture = CompletableFuture.completedFuture(null);
|
||||
if (Objects.equals(noteDO.getIsContentEmpty(), Boolean.FALSE)) {
|
||||
content = keyValueRpcService.findNoteContent(noteDO.getContentUuid());
|
||||
contentResultFuture = CompletableFuture
|
||||
.supplyAsync(() -> keyValueRpcService.findNoteContent(noteDO.getContentUuid()), threadPoolTaskExecutor);
|
||||
}
|
||||
CompletableFuture<String> finalContentResultFuture = contentResultFuture;
|
||||
CompletableFuture<FindNoteDetailRspVO> resultFuture = CompletableFuture
|
||||
.allOf(userResultFuture, contentResultFuture)
|
||||
.thenApply(s -> {
|
||||
// 获取 Future 返回的结果
|
||||
FindUserByIdRspDTO findUserByIdRspDTO = userResultFuture.join();
|
||||
String content = finalContentResultFuture.join();
|
||||
|
||||
// 笔记类型
|
||||
Integer noteType = noteDO.getType();
|
||||
@@ -245,7 +256,7 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
|
||||
}
|
||||
|
||||
// 构建返参 VO 实体类
|
||||
FindNoteDetailRspVO findNoteDetailRspVO = FindNoteDetailRspVO.builder()
|
||||
return FindNoteDetailRspVO.builder()
|
||||
.id(noteDO.getId())
|
||||
.type(noteDO.getType())
|
||||
.title(noteDO.getTitle())
|
||||
@@ -260,6 +271,11 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
|
||||
.updateTime(noteDO.getUpdateTime())
|
||||
.visible(noteDO.getVisible())
|
||||
.build();
|
||||
|
||||
});
|
||||
|
||||
// 获取拼装后的 FindNoteDetailRspVO
|
||||
FindNoteDetailRspVO findNoteDetailRspVO = resultFuture.get();
|
||||
// 异步线程中将笔记详情存入 Redis
|
||||
threadPoolTaskExecutor.submit(() -> {
|
||||
String noteDetailJson1 = JsonUtils.toJsonString(findNoteDetailRspVO);
|
||||
@@ -272,6 +288,7 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
|
||||
|
||||
/**
|
||||
* 校验笔记的可见性
|
||||
*
|
||||
* @param visible 是否可见
|
||||
* @param userId 当前用户 ID
|
||||
* @param creatorId 笔记创建者
|
||||
@@ -285,6 +302,7 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
|
||||
|
||||
/**
|
||||
* 校验笔记的可见性(针对 VO 实体类)
|
||||
*
|
||||
* @param userId 当前用户 ID
|
||||
* @param findNoteDetailRspVO 笔记详情VO类
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user