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());
    }
相关推荐
KmSH8umpK8 分钟前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第六篇
数据库·redis·分布式
梁萌30 分钟前
mysql使用事件做日志表数据转移
数据库·mysql
lThE ANDE31 分钟前
MySQL中的TRUNCATE TABLE命令
数据库·mysql
kexnjdcncnxjs35 分钟前
Redis如何记录每一次写操作_开启AOF持久化机制实现命令级追加记录
jvm·数据库·python
逸Y 仙X1 小时前
文章十九: ElasticSearch Full Text 全文本查询
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
STER labo1 小时前
mysql配置环境变量——(‘mysql‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件解决办法)
数据库·mysql·adb
微软技术分享1 小时前
本地部署千问 2.5-1.5B-GGUF + LangChain 封装学习
数据库·学习·langchain
七夜zippoe1 小时前
DolphinDB分区策略:VALUE分区详解
数据库·oracle·分区·value·dolphindb
rKWP8gKv71 小时前
数据库连接池选型:HikariCP与Druid的性能对比
数据库
dreamZhanglx1 小时前
MySQL进阶
数据库·mysql