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());
    }
相关推荐
不吃香菜学java4 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿4 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb
不知名的老吴4 小时前
Redis的延迟瓶颈:TCP栈开销无法避免
数据库·redis·缓存
YOU OU4 小时前
三大范式和E-R图
数据库
一江寒逸5 小时前
零基础从入门到精通MySQL(上篇):筑基篇——吃透核心概念与基础操作,打通SQL入门第一关
数据库·sql·mysql
@土豆5 小时前
Ubuntu 22.04 运行 Filebeat 7.11.2 崩溃问题分析及解决文档
linux·数据库·ubuntu
专注API从业者5 小时前
淘宝商品详情 API 与爬虫技术的边界:合法接入与反爬策略的技术博弈
大数据·数据结构·数据库·爬虫
Devin~Y5 小时前
高并发电商与AI智能客服场景下的Java面试实战:从Spring Boot到RAG与向量数据库落地
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag
爱码小白5 小时前
MySQL 单表查询练习题汇总
数据库·python·算法
WangJunXiang65 小时前
第09章:PostgreSQL日常维护
数据库·postgresql