cache

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMemory

func FormatMemory(bytes uint64) string

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

type CloxCache[K Key, V any] struct {
	// contains filtered or unexported fields
}

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

func NewCloxCache[K Key, V any](cfg Config) *CloxCache[K, V]

NewCloxCache creates a new cache with the given configuration

func (*CloxCache[K, V]) AverageK added in v0.2.1

func (c *CloxCache[K, V]) AverageK() float64

AverageK returns the average protection threshold across all shards

func (*CloxCache[K, V]) AverageLearnedThresholds added in v1.0.0

func (c *CloxCache[K, V]) AverageLearnedThresholds() (rateLow, rateHigh float64)

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]) Get

func (c *CloxCache[K, V]) Get(key K) (V, bool)

Get retrieves a value from the cache (lock-free)

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

func (*CloxCache[K, V]) Put

func (c *CloxCache[K, V]) Put(key K, value V) bool

Put inserts or updates a value in the cache

func (*CloxCache[K, V]) Stats

func (c *CloxCache[K, V]) Stats() (hits, misses, evictions uint64)

Stats return cache statistics

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

func ConfigFromCapacity(capacity int) Config

ConfigFromCapacity creates a CloxCache config for a specific entry capacity. Automatically configures optimal shard count and slot sizing.

func ConfigFromMemorySize

func ConfigFromMemorySize(targetBytes uint64) Config

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

func (c Config) EstimateMemoryUsage() uint64

EstimateMemoryUsage estimates total memory usage for a given configuration

type Key

type Key interface {
	~string | ~[]byte
}

Key is a type constraint for cache keys (string or []byte)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL