Cache Middleware (cache)
Response caching with pluggable store, built-in stampede prevention.
Config
Store — pluggable cache store (default: sharded in-memory LRU)
TTL — cache entry time-to-live
KeyGenerator — function to derive cache key from request (default: method + path + sorted query)
Singleflight — enable request coalescing on cache miss (default: true)
X-Cache header — HIT or MISS
MemoryStore: Sharded LRU
- Configurable number of shards (default: 16)
- Each shard has its own
sync.RWMutex + LRU list
- Key hashed to select shard
- Per-shard max entries; LRU eviction within each shard
- Expired entries cleaned lazily on access + periodic sweep
Stampede Prevention (Singleflight)
When enabled (default), concurrent cache misses for the same key are coalesced using sync/singleflight.
API
Invalidate(key string), InvalidatePrefix(prefix string)
Store interface: Get(key) ([]byte, bool), Set(key, value, ttl), Delete(key), DeletePrefix(prefix)
Migrated from goceleris/middlewares#20
Cache Middleware (cache)
Response caching with pluggable store, built-in stampede prevention.
Config
Store— pluggable cache store (default: sharded in-memory LRU)TTL— cache entry time-to-liveKeyGenerator— function to derive cache key from request (default: method + path + sorted query)Singleflight— enable request coalescing on cache miss (default: true)X-Cacheheader —HITorMISSMemoryStore: Sharded LRU
sync.RWMutex+ LRU listStampede Prevention (Singleflight)
When enabled (default), concurrent cache misses for the same key are coalesced using
sync/singleflight.API
Invalidate(key string),InvalidatePrefix(prefix string)Storeinterface:Get(key) ([]byte, bool),Set(key, value, ttl),Delete(key),DeletePrefix(prefix)Migrated from goceleris/middlewares#20