REDIS通过UNIQUE哈希读取多个有序列表- NODEJS

7d7tgy0s  于 8个月前  发布在  Redis
关注(0)|答案(1)|浏览(69)

我有一个名为personells的数组,其中包含一个名为mgr_wrkpersonnel_empid的密钥。现在,当我循环访问personell时,我需要使用mgr_wrkpersonnel_empid作为我唯一的哈希值来迭代地检索每个personell的整个列表,并将它们推送到一个名为schedule的新数组中,然后在填充每个emp_id的列表时,以某种方式将每个列表Map到其各自的emp_id。
示例代码尝试做上述的一半,请注意,我已经写了redis,但我需要检索所有数据,以给予一致性,我的应用程序。这是必要的,因为我需要在Map上的速度。

let schedules = [];
personells.forEach((personell) => {
    console.log("personell", personell.dataValues.mgr_wrkpersonnels_empid);
    // Use Redis callback to retrieve the list
    client.lRange(`user:${personell.dataValues.mgr_wrkpersonnels_empid}`, 0, -1, (err, reply) => {
      schedules.push(reply);
      console.log("reply", reply);
      if (err) {
        console.error('Redis Error:', err);
        res.status(500).json({ error: 'Error retrieving keys from Redis' });
      }
    });
  });

犯这个错误

GET /scheduler?mgr_wrkpersonnels_designation=2 304 17.748 ms - -
/home/hub/GBHL/node_modules/@redis/client/dist/lib/client/index.js:490
        return Promise.reject(new errors_1.ClientClosedError());
                              ^

ClientClosedError: The client is closed
    at Commander._RedisClient_sendCommand (/home/hub/GBHL/node_modules/@redis/client/dist/lib/client/index.js:490:31)
    at Commander.commandsExecutor (/home/hub/GBHL/node_modules/@redis/client/dist/lib/client/index.js:188:154)
    at BaseClass.<computed> [as lRange] (/home/hub/GBHL/node_modules/@redis/client/dist/lib/commander.js:8:29)
    at /home/hub/GBHL/workforceAPI/workforce-api/routes/scheduler.js:94:12
    at Array.forEach (<anonymous>)
    at /home/hub/GBHL/workforceAPI/workforce-api/routes/scheduler.js:91:14
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.16.1
[nodemon] app crashed - waiting for file changes before starting...

我试着用cli获取redis上的文件,我得到了所有的列表,没有问题。
这是一个lrange请求的示例

127.0.0.1:6379> lrange user:G1257 0 -1
1) "{\"id\":2,\"start\":\"2023-08-05T00:00:00\",\"end\":\"2023-08-12T00:00:00\"}"
2) "{\"id\":4,\"start\":\"2023-08-14T00:00:00\",\"end\":\"2023-08-19T00:00:00\"}"
127.0.0.1:6379>

emp ID采用格式Gundi,其中x是一个数字,因此我有几个用户的记录包含ID,开始,结束值。我想做的是提取一个给定用户的所有记录,通过他们的empid作为redis散列,采用user:empid的形式

bksxznpy

bksxznpy1#

我使用了multi exec,这是nodejs中的一个管道解决方案,可以让你使用redis Commander执行批处理命令。
下面是我的get请求

router.get('/', async (req, res) => {

  const lRangeAsync = promisify(client.lRange).bind(client);

  try {
    const personells = await Personnel.findAll({
      where: {
        mgr_wrkpersonnels_designation: req.query.mgr_wrkpersonnels_designation,
      },
    });

    const multi = client.multi();
    personells.forEach((personell) => {
      const resource = personell.dataValues.mgr_wrkpersonnels_empid;
      multi.lRange(`user:${resource}`, 0, -1);
    });

    // Execute the multi command
    const results = await multi.exec();

    // Process the results and create the final object with lists for each resource
    const allLists = [];
    results.forEach((result, index) => {
      //add empid to the result

      const resource = personells[index].dataValues.mgr_wrkpersonnels_empid;
      result = result.map((item) => {
        item = JSON.parse(item);
        item.resource = resource;
        return JSON.stringify(item);
      });

      allLists.push(result.map(JSON.parse));
    });

    console.log('Lists for each resource:', allLists);
    return res.json(allLists);
  } catch (error) {
    console.error('Error:', error);
    res.status(500).json({ error: 'Error retrieving data' });
  } finally {
    // Always quit the client to close the connection
  
  }
});

这是我的结果,非常顺利。我甚至有多个条目的散列。

Lists for each resource: [
  [
    {
      id: 2,
      start: '2023-08-05T00:00:00',
      end: '2023-08-12T00:00:00',
      resource: 'G1257'
    },
    {
      id: 4,
      start: '2023-08-14T00:00:00',
      end: '2023-08-19T00:00:00',
      resource: 'G1257'
    }
  ],
  [
    {
      id: 2,
      start: '2023-08-07T00:00:00',
      end: '2023-08-08T00:00:00',
      resource: 'G1189'
    }
  ],
  [
    {
      id: 2,
      start: '2023-08-07T00:00:00',
      end: '2023-08-15T00:00:00',
      resource: 'G1258'
    },
    {
      id: 3,
      start: '2023-08-04T00:00:00',
      end: '2023-08-10T00:00:00',
      resource: 'G1258'
    },
    {
      id: 3,
      start: '2023-08-10T00:00:00',
      end: '2023-08-17T00:00:00',
      resource: 'G1258'
    },
    {
      id: 4,
      start: '2023-08-18T00:00:00',
      end: '2023-08-23T00:00:00',
      resource: 'G1258'
    }
  ],
  [
    {
      id: 3,
      start: '2023-08-04T00:00:00',
      end: '2023-08-10T00:00:00',
      resource: 'G1162'
    },
    {
      id: 2,
      start: '2023-08-13T00:00:00',
      end: '2023-08-14T00:00:00',
      resource: 'G1162'
    }
  ],
  [
    {
      id: 2,
      start: '2023-08-09T00:00:00',
      end: '2023-08-16T00:00:00',
      resource: 'G1110'
    }
  ],
  [
    {
      id: 2,
      start: '2023-08-05T00:00:00',
      end: '2023-08-10T00:00:00',
      resource: 'G1262'
    }
  ]
]
GET /scheduler?mgr_wrkpersonnels_designation=1 200 6.069 ms - 959

忽略非唯一ID。这些只是对另一张table的引用。SHift ID,我用它来填充和呈现前端的一些信息。

相关问题