应用程序的并发处理与用户认证
1. 并发处理
在处理并发问题时,有多种解决方案,如使用锁机制和原子操作。
1.1 MongoDB 中的并发处理
以下是一个使用锁机制的示例代码:
// Our processing function
function processCall(cb) {
// Add delay of 5 seconds here - imitating processing of the request
setTimeout(cb, 5000);
}
function aquireLock(name, cb) {
var now = Date.now();
var expired = now - 60 * 1000;
// The basics of this command is that we either:
// 1. Find an old lock and update it with a new timestamp
// 2. Don't find one, in which case we try to insert
// This will either:
// 2.1 fail, because of unique index - a lock exists
// 2.2 succeeds - a new lock is created
Lock.findOneAndUpdate({
name: name,
timestamp: {$lt: ex
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



