Skip to content

[middleware] Cache (cache) #184

@FumingPower3925

Description

@FumingPower3925

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

  1. Configurable number of shards (default: 16)
  2. Each shard has its own sync.RWMutex + LRU list
  3. Key hashed to select shard
  4. Per-shard max entries; LRU eviction within each shard
  5. 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

Metadata

Metadata

Labels

middlewareMiddleware implementation

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions