gpt4 book ai didi

node.js - redis 和 watch + multi 允许并发用户

转载 作者:IT王子 更新时间:2023-10-29 06:15:59 25 4
gpt4 key购买 nike

我正在对使用相同电子邮件地址的网络服务的用户注册进行负载测试,同时连接的前 10 个用户将始终注册。

我正在使用 WATCH 和 MULTI,但这似乎不起作用。

我正在调用 save() 来保存用户。

this.insert = function(callback) {
this.preInsert();

created = new Date();
updated = new Date();

// Also with these uncommented it still doesn't work
// Common.client.watch("u:" + this.username);
// Common.client.watch("em:" + this.email);

console.log(ID + " email is locked " + this.email);
Common.client.multi()
.set("u:" + this.username, ID)
.hmset("u:" + ID,
{"username": this.username
,"password": this.password
,"email": this.email
,"payment_plan": payment_plan
,"created": created.getTime()
,"updated": updated.getTime()
,"avatar": this.avatar})
.zadd("u:users", 0, ID)
.sadd("u:emails", this.email)
.set("u:"+ ID + ":stats", 0)
.set("em:" + this.email, ID)
.exec();

this.postInsert();

if (callback != null)
callback(null, this);
}

this.save = function(callback) {
// new user
if (ID == -1) {
var u = this;

Common.client.watch("u:" + this.username);
Common.client.exists("u:" + this.username, function(error, exists) {
// This username already exists
if (exists == 1) {
Common.client.unwatch();
if (callback != null)
callback({code: 100, message: "This username already exists"});
}
else {
Common.client.watch("em:" + u.email);
Common.client.get("em:" + u.email, function(err, emailExists) {
if (emailExists != null) {
Common.client.unwatch();
if (callback != null)
callback({code: 101, message: "This email is already in use"});
}
else {
Common.client.incr("u:nextID", function(error, id) {
if (error) callback(error);
else {
ID = id;
u.insert(callback);
}
});
}
});
}
});
}
// existing user
else {
var u = this;
Common.client.get("em:" + this.email, function(err, emailExists) {
if (emailExists != ID && emailExists) {
if (callback != null) {
callback({code: 101, message: "This email is already in use " + ID + " " + emailExists});
}
}
else {
u.update(callback);
}
});
}
}

输出几乎总是:

1 email is locked test@test.com
2 email is locked test@test.com
3 email is locked test@test.com
4 email is locked test@test.com
5 email is locked test@test.com
6 email is locked test@test.com
7 email is locked test@test.com
8 email is locked test@test.com
9 email is locked test@test.com
10 email is locked test@test.com

我是不是做错了什么,或者redis不能处理那么多的并发。这也是 Common 的定义:

var Common = {
client: redis.createClient(),
...
};

最佳答案

是的!经过一夜的休息,我当然在淋浴时找到了解决方案。

问题是我为整个应用程序使用了一个 redis 线程,所有连接都在该线程上注册了 watch 。当然,这并不表示 key 已被其他客户端修改,因为没有其他客户端。

关于node.js - redis 和 watch + multi 允许并发用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com