package pgmonitor import ( "sync" "time" ) type cacheEntry struct { at time.Time data any } type ttlCache struct { mu sync.RWMutex ttl time.Duration items map[string]cacheEntry } func newTTLCache(ttl time.Duration) *ttlCache { return &ttlCache{ttl: ttl, items: make(map[string]cacheEntry)} } func (c *ttlCache) get(key string) (any, bool) { c.mu.RLock() defer c.mu.RUnlock() e, ok := c.items[key] if !ok || time.Since(e.at) > c.ttl { return nil, false } return e.data, true } func (c *ttlCache) set(key string, data any) { c.mu.Lock() defer c.mu.Unlock() c.items[key] = cacheEntry{at: time.Now().UTC(), data: data} }