package db import ( "sync" "time" ) type entityCacheEntry struct { slug string id string attrs string exp time.Time } type EntityCache struct { mu sync.RWMutex m map[string]entityCacheEntry ttl time.Duration } func NewEntityCache(ttl time.Duration) *EntityCache { return &EntityCache{ m: make(map[string]entityCacheEntry), ttl: ttl, } } func (c *EntityCache) GetSlug(id string) (string, bool) { c.mu.RLock() e, ok := c.m[id] c.mu.RUnlock() if !ok || time.Now().After(e.exp) { return "", false } return e.slug, true } func (c *EntityCache) GetID(slug string) (string, bool) { c.mu.RLock() e, ok := c.m[slug] c.mu.RUnlock() if !ok || time.Now().After(e.exp) { return "", false } return e.id, true } func (c *EntityCache) Set(slug, id, attrs string) { exp := time.Now().Add(c.ttl) c.mu.Lock() c.m[slug] = entityCacheEntry{slug: slug, id: id, attrs: attrs, exp: exp} c.m[id] = entityCacheEntry{slug: slug, id: id, attrs: attrs, exp: exp} c.mu.Unlock() } func (c *EntityCache) Invalidate(slug, id string) { c.mu.Lock() delete(c.m, slug) delete(c.m, id) c.mu.Unlock() }