Cache (Multi-Layer)

/cache Tier 1

Read-through cache with singleflight stampede protection and automatic L1 back-fill on L2 hits. Layers are checked in order: L1 (fast, local) → L2 (shared, e.g. Redis). On miss, the handler runs once and the result is written to all layers.

requestL1 memory5min TTLL2 redis30min TTLsingleflightcollapse misshandlerexecutemisshitmisshitback-fill L1write to all layers on missRead-through with stampede protection and L1 back-fill

Signature

action contract
.Cache(ttl time.Duration, keyFn func(Req) string, layers ...CacheLayer[Res]) *Builder[Req, Res]