Documentation
¶
Overview ¶
Package cache implements a highly-concurrent, sharded in-memory cache engineered specifically for loads exceeding 20,000+ RPS.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[V any] interface { fmt.Stringer Options[V] // Hash computes an allocation-free 16-byte cache token using the standard XXH3 algorithm. Hash(data []byte) Key // Set stores a key-value pair in the cache with an explicit Time-To-Live (TTL) duration. // If the shard breaches its capacity limit, a fast random sampling eviction pass is triggered. Set(key Key, value V, ttl time.Duration) // SetDefault assigns a value to the key using the fallback TTL specified during configuration. SetDefault(key Key, value V) // Get retrieves a value from the cache. Returns the value and true on a cache hit. // Returns a zero-value representation and false on a cache miss or if the entry has expired. // Expired elements are lazily evicted on the fly under synchronized locks. Get(key Key) (V, bool) // Peek returns an entry value from the cache without updating hit/miss telemetry // counters or executing lazy-deletion write loops on expired items. Peek(key Key) (V, bool) // Keys aggregates and extracts all active, non-expired keys currently tracking across all 64 shards. // Iterates through shards sequentially to completely avoid global lock contention. // Returns a zero-allocation snapshot index representation. Keys() []Key // Len estimates the total logical size across all shards combined. Len() int // Invalidate removes a single entry immediately from its designated shard. // Invokes the registered onEvicted callback asynchronously if configured. Invalidate(key Key) // InvalidateFn evaluates a user-provided predicate function against all entries across the cache. // Elements returning true are cleanly wiped shard-by-shard. InvalidateFn(fn func(key Key) bool) // RemoveOldest samples candidates across a pseudo-random shard bucket and evicts the one // closest to its absolute expiration timestamp. RemoveOldest() // DeleteExpired actively iterates across all shards, removing elements that have breached // their expiration timestamps. Automatically triggered by the background cleaner routine. DeleteExpired() // Purge clears all tracking maps across all shards instantly. Wipes the allocation matrix. Purge() Stat() Stats // Close terminates background ticking workers cleanly and blocks until execution routines wind down. Close() }
Cache defines the complete interface contract for the ultra-performant sharded cache subsystem.
type HasherFn ¶
HasherFn defines the callback signature for custom hashing algorithms. Must accept raw data vectors and project them into Key (explicit 128-bit unsigned boundaries).
type Key ¶
Key acts as the public vehicle for your cache ecosystem.
func DefaultHasher ¶
DefaultHasher leverages the extremely fast XXH3 non-cryptographic 128-bit hash standard.
type Options ¶
type Options[V any] interface { WithMaxKeys(maxKeys int) Cache[V] WithDefaultTTL(ttl time.Duration) Cache[V] WithClockInterval(interval time.Duration) Cache[V] WithCleanupInterval(interval time.Duration) Cache[V] WithHasher(fn HasherFn) Cache[V] WithOnEvicted(fn func(key Key, value V)) Cache[V] Start() Cache[V] }
Options outlines the customizable fluent builder rules for configuration management.
type SingleFlightRing ¶
type SingleFlightRing[V any] struct { // contains filtered or unexported fields }
SingleFlightRing short-circuits duplicate concurrent workflows safely.
func NewSingleFlightRing ¶
func NewSingleFlightRing[V any]() *SingleFlightRing[V]
NewSingleFlightRing initializes the sharded downstream traffic isolation plane.