github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/usermemo.go (about) 1 package libkb 2 3 import ( 4 "sync" 5 "time" 6 7 keybase1 "github.com/keybase/client/go/protocol/keybase1" 8 ) 9 10 type UserPlusKeysMemo struct { 11 upk keybase1.UserPlusKeys 12 validUntil time.Time 13 sync.RWMutex 14 Contextified 15 } 16 17 func NewUserPlusKeysMemo(g *GlobalContext) *UserPlusKeysMemo { 18 return &UserPlusKeysMemo{ 19 Contextified: NewContextified(g), 20 } 21 } 22 23 func (u *UserPlusKeysMemo) Get(uid keybase1.UID) (keybase1.UserPlusKeys, bool) { 24 u.RLock() 25 defer u.RUnlock() 26 27 if !u.upk.Uid.Equal(uid) { 28 return keybase1.UserPlusKeys{}, false 29 } 30 31 if time.Now().Before(u.validUntil) { 32 u.G().Log.Debug("UserPlusKeysMemo: cache expired") 33 return keybase1.UserPlusKeys{}, false 34 } 35 36 u.G().Log.Debug("UserPlusKeysMemo: cache hit") 37 38 return u.upk, true 39 } 40 41 func (u *UserPlusKeysMemo) Set(upk keybase1.UserPlusKeys) { 42 u.Lock() 43 defer u.Unlock() 44 45 u.G().Log.Debug("UserPlusKeysMemo: set %s, %s", upk.Username, upk.Uid) 46 47 u.upk = upk 48 u.validUntil = time.Now().Add(1 * time.Minute) 49 } 50 51 func (u *UserPlusKeysMemo) Clear() { 52 u.Lock() 53 defer u.Unlock() 54 55 u.G().Log.Debug("UserPlusKeysMemo: clear") 56 57 u.upk = keybase1.UserPlusKeys{} 58 u.validUntil = time.Time{} 59 }