Every exported feature of github.com/ubgo/cache (and its cachetest / admin
sub-packages), documented one-by-one: what it is, real-world use cases, and a
runnable Go snippet.
New here? Read the family overview to pick a backend, or the migration guide if you're coming from Ristretto / golang-lru / go-cache.
- Pick a backend module (
cache-mem,cache-redis,cache-pg,cache-tiered, …). It implementscache.Cache. - For raw bytes, use the core operations (
Get/Set/Del/…). - For typed load-through with stampede protection, use
Rememberor aTyped[T]view. - Compose cross-cutting behavior by wrapping the cache: namespacing, observability, resilience.
- Unit-test consumers with
cachetest.Mock; adapter authors runcachetest.Run.
All snippets use mem "github.com/ubgo/cache-mem" as a stand-in backend; swap
in any adapter — behavior is identical (enforced by the conformance suite).
| Page | Covers |
|---|---|
| core-operations.md | Cache interface (all 19 methods), Item, IterateOpts, Iterator, sentinel errors, Stats, EvictionCause, HitRatio |
| remember.md | Remember, LoadFn, all RememberOpts, GetT/SetT, the envelope, RememberMulti/MultiLoadFn |
| generics.md | Typed[T], NewTyped, its methods, Raw |
| codecs.md | Codec, JSONCodec, GobCodec, RawCodec, DefaultCodec, EncryptedCodec, KeyProvider, StaticKey |
| namespacing.md | Namespaced + scoped Flush |
| locker.md | Locker, NewLock, Acquire/Refresh/Release, ErrLockNotAcquired |
| invalidation.md | Invalidation, NewInProcInvalidation, InvalidateAll |
| observability.md | Instrument, ObsHooks, OpEvent, KeyHash, HotKeyTracker, NamespaceStats, NamespaceFn, DefaultNamespaceFn |
| resilience.md | NewCircuitBreaker, NewRetry, NewBulkhead, NewAuditLog, AuditEvent, AuditFunc, all options, stacking order |
| helpers.md | Memoize, QueryKey, Once, ErrInFlight, Warm, WarmResult |
| admin.md | admin.Mount, admin.Handler, admin.Options, routes, auth gating |
| testing.md | cachetest.Run, Bench, Factory, Mock, Zipfian/NewZipfian, ScanGen, HitRate, Round |
| I want to… | Use | Page |
|---|---|---|
| Store/read raw bytes | Set / Get |
core |
| Cache a typed value with load-through | Remember[T] |
remember |
| Keep hot keys from ever expiring | WithRefreshAhead |
remember |
| Stay up when the DB is down | WithStaleIfError |
remember |
| Stop re-querying missing rows | WithNegativeTTL |
remember |
| Avoid a thundering-herd expiry | WithJitter |
remember |
| Batch-load N keys in 2 round-trips | RememberMulti[T] |
remember |
| A type-safe cache handle | Typed[T] |
generics |
| Encrypt PII at rest in a shared store | EncryptedCodec |
codecs |
| Share one backend across services | Namespaced |
namespacing |
| A cross-pod mutex | NewLock |
locker |
| Drop L1 copies when another node writes | Invalidation |
invalidation |
| Metrics / tracing without a dependency | Instrument + ObsHooks |
observability |
| Per-feature hit-rate | NamespaceStats |
observability |
| Find the key that is 60% of traffic | HotKeyTracker |
observability |
| Fail fast when the backend is sick | NewCircuitBreaker |
resilience |
| Retry transient backend errors | NewRetry |
resilience |
| Bound in-flight load on the backend | NewBulkhead |
resilience |
| A compliance trail of writes | NewAuditLog |
resilience |
| Memoize a function | Memoize |
helpers |
| Idempotent webhooks/payments | Once |
helpers |
| Preload a cold cache at startup | Warm |
helpers |
| A collision-free cache key | QueryKey |
helpers |
| Inspect cache in production over HTTP | admin.Mount |
admin |
| Unit-test code that uses a cache | cachetest.Mock |
testing |
| Verify a new adapter is correct | cachetest.Run |
testing |