cache

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Options

func Options() *options

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

func (c *LRUCache[K]) Fetch(key K) (any, bool)

Fetch retrieves a value from the cache. If found, the item is promoted to Most Recently Used (MRU).

func (*LRUCache[K]) ForEach

func (c *LRUCache[K]) ForEach(f func(key K, value any) error) error

ForEach iterates over the cache items.

func (*LRUCache[K]) Has

func (c *LRUCache[K]) Has(key K) bool

Has checks if an item exists in the cache without moving its MRU status.

func (*LRUCache[K]) Size

func (c *LRUCache[K]) Size() int

Size returns the number of items in the cache.

func (*LRUCache[K]) Store

func (c *LRUCache[K]) Store(key K, value any, opts *options) bool

Store adds a value to the cache, applying any provided options.

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

func (c *TTLCache[K]) Fetch(key K) (any, bool)

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

func (c *TTLCache[K]) ForEach(f func(key K, value any) error) error

ForEach iterates over the cache items.

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.

func (*TTLCache[K]) Has

func (c *TTLCache[K]) Has(key K) bool

Has checks if an item exists in the cache and is not expired.

func (*TTLCache[K]) Size

func (c *TTLCache[K]) Size() int

Size returns the total number of items across all shards.

func (*TTLCache[K]) Store

func (c *TTLCache[K]) Store(key K, value any, opts *options) bool

┌─────────────┐ │ PUBLIC APIs │ └─────────────┘ Store adds an item to the cache, replacing any existing item. If ttl is 0 or negative, the item will never expire (passive-only).

type TTLCacheAttrs

type TTLCacheAttrs struct {
	NumOfShards     uint8
	CleanupInterval time.Duration
	HashFunc        func(key any) uint64
}

Jump to

Keyboard shortcuts

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