Skip to content

Commit

Permalink
Merge branch 'tptube-cache-v2' into 'master'
Browse files Browse the repository at this point in the history
add caching layer to teleport tube

See merge request VanessaE/pipeworks!46
  • Loading branch information
VanessaE committed Jun 1, 2021
2 parents 01f4ea0 + 047718b commit cec6049
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions teleport_tube.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local filename=minetest.get_worldpath() .. "/teleport_tubes"
local tp_tube_db = nil -- nil forces a read
local tp_tube_db_version = 2.0

-- cached rceiver list: hash(pos) => {receivers}
local cache = {}

local function hash(pos)
return string.format("%.30g", minetest.hash_node_position(pos))
end
Expand All @@ -18,6 +21,8 @@ local function save_tube_db()
else
error(err)
end
-- reset tp-tube cache
cache = {}
end

local function migrate_tube_db()
Expand Down Expand Up @@ -101,6 +106,12 @@ local function read_node_with_vm(pos)
end

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 receivers = {}
local dirty = false
Expand All @@ -121,6 +132,8 @@ local function get_receivers(pos, channel)
if dirty then
save_tube_db()
end
-- cache the result for next time
cache[hash] = receivers
return receivers
end

Expand Down

0 comments on commit cec6049

Please sign in to comment.