Created
December 30, 2022 12:24
-
-
Save brownsoo/4f9fcf588df06f84e7175a32ae298f9d to your computer and use it in GitHub Desktop.
캐시 구현 목업 from Node.js 하이 퍼포먼스
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from Node.js 하이 퍼포먼스 | |
var users = {}; | |
function getUser(id, next) { | |
if (users.hasOwnPropoerty(id)) { | |
if (users[id].hasOwnProperty("data")) { | |
return next(null, users[id].data); | |
} | |
// 아직 값이 없음 | |
return users[id].queue.push(next); | |
} | |
users[id] = { | |
queue: [next] | |
}; | |
userDb.findOne({id: id}, function (err, user){ | |
if (err) return next(err); | |
users[id].data = user; | |
users[id].queue.map(function (cb){ | |
cb(null, user); | |
}); | |
delete users[id].queue; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment