如何在使用 node-redis 进行 HSCAN 时返回缓冲区?

huangapple go评论58阅读模式
英文:

How to return buffers using node-redis when doing HSCAN?

问题

大多数命令似乎允许使用 { returnBuffers: true } 返回缓冲区。
然而,对于各种扫描方法似乎不起作用。特别是,我试图使用 hScanIterator

是否有办法在使用 hScanIterator 时返回缓冲区?

英文:

Seems most commands allow for buffers to be returned using { returnBuffers: true }
This however doesn't seem to work for the various scan methods. Particularly, I'm trying to use hScanIterator.

Is there any way to return buffers using hScanIterator?

答案1

得分: 1

我深入研究了代码,似乎不支持它。 话虽如此,我认为应该支持。 我会提出一个问题并要求支持。

作为一种解决方法,返回的字符串可以解析为字节。 它们使用C样式的转义语法。 我尝试在Node.js中找到一个可以将它们转换为字节的库,但没有找到,但肯定不难编写一个。 Redis中转义这些字符串的代码(至少我认为这是代码)向您展示了在字符串中需要查找和转换的内容。

也许不是您希望得到的答案,但在添加该功能之前,这可能是最好的答案了。 😃

英文:

I did a spelunk into the code and it looks like it doesn't support it. That said, I think it should. I'd open an issue and ask for it.

As a workaround, the strings that are returned are parsable to bytes. They use a C-style escaping syntax. I tried to find a library for Node.js that would convert them to bytes to no avail but it certainly wouldn't be hard to write one. The code that escapes these string in Redis (at least I think this is the code) shows you what you would need to look for and convert in the string.

Perhaps not the answer you were hoping for, but probably as good as it will get until the feature is added. 😉

答案2

得分: 0

内置的扫描迭代器方法不支持 returnBuffer 标志(尽管它们应该支持)。在此期间,您可以自己实现迭代器:

function* hScanBufferIterator(client, key, options) {
  let cursor = 0;
  do {
    const reply = await client.hScan(
      client.commandOptions({ returnBuffers: true }),
      key,
      cursor,
      options
    );
    cursor = reply.cursor;
    for (const tuple of reply.tuples) {
      yield tuple;
    }
  } while (cursor !== 0);
}
英文:

The built-in scan iterator methods does not support the returnBuffer flag (although they should). In the mean time you can implement the iterator youself:

function* hScanBufferIterator(client, key, options) {
  let cursor = 0;
  do {
    const reply = await client.hScan(
      client.commandOptions({ returnBuffers: true }),
      key,
      cursor,
      options
    );
    cursor = reply.cursor;
    for (const tuple of reply.tuples) {
      yield tuple;
    }
  } while (cursor !== 0);
}

huangapple
  • 本文由 发表于 2023年1月9日 03:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定