所谓雪崩问题,是在缓存失效的情景下,大并发高访问量同时涌入数据库中查询, 数据库无法同时承受如此大的查询请求, 往前影响到网站整体响应缓慢。
Created
January 16, 2012 04:00
-
-
Save JacksonTian/1618990 to your computer and use it in GitHub Desktop.
避免重复调用,从容应对雪崩问题
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
var emitter = new event.Emitter(); | |
var status = "ready"; | |
var select = function (callback) { | |
emitter.once("selected", function (results) { | |
callback(results); | |
}); | |
if (status === "ready") { | |
status = "pending"; | |
db.select("SQL", function (results) { | |
emitter.emit("selected", results); | |
status = "ready"; | |
}); | |
} | |
}; | |
select(function (results) { | |
// TODO | |
}); | |
select(function (results) { | |
// TODO | |
}); | |
select(function (results) { | |
// TODO | |
}); |
雪崩问题常规解决思路是允许一定数量的请求访问数据库;或者让一个请求入数据库回写缓存让其它请求等待。刚才我看错了,这个代码没问题。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请使用setTimeout来模拟,让代码能跑起来,更有说服力。