cache

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 3 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 {
	Put(key K, value V)
	Get(key K) (V, bool)
	Delete(key K)
	Stats() Stats
}

func NewNonExpiringMapCache

func NewNonExpiringMapCache[K comparable, V any](capacity int) Cache[K, V]

func NewSieve

func NewSieve[K comparable, V any](capacity int) Cache[K, V]

NewSieve creates a new Sieve cache with the supplied capacity.

If `capacity` is less than or equal to zero, a capacity of one is used to prevent function calls from panicking. The returned value implements the `Cache[K,V]` interface.

type NonExpiringMapCache

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

NonExpiringMapCache

func (*NonExpiringMapCache[K, V]) Delete

func (s *NonExpiringMapCache[K, V]) Delete(key K)

func (*NonExpiringMapCache[K, V]) Get

func (s *NonExpiringMapCache[K, V]) Get(key K) (V, bool)

func (*NonExpiringMapCache[K, V]) Put

func (s *NonExpiringMapCache[K, V]) Put(key K, value V)

func (*NonExpiringMapCache[K, V]) Stats

func (s *NonExpiringMapCache[K, V]) Stats() Stats

type Sieve

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

Sieve implements a clock‑hand (second‑chance) cache with a fixed capacity. It is safe for concurrent use by multiple goroutines.

The cache maintains:

  • a map (`store`) from keys to entries for O(1) lookups,
  • a doubly‑linked list (`queue`) that orders entries from most recently inserted (front) to least recently inserted (back),
  • a “hand” pointer that walks the list during eviction, giving each entry a second chance if it has been visited since the previous sweep.

Statistics about cache usage are collected in the embedded `stats` field.

func (*Sieve[K, V]) Delete

func (s *Sieve[K, V]) Delete(key K)

Delete removes the entry identified by `key` from the cache.

If the entry being removed is currently pointed to by the clock hand, the hand is moved one step backward so that eviction can continue correctly. The method records a deletion in the statistics.

The operation holds an exclusive lock for the duration of the removal.

func (*Sieve[K, V]) Get

func (s *Sieve[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with `key`.

If the key is present, the entry’s visited flag is set, a hit is recorded, and the cached value is returned with `true`. If the key is absent, a miss is recorded and the zero value of V is returned with `false`.

The operation holds a read lock while accessing the map.

func (*Sieve[K, V]) Put

func (s *Sieve[K, V]) Put(key K, value V)

Put adds or updates the value associated with `key`.

If the key already exists, its value is replaced and the entry is marked as visited (so it will survive the next eviction sweep). If the key is new, a fresh entry is inserted, possibly triggering an eviction when the cache is full.

The operation holds an exclusive lock for the duration of the update.

func (*Sieve[K, V]) Stats

func (s *Sieve[K, V]) Stats() Stats

Stats returns a copy of the cache’s current statistics.

The returned `Stats` value can be inspected without holding the cache lock; it contains counters for hits, misses, puts, and deletions as well as the configured capacity.

type Stats

type Stats struct {
	Capacity int
	// contains filtered or unexported fields
}

func NewStats

func NewStats(capacity int) Stats

func (Stats) Combined

func (s Stats) Combined(other Stats) Stats

func (Stats) Delete

func (s Stats) Delete()

func (Stats) Hit

func (s Stats) Hit()

func (Stats) Hits

func (s Stats) Hits() int64

func (Stats) Miss

func (s Stats) Miss()

func (Stats) Misses

func (s Stats) Misses() int64

func (Stats) Put

func (s Stats) Put()

func (Stats) Size

func (s Stats) Size() int64

Jump to

Keyboard shortcuts

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