From 5b309bb846ae0083408fa42e01fdc21acacc48cf Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 16 Aug 2019 00:11:30 -0400 Subject: [PATCH] a cache for handles --- fun.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/fun.go b/fun.go index b16b641..f017536 100644 --- a/fun.go +++ b/fun.go @@ -374,8 +374,28 @@ func originate(u string) string { return "" } +var allhandles = make(map[string]string) +var handlelock sync.Mutex + // handle, handle@host func handles(xid string) (string, string) { + handlelock.Lock() + handle := allhandles[xid] + handlelock.Unlock() + if handle == "" { + handle = findhandle(xid) + handlelock.Lock() + allhandles[xid] = handle + handlelock.Unlock() + } + if handle == xid { + return xid, xid + } + return handle, handle + "@" + originate(xid) +} + +func findhandle(xid string) string { + log.Printf("finding") row := stmtGetXonker.QueryRow(xid, "handle") var handle string err := row.Scan(&handle) @@ -396,10 +416,7 @@ func handles(xid string) (string, string) { log.Printf("error saving handle: %s", err) } } - if handle == xid { - return xid, xid - } - return handle, handle + "@" + originate(xid) + return handle } func prepend(s string, x []string) []string {