tinylfu

package
v0.0.0-...-31ab89b Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: AGPL-3.0 Imports: 5 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] struct {
	// contains filtered or unexported fields
}

Cache represents a TinyLFU cache with Count-Min Sketch for frequency estimation

func New

func New[K comparable, V any](capacity int) (*Cache[K, V], error)

New creates a new TinyLFU cache with the given capacity

func NewWithEvict

func NewWithEvict[K comparable, V any](capacity int, onEvict func(K, V)) (*Cache[K, V], error)

NewWithEvict creates a new TinyLFU cache with eviction callback

func (*Cache[K, V]) Cap

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

Cap returns the capacity of the cache

func (*Cache[K, V]) Clear

func (c *Cache[K, V]) Clear()

Clear removes all items from the cache

func (*Cache[K, V]) Contains

func (c *Cache[K, V]) Contains(key K) bool

Contains checks if a key exists in the cache without updating its position

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (value V, ok bool)

Get retrieves a value from the cache

func (*Cache[K, V]) Items

func (c *Cache[K, V]) Items() map[K]V

Items returns all key-value pairs in the cache

func (*Cache[K, V]) Keys

func (c *Cache[K, V]) Keys() []K

Keys returns all keys in the cache

func (*Cache[K, V]) Len

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

Len returns the number of items in the cache

func (*Cache[K, V]) Peek

func (c *Cache[K, V]) Peek(key K) (value V, ok bool)

Peek returns a value without updating its position in the cache

func (*Cache[K, V]) Put

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

Put adds or updates a value in the cache

func (*Cache[K, V]) Remove

func (c *Cache[K, V]) Remove(key K) (value V, ok bool)

Remove removes a key from the cache

func (*Cache[K, V]) Resize

func (c *Cache[K, V]) Resize(capacity int) error

Resize changes the capacity of the cache

func (*Cache[K, V]) Stats

func (c *Cache[K, V]) Stats() Stats

Stats returns cache statistics

func (*Cache[K, V]) Values

func (c *Cache[K, V]) Values() []V

Values returns all values in the cache

type CountMinSketch

type CountMinSketch struct {
	// contains filtered or unexported fields
}

CountMinSketch implements frequency estimation

func NewCountMinSketch

func NewCountMinSketch(capacity int) *CountMinSketch

NewCountMinSketch creates a new Count-Min Sketch

func (*CountMinSketch) Add

func (cms *CountMinSketch) Add(key []byte)

Add increments the frequency count for a key

func (*CountMinSketch) EstimateCount

func (cms *CountMinSketch) EstimateCount(key []byte) int

EstimateCount returns the estimated frequency of a key

func (*CountMinSketch) Reset

func (cms *CountMinSketch) Reset()

Reset halves all counters (aging)

func (*CountMinSketch) Size

func (cms *CountMinSketch) Size() int

Size returns the number of items added to the sketch

type Stats

type Stats struct {
	Size           int // actual cache size
	Capacity       int // maximum cache capacity
	WindowSize     int // current window segment size
	ProbationSize  int // current probation segment size
	ProtectedSize  int // current protected segment size
	WindowCapacity int // window segment capacity
	MainCapacity   int // main cache capacity (probation + protected)
	SketchSize     int // frequency sketch size
	DoorkeeperSize int // doorkeeper (bloom filter) size
}

Stats represents cache statistics

Jump to

Keyboard shortcuts

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