Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewInMemoryCache ¶
NewInMemoryCache builds an in-memory cache. Writes expire after defaultExpiry unless overridden per call with cache.WithExpiry; a non-positive defaultExpiry means entries never expire by default.
By default expired entries are evicted lazily, on the read that discovers them or when overwritten, and the map is not otherwise size-bounded. Pass WithJanitor to sweep them on a timer instead — see that option for when the lazy default is not enough.
Types ¶
type Option ¶
type Option func(*options)
Option configures an in-memory cache at construction. Options are applied in the order given, and a nil option is ignored.
It carries no type parameter even though the cache does: nothing an Option sets depends on the cached type, and Go cannot infer a type argument from a call's result type — so an Option[T] would force every call site to spell the cached type out by hand — WithLogger[MyValue](l) — forever.
func WithJanitor ¶
WithJanitor starts a background sweep that removes expired entries every interval, rather than waiting for a read to discover them.
Without it an entry is only dropped when something reads that exact key or overwrites it. That is fine for a hot cache, where the keys worth evicting are the keys being read anyway. It is not fine for a workload that writes many keys and rarely reads them back — an idempotency-key store, a long-TTL request cache — because nothing ever triggers the lazy path and the map grows without bound. The rule of thumb: enable it whenever the expiry is long relative to how often a given key is read.
The sweep stops on Close, and also when ctx is done — whichever happens first. Passing context.Background() and relying on Close is the ordinary shape; a cancellable ctx is for tying the sweep to something narrower than the cache's own lifetime. A nil ctx or a non-positive interval starts no goroutine at all.
func WithLogger ¶
WithLogger attaches a logger. An absent logger logs nowhere.
func WithMetricsProvider ¶
WithMetricsProvider attaches a metrics provider for the cache's hit, miss, set, delete, and eviction counters and its latency histogram. An absent provider records nothing.
func WithTracerProvider ¶
func WithTracerProvider(tracerProvider tracing.TracerProvider) Option
WithTracerProvider attaches a tracer provider, enabling spans on every cache operation. An absent tracer provider traces nowhere.