Skip to content

Instantly share code, notes, and snippets.

@brownsoo
Created December 30, 2022 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brownsoo/4f9fcf588df06f84e7175a32ae298f9d to your computer and use it in GitHub Desktop.
Save brownsoo/4f9fcf588df06f84e7175a32ae298f9d to your computer and use it in GitHub Desktop.
캐시 구현 목업 from Node.js 하이 퍼포먼스
// 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