add memoize util
This commit is contained in:
parent
38b0767048
commit
a0187786c5
1
init.lua
1
init.lua
@ -8,6 +8,7 @@ building_lib = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local MP = minetest.get_modpath("building_lib")
|
local MP = minetest.get_modpath("building_lib")
|
||||||
|
dofile(MP .. "/memoize.lua")
|
||||||
dofile(MP .. "/entity.lua")
|
dofile(MP .. "/entity.lua")
|
||||||
dofile(MP .. "/preview.lua")
|
dofile(MP .. "/preview.lua")
|
||||||
dofile(MP .. "/api.lua")
|
dofile(MP .. "/api.lua")
|
||||||
|
24
memoize.lua
Normal file
24
memoize.lua
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
local cache = {}
|
||||||
|
|
||||||
|
function building_lib.memoize(fn, keyfn)
|
||||||
|
return function(...)
|
||||||
|
local key = keyfn(...)
|
||||||
|
if not cache[key] then
|
||||||
|
cache[key] = { fn(...) }
|
||||||
|
end
|
||||||
|
return unpack(cache[key])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function building_lib.memoize_invalidate(key)
|
||||||
|
cache[key] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- periodic cleanup
|
||||||
|
local function purge_cache()
|
||||||
|
cache = {}
|
||||||
|
minetest.after(300, purge_cache)
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.after(10, purge_cache)
|
Loading…
x
Reference in New Issue
Block a user