From 9c6f8f8b82b35a0e8e489cf83f8ea9c05f36fa6a Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 11 Oct 2016 12:06:11 +0800 Subject: [PATCH] add sharedata.flush() to collect old version immediately --- lualib/sharedata.lua | 15 +++++++++++++++ lualib/sharedata/corelib.lua | 6 +++++- service/sharedatad.lua | 27 ++++++++++++++++++++------- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lualib/sharedata.lua b/lualib/sharedata.lua index 93ead44d..5526b327 100644 --- a/lualib/sharedata.lua +++ b/lualib/sharedata.lua @@ -8,6 +8,7 @@ skynet.init(function() end) local sharedata = {} +local cache = setmetatable({}, { __mode = "kv" }) local function monitor(name, obj, cobj) local newobj = cobj @@ -18,13 +19,20 @@ local function monitor(name, obj, cobj) end sd.update(obj, newobj) end + if cache[name] == obj then + cache[name] = nil + end end function sharedata.query(name) + if cache[name] then + return cache[name] + end local obj = skynet.call(service, "lua", "query", name) local r = sd.box(obj) skynet.send(service, "lua", "confirm" , obj) skynet.fork(monitor,name, r, obj) + cache[name] = r return r end @@ -40,4 +48,11 @@ function sharedata.delete(name) skynet.call(service, "lua", "delete", name) end +function sharedata.flush() + for name, obj in pairs(cache) do + sd.flush(obj) + end + collectgarbage() +end + return sharedata diff --git a/lualib/sharedata/corelib.lua b/lualib/sharedata/corelib.lua index 481e85fa..08d6dd3d 100644 --- a/lualib/sharedata/corelib.lua +++ b/lualib/sharedata/corelib.lua @@ -128,4 +128,8 @@ function conf.update(self, pointer) core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) end -return conf \ No newline at end of file +function conf.flush(obj) + getcobj(obj) +end + +return conf diff --git a/service/sharedatad.lua b/service/sharedatad.lua index 3f86155b..9888216b 100644 --- a/service/sharedatad.lua +++ b/service/sharedatad.lua @@ -8,6 +8,7 @@ local NORET = {} local pool = {} local pool_count = {} local objmap = {} +local collect_tick = 600 local function newobj(name, tbl) assert(pool[name] == nil) @@ -19,17 +20,28 @@ local function newobj(name, tbl) pool_count[name] = { n = 0, threshold = 16 } end +local function collect10sec() + if collect_tick > 10 then + collect_tick = 10 + end +end + local function collectobj() while true do - skynet.sleep(600 * 100) -- sleep 10 min - collectgarbage() - for obj, v in pairs(objmap) do - if v == true then - if sharedata.host.getref(obj) <= 0 then - objmap[obj] = nil - sharedata.host.delete(obj) + skynet.sleep(100) -- sleep 1s + if collect_tick <= 0 then + collect_tick = 600 -- reset tick count to 600 sec + collectgarbage() + for obj, v in pairs(objmap) do + if v == true then + if sharedata.host.getref(obj) <= 0 then + objmap[obj] = nil + sharedata.host.delete(obj) + end end end + else + collect_tick = collect_tick - 1 end end end @@ -109,6 +121,7 @@ function CMD.update(name, t, ...) response(true, newobj) end end + collect10sec() -- collect in 10 sec end local function check_watch(queue)