add sharedata.flush() to collect old version immediately

This commit is contained in:
Cloud Wu
2016-10-11 12:06:11 +08:00
parent f48eeca667
commit 9c6f8f8b82
3 changed files with 40 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ skynet.init(function()
end) end)
local sharedata = {} local sharedata = {}
local cache = setmetatable({}, { __mode = "kv" })
local function monitor(name, obj, cobj) local function monitor(name, obj, cobj)
local newobj = cobj local newobj = cobj
@@ -18,13 +19,20 @@ local function monitor(name, obj, cobj)
end end
sd.update(obj, newobj) sd.update(obj, newobj)
end end
if cache[name] == obj then
cache[name] = nil
end
end end
function sharedata.query(name) function sharedata.query(name)
if cache[name] then
return cache[name]
end
local obj = skynet.call(service, "lua", "query", name) local obj = skynet.call(service, "lua", "query", name)
local r = sd.box(obj) local r = sd.box(obj)
skynet.send(service, "lua", "confirm" , obj) skynet.send(service, "lua", "confirm" , obj)
skynet.fork(monitor,name, r, obj) skynet.fork(monitor,name, r, obj)
cache[name] = r
return r return r
end end
@@ -40,4 +48,11 @@ function sharedata.delete(name)
skynet.call(service, "lua", "delete", name) skynet.call(service, "lua", "delete", name)
end end
function sharedata.flush()
for name, obj in pairs(cache) do
sd.flush(obj)
end
collectgarbage()
end
return sharedata return sharedata

View File

@@ -128,4 +128,8 @@ function conf.update(self, pointer)
core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) })
end end
return conf function conf.flush(obj)
getcobj(obj)
end
return conf

View File

@@ -8,6 +8,7 @@ local NORET = {}
local pool = {} local pool = {}
local pool_count = {} local pool_count = {}
local objmap = {} local objmap = {}
local collect_tick = 600
local function newobj(name, tbl) local function newobj(name, tbl)
assert(pool[name] == nil) assert(pool[name] == nil)
@@ -19,17 +20,28 @@ local function newobj(name, tbl)
pool_count[name] = { n = 0, threshold = 16 } pool_count[name] = { n = 0, threshold = 16 }
end end
local function collect10sec()
if collect_tick > 10 then
collect_tick = 10
end
end
local function collectobj() local function collectobj()
while true do while true do
skynet.sleep(600 * 100) -- sleep 10 min skynet.sleep(100) -- sleep 1s
collectgarbage() if collect_tick <= 0 then
for obj, v in pairs(objmap) do collect_tick = 600 -- reset tick count to 600 sec
if v == true then collectgarbage()
if sharedata.host.getref(obj) <= 0 then for obj, v in pairs(objmap) do
objmap[obj] = nil if v == true then
sharedata.host.delete(obj) if sharedata.host.getref(obj) <= 0 then
objmap[obj] = nil
sharedata.host.delete(obj)
end
end end
end end
else
collect_tick = collect_tick - 1
end end
end end
end end
@@ -109,6 +121,7 @@ function CMD.update(name, t, ...)
response(true, newobj) response(true, newobj)
end end
end end
collect10sec() -- collect in 10 sec
end end
local function check_watch(queue) local function check_watch(queue)