perf(cache): sort-based eviction and consistent non-positive TTL handling

The count-based evictOldest used an O(n squared) selection sort with a
candidate cap over an unordered map range, so it could evict the wrong
entries. It now collects all entries and sorts by expiry, matching the
memory-based evictor.

A non-positive CACHE_TTL behaved differently per backend: the standard
memory cache panicked, Redis stored a permanent key, and the LRU cache
expired entries immediately. Each backend now clamps a non-positive TTL
to a shared default at its Set boundary. The nil-logger fallback in the
Redis wrapper now uses the constructor so it cannot panic.
This commit is contained in:
2026-08-14 11:09:57 +01:00
parent 1cab28b054
commit 159faa68e0
9 changed files with 308 additions and 25 deletions
+8
View File
@@ -65,6 +65,14 @@ func (c *LRUMemoryCache) Set(key string, value []byte, ttl time.Duration) {
}
entrySize := int64(len(key) + len(finalValue) + 64) // 64 bytes overhead estimate
// A non-positive TTL is clamped to defaultTTL (defined in memory.go) so
// this backend does not stamp the entry as already-expired (immediate
// miss on every read). Keeps behavior identical to the standard memory
// backend and Redis, which clamp the same way at their own Set boundary.
if ttl <= 0 {
ttl = defaultTTL
}
expiresAt := time.Now().Add(ttl)
c.mu.Lock()