cache works better when we put stuff in it
Ted Unangst tedu@tedunangst.com
Sun, 14 Apr 2019 13:20:56 -0400
1 files changed,
18 insertions(+),
4 deletions(-)
jump to
M
fun.go
→
fun.go
@@ -219,12 +219,13 @@ return x
} var ziggies = make(map[string]*rsa.PrivateKey) +var zaggies = make(map[string]*rsa.PublicKey) var ziggylock sync.Mutex func ziggy(username string) (keyname string, key *rsa.PrivateKey) { ziggylock.Lock() - defer ziggylock.Unlock() key = ziggies[username] + ziggylock.Unlock() if key == nil { db := opendatabase() row := db.QueryRow("select seckey from users where username = ?", username)@@ -233,22 +234,31 @@ row.Scan(&data)
var err error key, _, err = pez(data) if err != nil { - log.Printf("error loading %s seckey: %s", username, err) + log.Printf("error decoding %s seckey: %s", username, err) return } + ziggylock.Lock() + ziggies[username] = key + ziggylock.Unlock() } keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username) return } func zaggy(keyname string) (key *rsa.PublicKey) { + ziggylock.Lock() + key = zaggies[keyname] + ziggylock.Unlock() + if key != nil { + return + } db := opendatabase() row := db.QueryRow("select pubkey from honkers where flavor = 'key' and xid = ?", keyname) var data string err := row.Scan(&data) savekey := false if err != nil { - savekey = true + log.Printf("hitting the webs for missing pubkey: %s", keyname) j, err := GetJunk(keyname) if err != nil { log.Printf("error getting %s pubkey: %s", keyname, err)@@ -265,12 +275,16 @@ if !ok {
log.Printf("error getting %s pubkey owner", keyname) return } + savekey = true } _, key, err = pez(data) if err != nil { - log.Printf("error getting %s pubkey: %s", keyname, err) + log.Printf("error decoding %s pubkey: %s", keyname, err) return } + ziggylock.Lock() + zaggies[keyname] = key + ziggylock.Unlock() if savekey { db.Exec("insert into honkers (name, xid, flavor, pubkey) values (?, ?, ?, ?)", "", keyname, "key", data)