From f274328871cf6c75be082a559eb5892d950b1f78 Mon Sep 17 00:00:00 2001 From: osmarks Date: Fri, 27 Jul 2018 10:26:37 +0100 Subject: [PATCH] Implement cached lookup because rednet is stupid --- lib.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib.lua b/lib.lua index c3270f5..1672706 100644 --- a/lib.lua +++ b/lib.lua @@ -64,6 +64,17 @@ end local protocol = "wyvern" +local lookup_cache = {} + +local function cached_lookup(protocol) + if lookup_cache[protocol] then return lookup_cache[protocol] + else + local ID = rednet.lookup(protocol) + lookup_cache[protocol] = ID + return ID + end +end + local function init_screen(scr, bg, fg) scr.setCursorPos(1, 1) scr.setBackgroundColor(bg) @@ -145,7 +156,7 @@ local function query_by_ID(ID, request, max_tries) end local function query_by_type(type, request, tries) - local ID = rednet.lookup(protocol .. "/" .. type) + local ID = cached_lookup(protocol .. "/" .. type) if not ID then return errors.make(errors.NOMATCHINGNODE, type) end return query_by_ID(ID, request, tries) end