var users = {}; function onLogin(uid){ var user = DBMgr.read(uid); users[uid] = user; } function onLogout(uid){ var user = users[uid]; DBMgr.write(user); delete users[uid]; }
function onTick(){ for (var uid in users) { var user = users[uid]; DBMgr.write(user); } }
var users = {}; var cache = {}; function onLogin(uid){ if (!!cache[uid]) { users[uid] = cache[uid]; delete cache[uid]; } else { users[uid] = DBMgr.read(uid); } } function onLogout(uid){ var user = users[uid]; cache[uid] = user; delete users[uid]; } function onTick(){ for (var uid in cache) { var user = cache[uid]; DBMgr.write(user); delete cache[uid]; } }
var users = {}; var cache = {}; function onLogin(uid){ var user = users[uid]; if (!user) { user = DBMgr.read(uid); users[uid] = user; cache[uid] = curTime + WRITE_GAP;// WRITE_GAP为存盘间隔时间 } } function onLogout(uid){ cache[uid] = 0 - (curTime + WRITE_GAP); } function onTick() { for (var uid in cache) { var time = cache[uid]; if (curTime < Math.abs(time)) { continue; } DBMgr.write(users[uid]); if (time < 0) { // 离线玩家 delete users[uid]; delete cache[uid]; } else { // 在线玩家 cache[uid] = curTime + WRITE_GAP; } } };
这样的结构定时存盘一批玩家数据,即使玩家离线也可以在内存中保存一段时间。
目前我们的服务端存盘机制就是这样的,如果以后有优化再补充。