Documentation
¶
Overview ¶
Package cache provides a lock-free adaptive in-memory cache implementation. CloxCache uses protected-freq eviction: items with high frequency are protected, with LRU as a tiebreaker among same-frequency items.
Index ¶
- func FormatMemory(bytes uint64) string
- type AdaptiveStats
- type CloxCache
- func (c *CloxCache[K, V]) AverageK() float64
- func (c *CloxCache[K, V]) AverageLearnedThresholds() (rateLow, rateHigh float64)
- func (c *CloxCache[K, V]) Close()
- func (c *CloxCache[K, V]) Get(key K) (V, bool)
- func (c *CloxCache[K, V]) GetAdaptiveStats() []AdaptiveStats
- func (c *CloxCache[K, V]) Put(key K, value V) bool
- func (c *CloxCache[K, V]) Stats() (hits, misses, evictions uint64)
- type Config
- type Key
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatMemory ¶
FormatMemory formats bytes as human-readable string
Types ¶
type AdaptiveStats ¶ added in v0.2.1
type AdaptiveStats struct {
ShardID int
K int32 // current protection threshold for this shard
GraduationRate float64 // fraction of items whose freq crossed the shard's k
EvictedUnprotected uint64 // items evicted with freq <= k
EvictedProtected uint64 // items evicted with freq > k (fallback)
ReachedProtected uint64 // items whose freq crossed the shard's current k
// Learned thresholds (self-tuning)
LearnedRateLow float64 // learned low threshold (rate below which k decreases)
LearnedRateHigh float64 // learned high threshold (rate above which k increases)
WindowHitRate float64 // current window hit rate
}
AdaptiveStats returns per-shard adaptive threshold statistics
type CloxCache ¶
CloxCache is a lock-free adaptive in-memory cache. It stores generic keys of type K (string or []byte) and values of type V.
func NewCloxCache ¶
NewCloxCache creates a new cache with the given configuration
func (*CloxCache[K, V]) AverageK ¶ added in v0.2.1
AverageK returns the average protection threshold across all shards
func (*CloxCache[K, V]) AverageLearnedThresholds ¶ added in v1.0.0
AverageLearnedThresholds returns the average learned rate thresholds across all shards
func (*CloxCache[K, V]) Close ¶
func (c *CloxCache[K, V]) Close()
Close stops background goroutines and waits for them to exit. Safe to call multiple times.
func (*CloxCache[K, V]) GetAdaptiveStats ¶ added in v0.2.1
func (c *CloxCache[K, V]) GetAdaptiveStats() []AdaptiveStats
GetAdaptiveStats returns adaptive threshold stats for all shards
type Config ¶
type Config struct {
NumShards int // Must be power of 2
SlotsPerShard int // Must be power of 2
Capacity int // Max entries (0 = use SlotsPerShard * NumShards as default)
CollectStats bool // Enable hit/miss/eviction counters
// (recommend: 15 for temporal workloads and low latency)
SweepPercent int // Percentage of shard to scan during eviction
}
Config holds CloxCache configuration
func ConfigFromCapacity ¶ added in v1.0.0
ConfigFromCapacity creates a CloxCache config for a specific entry capacity. Automatically configures optimal shard count and slot sizing.
func ConfigFromMemorySize ¶
ConfigFromMemorySize creates a CloxCache config for a specific memory budget. Estimates how many entries fit in the given memory and configures accordingly.
func (Config) EstimateMemoryUsage ¶
EstimateMemoryUsage estimates total memory usage for a given configuration