redis.pipeline.exec() 是否“重置”管道?

dnl*_*wng 5 redis node.js

我正在尝试使用管道将大约 500k 记录批量插入到 Redis 中,我的代码大致如下

const seedCache = async (
  products: Array<Item>,
  chunkSize,
) => {
  for (const chunk of _.chunk(products, chunkSize)) {
    chunk.forEach((item) => {
      pipeline.set(item.id, item.data);
    });
    await pipeline.exec();
    console.log(pipeline.length);
  }
  redis.quit();
};
Run Code Online (Sandbox Code Playgroud)

本质上,我将 chunkSize 项加载到管道中,然后等待 pipeline.exec() 返回,然后继续。

我预计“console.log(pipeline.length)”每次都会打印“0”,因为它只有在管道刷新到 Redis 后才会运行。但是,我发现 pipeline.length 永远不会重置为 0;相反,它只会不断增长,直到最后其长度等于 products.length。这导致我的机器内存不足,无法处理大型数据集。

有谁知道为什么会发生这种情况?另外,这是否是向 Redis 批量插入记录的正确方法?由于使用 5000 个产品和批量大小 100 运行此脚本只会将 200 个产品插入到缓存中,而它确实成功插入了具有相同批量大小的 1000 个产品的数组。这些文档相当大(~5kB),因此需要以某种方式分批完成。