@@ -3,7 +3,9 @@ package teams
33import (
44 "fmt"
55 "sort"
6+ "strings"
67
8+ lru "github.com/hashicorp/golang-lru"
79 "github.com/keybase/client/go/kbfs"
810 "github.com/keybase/client/go/libkb"
911 "github.com/keybase/client/go/protocol/keybase1"
@@ -37,7 +39,6 @@ func (i *implicitTeam) GetAppStatus() *libkb.AppStatus {
3739// Resolves social assertions.
3840func LookupImplicitTeam (ctx context.Context , g * libkb.GlobalContext , displayName string , public bool ) (
3941 team * Team , teamName keybase1.TeamName , impTeamName keybase1.ImplicitTeamDisplayName , err error ) {
40-
4142 team , teamName , impTeamName , _ , err = LookupImplicitTeamAndConflicts (ctx , g , displayName , public )
4243 return team , teamName , impTeamName , err
4344}
@@ -55,13 +56,36 @@ func LookupImplicitTeamAndConflicts(ctx context.Context, g *libkb.GlobalContext,
5556
5657func LookupImplicitTeamIDUntrusted (ctx context.Context , g * libkb.GlobalContext , displayName string ,
5758 public bool ) (res keybase1.TeamID , err error ) {
58- imp , err := loadImpteamFromServer (ctx , g , displayName , public )
59+ imp , err := loadImpteam (ctx , g , displayName , public )
5960 if err != nil {
6061 return res , err
6162 }
6263 return imp .TeamID , nil
6364}
6465
66+ func loadImpteam (ctx context.Context , g * libkb.GlobalContext , displayName string , public bool ) (imp implicitTeam , err error ) {
67+ cacheKey := impTeamCacheKey (displayName , public )
68+ cacher := g .GetImplicitTeamCacher ()
69+ if cacher != nil {
70+ if cv , ok := cacher .Get (cacheKey ); ok {
71+ if imp , ok := cv .(implicitTeam ); ok {
72+ g .Log .CDebugf (ctx , "using cached iteam" )
73+ return imp , nil
74+ }
75+ g .Log .CDebugf (ctx , "Bad element of wrong type from cache: %T" , cv )
76+ }
77+ }
78+ imp , err = loadImpteamFromServer (ctx , g , displayName , public )
79+ if err != nil {
80+ return imp , err
81+ }
82+ // If the team has any assertions just skip caching
83+ if cacher != nil && ! strings .Contains (imp .DisplayName , "@" ) {
84+ cacher .Put (cacheKey , imp )
85+ }
86+ return imp , nil
87+ }
88+
6589func loadImpteamFromServer (ctx context.Context , g * libkb.GlobalContext , displayName string , public bool ) (imp implicitTeam , err error ) {
6690 arg := libkb .NewAPIArgWithNetContext (ctx , "team/implicit" )
6791 arg .SessionType = libkb .APISessionTypeOPTIONAL
@@ -104,7 +128,7 @@ func lookupImplicitTeamAndConflicts(ctx context.Context, g *libkb.GlobalContext,
104128 if err != nil {
105129 return team , teamName , impTeamName , conflicts , err
106130 }
107- imp , err := loadImpteamFromServer (ctx , g , lookupNameWithoutConflict , impTeamName .IsPublic )
131+ imp , err := loadImpteam (ctx , g , lookupNameWithoutConflict , impTeamName .IsPublic )
108132 if err != nil {
109133 return team , teamName , impTeamName , conflicts , err
110134 }
@@ -120,7 +144,7 @@ func lookupImplicitTeamAndConflicts(ctx context.Context, g *libkb.GlobalContext,
120144
121145 if err != nil {
122146 // warn, don't fail
123- g .Log .CWarningf (ctx , "LookupImplicitTeam got conflict suffix: %v" , err )
147+ g .Log .CDebugf (ctx , "LookupImplicitTeam got conflict suffix: %v" , err )
124148 err = nil
125149 continue
126150 }
@@ -270,7 +294,7 @@ func formatImplicitTeamDisplayNameCommon(ctx context.Context, g *libkb.GlobalCon
270294 }
271295
272296 var suffix string
273- if impTeamName .ConflictInfo != nil && impTeamName . ConflictInfo .IsConflict () {
297+ if impTeamName .ConflictInfo .IsConflict () {
274298 suffix = libkb .FormatImplicitTeamDisplayNameSuffix (* impTeamName .ConflictInfo )
275299 }
276300
@@ -295,3 +319,42 @@ func sortStringsFront(ss []string, front string) {
295319 return a < b
296320 })
297321}
322+
323+ func impTeamCacheKey (displayName string , public bool ) string {
324+ return fmt .Sprintf ("%s-%v" , displayName , public )
325+ }
326+
327+ type implicitTeamCache struct {
328+ cache * lru.Cache
329+ }
330+
331+ func newImplicitTeamCache (g * libkb.GlobalContext ) * implicitTeamCache {
332+ cache , err := lru .New (libkb .ImplicitTeamCacheSize )
333+ if err != nil {
334+ panic (err )
335+ }
336+ return & implicitTeamCache {
337+ cache : cache ,
338+ }
339+ }
340+
341+ func (i * implicitTeamCache ) Get (key interface {}) (interface {}, bool ) {
342+ return i .cache .Get (key )
343+ }
344+
345+ func (i * implicitTeamCache ) Put (key , value interface {}) bool {
346+ return i .cache .Add (key , value )
347+ }
348+
349+ func (i * implicitTeamCache ) OnLogout (m libkb.MetaContext ) error {
350+ i .cache .Purge ()
351+ return nil
352+ }
353+
354+ var _ libkb.MemLRUer = & implicitTeamCache {}
355+
356+ func NewImplicitTeamCacheAndInstall (g * libkb.GlobalContext ) {
357+ cache := newImplicitTeamCache (g )
358+ g .SetImplicitTeamCacher (cache )
359+ g .AddLogoutHook (cache )
360+ }
0 commit comments