Merge branch 'tptube-cache-v2' into 'master'

add caching layer to teleport tube

See merge request VanessaE/pipeworks!46
This commit is contained in:
Vanessa Dannenberg 2021-06-01 17:17:34 +00:00
commit cec6049dd1

View File

@ -4,6 +4,9 @@ local filename=minetest.get_worldpath() .. "/teleport_tubes"
local tp_tube_db = nil -- nil forces a read local tp_tube_db = nil -- nil forces a read
local tp_tube_db_version = 2.0 local tp_tube_db_version = 2.0
-- cached rceiver list: hash(pos) => {receivers}
local cache = {}
local function hash(pos) local function hash(pos)
return string.format("%.30g", minetest.hash_node_position(pos)) return string.format("%.30g", minetest.hash_node_position(pos))
end end
@ -18,6 +21,8 @@ local function save_tube_db()
else else
error(err) error(err)
end end
-- reset tp-tube cache
cache = {}
end end
local function migrate_tube_db() local function migrate_tube_db()
@ -101,6 +106,12 @@ local function read_node_with_vm(pos)
end end
local function get_receivers(pos, channel) local function get_receivers(pos, channel)
local hash = minetest.hash_node_position(pos)
if cache[hash] then
-- re-use cached result
return cache[hash]
end
local tubes = tp_tube_db or read_tube_db() local tubes = tp_tube_db or read_tube_db()
local receivers = {} local receivers = {}
local dirty = false local dirty = false
@ -121,6 +132,8 @@ local function get_receivers(pos, channel)
if dirty then if dirty then
save_tube_db() save_tube_db()
end end
-- cache the result for next time
cache[hash] = receivers
return receivers return receivers
end end