A community based topic aggregation platform built on atproto
at main 2.4 kB view raw
1package identity 2 3import ( 4 "context" 5 "log" 6) 7 8// cachingResolver wraps a base resolver with caching 9type cachingResolver struct { 10 base Resolver 11 cache IdentityCache 12} 13 14// newCachingResolver creates a new caching resolver 15func newCachingResolver(base Resolver, cache IdentityCache) Resolver { 16 return &cachingResolver{ 17 base: base, 18 cache: cache, 19 } 20} 21 22// Resolve resolves a handle or DID to complete identity information 23// First checks cache, then falls back to base resolver 24func (r *cachingResolver) Resolve(ctx context.Context, identifier string) (*Identity, error) { 25 // Try cache first 26 cached, err := r.cache.Get(ctx, identifier) 27 if err == nil { 28 // Cache hit - mark it as from cache 29 cached.Method = MethodCache 30 return cached, nil 31 } 32 33 // Cache miss - resolve using base resolver 34 identity, err := r.base.Resolve(ctx, identifier) 35 if err != nil { 36 return nil, err 37 } 38 39 // Cache the resolved identity (ignore cache errors, just log them) 40 if cacheErr := r.cache.Set(ctx, identity); cacheErr != nil { 41 log.Printf("Warning: failed to cache identity for %s: %v", identifier, cacheErr) 42 } 43 44 return identity, nil 45} 46 47// ResolveHandle specifically resolves a handle to DID and PDS URL 48func (r *cachingResolver) ResolveHandle(ctx context.Context, handle string) (did, pdsURL string, err error) { 49 identity, err := r.Resolve(ctx, handle) 50 if err != nil { 51 return "", "", err 52 } 53 54 return identity.DID, identity.PDSURL, nil 55} 56 57// ResolveDID retrieves a DID document and extracts the PDS endpoint 58func (r *cachingResolver) ResolveDID(ctx context.Context, did string) (*DIDDocument, error) { 59 // Try to get from cache first 60 cached, err := r.cache.Get(ctx, did) 61 if err == nil { 62 // We have cached identity, construct a simple DID document 63 return &DIDDocument{ 64 DID: cached.DID, 65 Service: []Service{ 66 { 67 ID: "#atproto_pds", 68 Type: "AtprotoPersonalDataServer", 69 ServiceEndpoint: cached.PDSURL, 70 }, 71 }, 72 }, nil 73 } 74 75 // Cache miss - use base resolver 76 return r.base.ResolveDID(ctx, did) 77} 78 79// Purge removes an identifier from the cache and propagates to base 80func (r *cachingResolver) Purge(ctx context.Context, identifier string) error { 81 // Purge from cache 82 if err := r.cache.Purge(ctx, identifier); err != nil { 83 return err 84 } 85 86 // Propagate to base resolver (though it typically won't cache) 87 return r.base.Purge(ctx, identifier) 88}