Fetch, resize, reformat, and cache Atmosphere avatar images atp.pics
atproto
at optimize-resolution 37 lines 751 B view raw
1// Package cache provides a lightweight in-process TTL cache for DID → avatar CID mappings. 2package cache 3 4import ( 5 "sync" 6 "time" 7) 8 9const ttl = 5 * time.Minute 10 11type entry struct { 12 cid string 13 mimeType string 14 expiry time.Time 15} 16 17// CIDCache caches DID → (avatar CID, mimeType) with a fixed 5-minute TTL. 18type CIDCache struct { 19 m sync.Map 20} 21 22func (c *CIDCache) Get(did string) (cid, mimeType string, ok bool) { 23 v, ok := c.m.Load(did) 24 if !ok { 25 return "", "", false 26 } 27 e := v.(entry) 28 if time.Now().After(e.expiry) { 29 c.m.Delete(did) 30 return "", "", false 31 } 32 return e.cid, e.mimeType, true 33} 34 35func (c *CIDCache) Set(did, cid, mimeType string) { 36 c.m.Store(did, entry{cid: cid, mimeType: mimeType, expiry: time.Now().Add(ttl)}) 37}