代码之家  ›  专栏  ›  技术社区  ›  nagy.zsolt.hun

节点redis-id生成竞争条件

  •  1
  • nagy.zsolt.hun  · 技术社区  · 7 年前

    多个进程可以访问我的redis商店。添加新 使用者 哈希,我执行以下步骤:

    1. 增量用户ID
    2. 设置用户:[递增的用户ID]。。。

    如何将这些步骤捆绑到事务中?

    const client = require('redis').createClient();
    
    client.on("connect", () => {
        const multi = client.multi();
        multi.incr("userId", (userId) => {
            console.log("new userId is %s", userId);    // TODO userId should not be null
            multi.set("user:"+userId, {name:"UserName"} );
        });
        multi.exec();    // TODO after the execution I expect to see the key user:null using redis-cli, but it does not exist
    });
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Itamar Haber    7 年前

    您不能在同一事务内的事务上使用来自操作的回复,但在您的情况下也不需要这样做-在 INCR 操作是原子的,并保证返回一个无竞争的唯一值。