Redis-2-queryFormat()方法

查询排版状态方法

方法用于查询当前用户排版过的所有文件及其排版结果。它的主要功能是从 Redis 中获取用户的历史排版记录,并过滤掉不需要展示的状态(如已导出或已丢弃的文件),最终返回一个排版结果列表。

queryFormat :获取当前用户排版过的所有原文件 sourceNodeIds(根据userId) -- 获取该原文件的所有排版结果 resultNodeIds(根据sourceNodeId) -- 获取每个排版结果的详情itemJson(根据resultNodeId) -- 过滤
详细:从 Redis 的 format:user:{userId} 键中获取用户排版过的原文件 sourceNodeId 列表;遍历每个原文件,通过 format:source:{userId}:{sourceNodeId} 获取其对应的排版结果 resultNodeId;从 format:item:{resultNodeId} 中读取排版结果详情,并反序列化为 FormatResultItem 对象;最终返回过滤后的排版结果列表。

分析:

java 复制代码
String itemJson = stringRedisTemplate.opsForValue().get(FORMAT_ITEM_KEY_PREFIX + resultNodeId);
FormatResultItem item = objectMapper.readValue(itemJson, FormatResultItem.class);

这行代码的作用是从 Redis 中获取的 JSON 字符串 itemJson 反序列化为 FormatResultItem 对象。这个对象包含了排版任务的相关信息,最终会作为排版结果的一部分返回给前端。信息包括:

  1. 排版状态 (status):表示当前排版任务的状态(如排版中、已完成、失败等);
  2. 文件类型 (type):排版后的文件类型(如 PDF、Word 等);
  3. 结果文件 ID (nodeId):排版生成的文件在系统中的唯一标识;
  4. 源文件 ID (sourceNodeId):原始文件的唯一标识;
  5. 文件名 (fileName):排版后文件的名称。
    这些信息会被封装到 FormatResultItem 对象中,并通过 queryFormat 接口返回给前端,供用户查看排版任务的进度和结果。

1. FormatService中:queryFormat()

java 复制代码
public List<FormatResultItem> queryFormat(QueryFormatRequest request) {
        Long userId = SecurityUtils.getCurrentUserId();

        // 1. 获取当前用户排版过的所有原文件 nodeId
        String userKey = FORMAT_USER_KEY_PREFIX + userId;
        Set<String> sourceNodeIds = stringRedisTemplate.opsForSet().members(userKey);
        if (CollectionUtils.isEmpty(sourceNodeIds)) {
            log.info("FormatService##queryFormat-1,当前用户没有排版过的文件,userId={},userKey={}", userId,userKey);
            return new ArrayList<>();
        }

        // 2. 遍历每个原文件,获取其排版结果
        List<FormatResultItem> resultItems = new ArrayList<>();
        for (String sourceNodeId : sourceNodeIds) {
            // 获取该原文件的所有排版结果 nodeId
            String sourceKey = FORMAT_SOURCE_KEY_PREFIX + userId + ":" + sourceNodeId;
            Set<String> resultNodeIds = stringRedisTemplate.opsForSet().members(sourceKey);
            if (CollectionUtils.isEmpty(resultNodeIds)) {
                log.info("FormatService##queryFormat-2,当前用户没有排版过的文件,userId={},sourceNodeId={},sourceKey={}", userId, sourceNodeId, sourceKey);
                continue;
            }

            // 获取每个排版结果的详情
            for (String resultNodeId : resultNodeIds) {
                String itemKey = FORMAT_ITEM_KEY_PREFIX + resultNodeId;
                String itemJson = stringRedisTemplate.opsForValue().get(itemKey);
                if (StringUtils.hasText(itemJson)) {
                    try {
                        FormatResultItem item = objectMapper.readValue(itemJson, FormatResultItem.class);
                        resultItems.add(item);
                    } catch (JsonProcessingException e) {
                        log.error("解析排版结果项失败,resultNodeId={}, itemJson={}", resultNodeId, itemJson, e);
                    }
                }
            }
        }

        // 3. 过滤掉状态为2(导出)和3(丢弃)的数据
        return resultItems.stream()
                .filter(item -> !FormatStatus.shouldFilter(item.getStatus()))
                .collect(Collectors.toList());
    }
相关推荐
这个DBA有点耶4 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G4 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备4 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远4 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
IT大白鼠4 天前
Redis 系列 · 第 01 篇——认知入门:Redis 是什么
redis·nosql
彧azz4 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
2601_962218614 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
全栈弄潮儿²⁰²⁴4 天前
AI Agent 开发实战(30):限流、缓存与成本控制
人工智能·gpt·缓存·agent·限流·agi·成本控制
张洛闻Eren4 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
于平安4 天前
MySQL-触发器
数据库·mysql