local skynet = require "skynet" require "skynet.manager" local spawn = require "model.spawn" local character = { } function character.loadfrom(metadata) local usercenter = skynet.localname(".usercenter") local skynet_call = skynet.call local skynet_sleep = skynet.sleep local trigger = {} local t = spawn().loadfrom(metadata) rawset(t, "persist", function(k, v) trigger[k] = v end) local k = assert(t.uid) local function flushall() -- Execute all asynchronous setter local tmp = trigger trigger = {} for _, f in pairs(tmp) do f() end -- Serialize dirty data to array local pkg = t.pack(k) if pkg then -- Save data to database skynet_call(usercenter, "lua", "batch_save", pkg) end end character.flushall = flushall setmetatable(character, t) skynet.fork(function() while true do flushall() skynet_sleep(300) end end) return character end return character