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

节点redis

运维笔记admin21浏览0评论

节点redis

节点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. 暂无评论