Documentation
¶
Index ¶
- func Options() *options
- type Cache
- type LRUCache
- type TTLCache
- func (c *TTLCache[K]) Evict(key K)
- func (c *TTLCache[K]) Fetch(key K) (any, bool)
- func (c *TTLCache[K]) ForEach(f func(key K, value any) error) error
- func (c *TTLCache[K]) ForceCleanup()
- func (c *TTLCache[K]) Has(key K) bool
- func (c *TTLCache[K]) Size() int
- func (c *TTLCache[K]) Store(key K, value any, opts *options) bool
- type TTLCacheAttrs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache[K comparable] interface { // Fetch retrieves a value from the cache. Fetch(key K) (any, bool) // Store adds a value to the cache, applying any provided options. Store(key K, value any, opts *options) bool Evict(key K) Has(key K) bool // ForEach iterates over the cache items. ForEach(f func(key K, value any) error) error // Size returns the number of items in the cache. Size() int }
Cache is the unified interface for all cache implementations. The Set method accepts a variadic list of options.
func NewLRUCache ¶
func NewLRUCache[K comparable]( capacity int, onInvalidate func(key K, value any), ) Cache[K]
NewLRUCache creates a new LRU Cache instance. Capacity must be greater than zero.
type LRUCache ¶
type LRUCache[K comparable] struct { // contains filtered or unexported fields }
LRUCache is a concurrent, fixed-size cache with an LRU eviction policy.
func (*LRUCache[K]) Evict ¶
func (c *LRUCache[K]) Evict(key K)
Evict removes an item from the cache.
func (*LRUCache[K]) Fetch ¶
Fetch retrieves a value from the cache. If found, the item is promoted to Most Recently Used (MRU).
type TTLCache ¶
type TTLCache[K comparable] struct { // contains filtered or unexported fields }
TTLCache is a high-performance, sharded, generic TTL cache.
func NewTTLCache ¶
func NewTTLCache[K comparable]( attrs TTLCacheAttrs, ) *TTLCache[K]
NewTTLCache creates a new sharded TTL cache with a background janitor goroutine. numShards specifies the number of shards to create and must be greater than 0. cleanupInterval specifies how often the janitor should run.
func (*TTLCache[K]) Evict ¶
func (c *TTLCache[K]) Evict(key K)
Evict removes an item from the cache.
func (*TTLCache[K]) Fetch ¶
Fetch retrieves an item from the cache. It returns the item (of type T) and true if found and not expired. Otherwise, it returns the zero value of T and false.
func (*TTLCache[K]) ForceCleanup ¶
func (c *TTLCache[K]) ForceCleanup()
ForceCleanup actively scans all shards and deletes expired items. This is called periodically by the janitor but can also be called manually.