cache

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 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 {
	// Get retrieves a value from the cache.
	Get(key K) (any, bool)
	// Set adds a value to the cache, applying any provided options.
	Set(key K, value any, opts *options) bool
	Delete(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]) Delete added in v1.5.0

func (c *LRUCache[K]) Delete(key K)

Delete removes an item from the cache.

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]) Get added in v1.5.0

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

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

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]) Set added in v1.5.0

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

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

func (*LRUCache[K]) Size

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

Size returns the number of items in the cache.

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]) Delete added in v1.5.0

func (c *TTLCache[K]) Delete(key K)

Delete removes an item from the cache.

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]) Get added in v1.5.0

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

Get 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]) 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]) Set added in v1.5.0

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

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

func (*TTLCache[K]) Size

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

Size returns the total number of items across all shards.

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