a cache for handles

This commit is contained in:
Ted Unangst 2019-08-16 00:11:30 -04:00
parent 953f7c5743
commit 5b309bb846
1 changed files with 21 additions and 4 deletions

25
fun.go
View File

@ -374,8 +374,28 @@ func originate(u string) string {
return "" return ""
} }
var allhandles = make(map[string]string)
var handlelock sync.Mutex
// handle, handle@host // handle, handle@host
func handles(xid string) (string, string) { 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") row := stmtGetXonker.QueryRow(xid, "handle")
var handle string var handle string
err := row.Scan(&handle) err := row.Scan(&handle)
@ -396,10 +416,7 @@ func handles(xid string) (string, string) {
log.Printf("error saving handle: %s", err) log.Printf("error saving handle: %s", err)
} }
} }
if handle == xid { return handle
return xid, xid
}
return handle, handle + "@" + originate(xid)
} }
func prepend(s string, x []string) []string { func prepend(s string, x []string) []string {