twcache

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] interface {
	Add(key K, value V) (evicted bool)
	Get(key K) (value V, ok bool)
	Purge()
}

Cache defines a generic interface for a key-value storage with type constraints on keys and values. The keys must be of a type that supports comparison. Add inserts a new key-value pair, potentially evicting an item if necessary. Get retrieves a value associated with the given key, returning a boolean to indicate if the key was found. Purge clears all items from the cache.

type EvictCallback

type EvictCallback[K comparable, V any] func(key K, value V)

EvictCallback is a function called when an entry is evicted. This includes evictions during Purge or Resize operations.

type LRU

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

LRU is a thread-safe, generic LRU cache with a fixed size. It has zero dependencies, high performance, and full features.

func NewLRU

func NewLRU[K comparable, V any](size int) *LRU[K, V]

NewLRU creates a new LRU cache with the given size. Returns nil if size <= 0, acting as a disabled cache. Caps size at 100,000 for reasonableness.

func NewLRUEvict

func NewLRUEvict[K comparable, V any](size int, onEvict EvictCallback[K, V]) *LRU[K, V]

NewLRUEvict creates a new LRU cache with an eviction callback. The callback is optional and called on evictions. Returns nil if size <= 0.

func (*LRU[K, V]) Add

func (c *LRU[K, V]) Add(key K, value V) (evicted bool)

Add inserts or updates a key-value pair. Evicts the oldest if cache is full. Returns true if an eviction occurred.

func (*LRU[K, V]) Cap

func (c *LRU[K, V]) Cap() int

Cap returns the maximum capacity of the cache.

func (*LRU[K, V]) Get

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

Get retrieves a value by key if it exists. Returns the value and true if found, else zero and false. Updates the entry to most recently used.

func (*LRU[K, V]) GetOrCompute

func (c *LRU[K, V]) GetOrCompute(key K, compute func() V) V

GetOrCompute retrieves a value or computes it if missing. Ensures no double computation under concurrency. Ideal for expensive computations like twwidth.

func (*LRU[K, V]) HitRate

func (c *LRU[K, V]) HitRate() float64

HitRate returns the cache hit ratio (0.0 to 1.0). Based on hits / (hits + misses).

func (*LRU[K, V]) Len

func (c *LRU[K, V]) Len() int

Len returns the current number of items in the cache.

func (*LRU[K, V]) Purge

func (c *LRU[K, V]) Purge()

Purge clears all entries from the cache. Calls onEvict for each entry if set. Resets hit/miss counters.

func (*LRU[K, V]) Remove

func (c *LRU[K, V]) Remove(key K) bool

Remove deletes a key from the cache. Returns true if the key was found and removed.

func (*LRU[K, V]) RemoveOldest

func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)

RemoveOldest removes and returns the least recently used item. Returns key, value, and true if an item was removed. Calls onEvict if set.

Jump to

Keyboard shortcuts

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