modified internal/atproto/auth.go +18 -0
| @@ -6,6 +6,7 @@ import ( |
| 6 | 6 | "net" |
| 7 | 7 | "net/http" |
| 8 | 8 | "os" |
| 9 | + "sync" |
| 9 | 10 | "time" |
| 10 | 11 | |
| 11 | 12 | "github.com/bluesky-social/indigo/atproto/identity" |
| @@ -118,7 +119,23 @@ type Profile struct { |
| 118 | 119 | AvatarURL string |
| 119 | 120 | } |
| 120 | 121 | |
| 122 | +type profileEntry struct { |
| 123 | + profile Profile |
| 124 | + fetched time.Time |
| 125 | +} |
| 126 | + |
| 127 | +var profileCache sync.Map |
| 128 | + |
| 129 | +const profileCacheTTL = 4 * time.Hour |
| 130 | + |
| 121 | 131 | func ResolveProfile(ctx context.Context, did string) Profile { |
| 132 | + if cached, ok := profileCache.Load(did); ok { |
| 133 | + entry := cached.(*profileEntry) |
| 134 | + if time.Since(entry.fetched) < profileCacheTTL { |
| 135 | + return entry.profile |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 122 | 139 | ident, err := ResolveIdentity(ctx, did) |
| 123 | 140 | if err != nil { |
| 124 | 141 | return Profile{} |
| @@ -140,5 +157,6 @@ func ResolveProfile(ctx context.Context, did string) Profile { |
| 140 | 157 | p.DisplayName = dn |
| 141 | 158 | p.AvatarURL = avatar |
| 142 | 159 | |
| 160 | + profileCache.Store(did, &profileEntry{profile: p, fetched: time.Now()}) |
| 143 | 161 | return p |
| 144 | 162 | } |
| @@ -6,6 +6,7 @@ import ( |
| 6 | "net" | 6 | "net" |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "os" | 8 | "os" |
| | 9 | + "sync" |
| 9 | "time" | 10 | "time" |
| 10 | | 11 | |
| 11 | "github.com/bluesky-social/indigo/atproto/identity" | 12 | "github.com/bluesky-social/indigo/atproto/identity" |
| @@ -118,7 +119,23 @@ type Profile struct { |
| 118 | AvatarURL string | 119 | AvatarURL string |
| 119 | } | 120 | } |
| 120 | | 121 | |
| | 122 | +type profileEntry struct { |
| | 123 | + profile Profile |
| | 124 | + fetched time.Time |
| | 125 | +} |
| | 126 | + |
| | 127 | +var profileCache sync.Map |
| | 128 | + |
| | 129 | +const profileCacheTTL = 4 * time.Hour |
| | 130 | + |
| 121 | func ResolveProfile(ctx context.Context, did string) Profile { | 131 | func ResolveProfile(ctx context.Context, did string) Profile { |
| | 132 | + if cached, ok := profileCache.Load(did); ok { |
| | 133 | + entry := cached.(*profileEntry) |
| | 134 | + if time.Since(entry.fetched) < profileCacheTTL { |
| | 135 | + return entry.profile |
| | 136 | + } |
| | 137 | + } |
| | 138 | + |
| 122 | ident, err := ResolveIdentity(ctx, did) | 139 | ident, err := ResolveIdentity(ctx, did) |
| 123 | if err != nil { | 140 | if err != nil { |
| 124 | return Profile{} | 141 | return Profile{} |
| @@ -140,5 +157,6 @@ func ResolveProfile(ctx context.Context, did string) Profile { |
| 140 | p.DisplayName = dn | 157 | p.DisplayName = dn |
| 141 | p.AvatarURL = avatar | 158 | p.AvatarURL = avatar |
| 142 | | 159 | |
| | 160 | + profileCache.Store(did, &profileEntry{profile: p, fetched: time.Now()}) |
| 143 | return p | 161 | return p |
| 144 | } | 162 | } |