使用ioredis

时间:2019-02-18 15:46:59

标签: node.js redis ioredis

我将ioredis客户端(@ 4.6.2)与node.js结合使用,我需要进行很多位操作(彼此之间不依赖)。像这样:

import * as ioredis from "ioredis";

...

private readonly client: ioredis.Redis;
this.client = new ioredis("my_url");

...

await this.client.send_command("BITOP", "OR", "a_or_b", "a", "b");
await this.client.send_command("BITOP", "OR", "a_or_c", "a", "c");
await this.client.send_command("BITOP", "OR", "a_or_d", "a", "d");
await this.client.send_command("BITOP", "OR", "a_or_e", "a", "e");
// etc...

通过其他一些操作(例如setbit),我可以使用 pipeline 对象及其exec()函数以原子方式运行它们:

const pipeline: Pipeline = this.client.pipeline();
pipeline.setbit(a, 1, 1);
pipeline.setbit(a, 12, 0);
pipeline.setbit(b, 3,  1);
await pipeline.exec();

但是我找不到任何pipeline.bitop()pipeline.send_command()函数。

是否可以通过原子操作发送这些BITOP命令?谢谢

1 个答案:

答案 0 :(得分:0)

我终于设法做到了,使用命令数组作为构造函数的参数(如ioredis文档中所述),而且速度更快!

const result: number[][] = await this.redis.pipeline([
      ["bitop", "OR", "a_or_b", "a", "b"],
      ["bitop", "OR", "a_or_c", "a", "c"],
      ["bitop", "OR", "a_or_d", "a", "d"],
      ...

      ["bitcount", "a_or_b"],
      ["bitcount", "a_or_c"],
      ["bitcount", "a_or_d"],
      ...
]).exec();