应用程序认证与密码安全全解析
1. 并发处理机制
在处理并发问题时,我们可以采用不同的方法,如使用锁机制和原子操作。下面我们将分别介绍在MongoDB和MySQL中的并发处理方式。
1.1 MongoDB并发处理
在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.find
超级会员免费看
订阅专栏 解锁全文
3万+

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



