2017-12-28 14 views
1

複数のプロセスが私のredisストアにアクセスできます。新しいユーザーハッシュを追加するとき、私は次の手順を実行します。ノードredis - id生成競合状態

  1. INCRユーザーID
  2. セットのユーザー:私はにこれらの手順をバンドルすることができますどのように[インクリメントのuserId] ...

トランザクション?

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 
}); 

答えて

0

あなたは同じトランザクション内でトランザクションの操作からの応答を使用することはできませんが、そのために必要があなたのケースでもありません - INCR操作はレースフリーを返すために、原子と保証されています一意の値。