最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

节点redis

IT培训 admin 4浏览 0评论

节点redis

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

  1. incr userId
  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
});
回答如下:

您不能在同一事务中使用对事务的操作的回复,但在您的情况下也不需要 - INCR操作是原子操作并保证返回无竞争的唯一值。

节点redis

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

  1. incr userId
  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
});
回答如下:

您不能在同一事务中使用对事务的操作的回复,但在您的情况下也不需要 - INCR操作是原子操作并保证返回无竞争的唯一值。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论