最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用ioredis在单个原子操作中发送许多BITOP

运维笔记admin14浏览0评论

使用ioredis在单个原子操作中发送许多BITOP

使用ioredis在单个原子操作中发送许多BITOP

我正在使用带有node.js的ioredis客户端(@ 4.6.2),我需要做很多位操作(不依赖于彼此)。像这样的东西:

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),我可以使用管道对象及其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命令?谢谢

回答如下:

我终于设法做到了,使用一组命令作为构造函数的参数(如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();
发布评论

评论列表(0)

  1. 暂无评论