package query import ( "container/list" "crypto/sha256" "encoding/hex" "sync" "sync/atomic" "time" ) // CacheEntry holds the cached HTTP response payload and metadata. type CacheEntry struct { ContentType string Body []byte StatusCode int ExpiresAt time.Time ETag string } type cacheElement struct { key string entry CacheEntry sizeBytes int64 } // CacheStats tracks in-memory response cache performance metrics. type CacheStats struct { Hits uint64 `json:"hits"` Misses uint64 `json:"misses"` Evictions uint64 `json:"evictions"` Items int `json:"items"` SizeBytes int64 `json:"size_bytes"` } // ResponseCache is a thread-safe, bounded in-memory LRU response cache. type ResponseCache struct { mu sync.Mutex maxEntries int maxBytes int64 curBytes int64 items map[string]*list.Element lruList *list.List hits atomic.Uint64 misses atomic.Uint64 evictions atomic.Uint64 } // NewResponseCache creates a new in-memory LRU cache with the specified capacity limits. func NewResponseCache(maxEntries int, maxBytes ...int64) *ResponseCache { if maxEntries <= 0 { maxEntries = 512 } var mb int64 = 64 << 20 // 64 MB default if len(maxBytes) > 0 && maxBytes[0] > 0 { mb = maxBytes[0] } return &ResponseCache{ maxEntries: maxEntries, maxBytes: mb, items: make(map[string]*list.Element, maxEntries), lruList: list.New(), } } // Get retrieves a cached response entry if it exists and has not expired. func (c *ResponseCache) Get(key string) (*CacheEntry, bool) { if c == nil { return nil, false } c.mu.Lock() defer c.mu.Unlock() elem, ok := c.items[key] if !ok { c.misses.Add(1) return nil, false } item := elem.Value.(*cacheElement) if time.Now().After(item.entry.ExpiresAt) { c.removeElement(elem) c.misses.Add(1) return nil, false } c.lruList.MoveToFront(elem) c.hits.Add(1) return &item.entry, true } // Set stores a response entry in the cache, evicting the least recently used entries if needed. func (c *ResponseCache) Set(key string, entry CacheEntry) { if c == nil { return } c.mu.Lock() defer c.mu.Unlock() entrySize := int64(len(entry.Body) + len(key) + len(entry.ContentType) + 64) // If entry already exists, purge old element first if elem, ok := c.items[key]; ok { c.removeElement(elem) } // Evict LRU elements while capacity is exceeded for c.lruList.Len() > 0 && (c.lruList.Len() >= c.maxEntries || (c.maxBytes > 0 && c.curBytes+entrySize > c.maxBytes)) { back := c.lruList.Back() if back != nil { c.removeElement(back) c.evictions.Add(1) } } item := &cacheElement{ key: key, entry: entry, sizeBytes: entrySize, } elem := c.lruList.PushFront(item) c.items[key] = elem c.curBytes += entrySize } func (c *ResponseCache) removeElement(elem *list.Element) { c.lruList.Remove(elem) item := elem.Value.(*cacheElement) delete(c.items, item.key) c.curBytes -= item.sizeBytes } // Stats returns cache hit, miss, and size metrics. func (c *ResponseCache) Stats() CacheStats { if c == nil { return CacheStats{} } c.mu.Lock() defer c.mu.Unlock() return CacheStats{ Hits: c.hits.Load(), Misses: c.misses.Load(), Evictions: c.evictions.Load(), Items: c.lruList.Len(), SizeBytes: c.curBytes, } } // Purge removes all entries from the cache. func (c *ResponseCache) Purge() { if c == nil { return } c.mu.Lock() defer c.mu.Unlock() c.lruList.Init() c.items = make(map[string]*list.Element, c.maxEntries) c.curBytes = 0 } // computeETag generates a fast, deterministic hex ETag from the payload. func computeETag(body []byte) string { hash := sha256.Sum256(body) return "\"" + hex.EncodeToString(hash[:8]) + "\"" } // ComputeQuantization aligns rolling dashboard query intervals to uniform step boundaries. // Enables rotating dashboards to hit the exact same cache key during a rotation cycle, // while allowing past historical queries (immutable data) to be cached with long TTLs. func ComputeQuantization(from, to, now int64) (queryFrom, queryTo int64, ttl time.Duration, isRolling bool) { window := to - from if window <= 0 { return from, to, 10 * time.Second, false } var intervalMs int64 switch { case window <= 5*60*1000: // <= 5 minutes intervalMs = 60 * 1000 // 1 minute case window <= 60*60*1000: // <= 1 hour intervalMs = 60 * 1000 // 1 minute case window <= 6*60*60*1000: // <= 6 hours intervalMs = 5 * 60 * 1000 // 5 minutes case window <= 24*60*60*1000: // <= 24 hours (1 day) intervalMs = 15 * 60 * 1000 // 15 minutes case window <= 7*24*60*60*1000: // <= 7 days (1 week) intervalMs = 60 * 60 * 1000 // 1 hour default: // >= 30 days (e.g. 30d, 90d, 1 year) intervalMs = 24 * 60 * 60 * 1000 // 1 day } // If 'to' is within 2 quantize intervals of 'now', it's an active rolling dashboard if now-to < 2*intervalMs { isRolling = true queryTo = ((to + intervalMs - 1) / intervalMs) * intervalMs queryFrom = queryTo - window intervalEnd := ((to / intervalMs) + 1) * intervalMs remainingMs := intervalEnd - now if remainingMs <= 0 { remainingMs = intervalMs } ttl = max(time.Duration(remainingMs)*time.Millisecond, 5*time.Second) return queryFrom, queryTo, ttl, isRolling } // Fixed historical range: cold tier is immutable, cache for 1 hour return from, to, 1 * time.Hour, false } // computeQuantization is an unexported alias for internal package usage. var computeQuantization = ComputeQuantization