From 3b70052f3d0aaffec33a3447071054ee0604f0e3 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 29 Oct 2019 14:18:13 -0400 Subject: [PATCH] can use the cache functions for handles now --- activity.go | 4 ++-- fun.go | 58 ++++++++++++++++++----------------------------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/activity.go b/activity.go index 1cdfeaa..4ac6e27 100644 --- a/activity.go +++ b/activity.go @@ -262,8 +262,8 @@ func eradicatexonk(userid int64, xid string) { func savexonk(x *Honk) { log.Printf("saving xonk: %s", x.XID) - go prehandle(x.Honker) - go prehandle(x.Oonker) + go handles(x.Honker) + go handles(x.Oonker) savehonk(x) } diff --git a/fun.go b/fun.go index e958600..830c861 100644 --- a/fun.go +++ b/fun.go @@ -487,36 +487,13 @@ 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) { - if xid == "" { - return "", "" - } - 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 { +var allhandles = cache.New(cache.Options{Filler: func(xid string) (string, bool) { row := stmtGetXonker.QueryRow(xid, "handle") var handle string err := row.Scan(&handle) if err != nil { - info, _ := investigate(xid) - if info == nil { + info, err := investigate(xid) + if err != nil { m := re_unurl.FindStringSubmatch(xid) if len(m) > 2 { handle = m[2] @@ -525,21 +502,26 @@ func findhandle(xid string) string { } } else { handle = info.Name - } - _, err = stmtSaveXonker.Exec(xid, handle, "handle") - if err != nil { - log.Printf("error saving handle: %s", err) + _, err = stmtSaveXonker.Exec(xid, handle, "handle") + if err != nil { + log.Printf("error saving handle: %s", err) + } } } - return handle -} - -var handleprelock sync.Mutex + return handle, true +}}) -func prehandle(xid string) { - handleprelock.Lock() - defer handleprelock.Unlock() - handles(xid) +// handle, handle@host +func handles(xid string) (string, string) { + if xid == "" { + return "", "" + } + var handle string + allhandles.Get(xid, &handle) + if handle == xid { + return xid, xid + } + return handle, handle + "@" + originate(xid) } func prepend(s string, x []string) []string {