cache

package
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMemoryCacheSweepInterval is the default interval for sweeping expired items from the cache.
	DefaultMemoryCacheSweepInterval = time.Minute * 5
)
View Source
const (
	// DefaultSweepInterval Cache default sweep interval
	DefaultSweepInterval = time.Second * 3
)

Variables

This section is empty.

Functions

func NewLRUCache

func NewLRUCache[V any](data V, cacheDuration time.Duration) *lruCache[V]

NewLRUCache creates a new lruCache entry with the given value and TTL

func NewMemoryCache

func NewMemoryCache(data interface{}, cacheDuration time.Duration) *memoryCache

NewMemoryCache new memory cache instance

Types

type Cache

type Cache[K comparable] interface {
	// Get retrieves a value from the cache by key.
	Get(key K) (value interface{}, found bool)

	// Put adds a value to the cache with the specified key and expiration time.
	Put(key K, value interface{}, expiration time.Duration)

	// Delete removes a value from the cache by key.
	Delete(key K)
}

Cache interface defines the methods for a cache implementation.

type EvictionReason

type EvictionReason string

EvictionReason represents the reason for evicting an item from the cache.

const (
	// EvictCapacity indicates that the item was evicted due to reaching the maximum capacity of the cache.
	EvictCapacity EvictionReason = "capacity"
	// EvictTTL indicates that the item was evicted due to exceeding its time-to-live (TTL).
	EvictTTL EvictionReason = "TTL"
	// EvictReplace indicates that the item was evicted due to being replaced by a new value.
	EvictReplace EvictionReason = "replace"
	// EvictDelete indicates that the item was evicted due to being explicitly deleted from the cache.
	EvictDelete EvictionReason = "delete"
	// EvictPurge indicates that the item was evicted due to a purge operation on the cache.
	EvictPurge EvictionReason = "purge"
)

type LRUCache

type LRUCache[K comparable, V any] interface {
	// Get retrieves a value from the cache by key.
	Get(key K) (value V, found bool)

	// Put adds a value to the cache with the specified key and expiration time.
	Put(key K, value V, expiration time.Duration)

	// Delete removes a value from the cache by key.
	Delete(key K)
}

LRUCache interface defines the methods for a Least Recently Used (LRU) cache implementation.

type LRUStore

type LRUStore[K comparable, V any] struct {

	// onEvict callback when an entry is evicted.
	OnEvict func(key K, value V, reason EvictionReason)
	// contains filtered or unexported fields
}

LRUStore is a thread-safe LRU cache implementation that supports expiration and memory size limits.

func NewLRUStore

func NewLRUStore[K comparable, V any](ctx context.Context, opts Options[K, V]) (*LRUStore[K, V], error)

NewLRUStore creates a new LRUStore with the given options.

func (*LRUStore[K, V]) CurrentBytes

func (l *LRUStore[K, V]) CurrentBytes() int64

CurrentBytes returns the current memory usage of the cache in bytes.

func (*LRUStore[K, V]) Delete

func (l *LRUStore[K, V]) Delete(key K)

Delete removes a value from the cache by key.

func (*LRUStore[K, V]) Get

func (l *LRUStore[K, V]) Get(key K) (value V, found bool)

Get retrieves a value from the cache by key.

func (*LRUStore[K, V]) Purge

func (l *LRUStore[K, V]) Purge()

Purge removes all items from the cache.

func (*LRUStore[K, V]) Put

func (l *LRUStore[K, V]) Put(key K, value V, cacheDuration time.Duration)

Put adds a value to the cache with the specified key and expiration time.

type MemoryCacheStore

type MemoryCacheStore[K comparable] struct {
	// contains filtered or unexported fields
}

MemoryCacheStore memory cache store

func NewMemoryCacheStore

func NewMemoryCacheStore[K comparable](ctx context.Context) *MemoryCacheStore[K]

NewMemoryCacheStore memory cache store

func (*MemoryCacheStore[K]) Delete

func (m *MemoryCacheStore[K]) Delete(key K)

Delete cache data from store

func (*MemoryCacheStore[K]) Get

func (m *MemoryCacheStore[K]) Get(key K) (value interface{}, found bool)

Get cache data from store, if cache data is expired, return nil

func (*MemoryCacheStore[K]) Put

func (m *MemoryCacheStore[K]) Put(key K, value interface{}, cacheDuration time.Duration)

Put cache data, if cacheDuration>0, store will clear data after timeout.

type Options

type Options[K comparable, V any] struct {
	// MaxSize defines the maximum number of items the cache can hold.
	MaxSize int // 0 = unlimited count
	// MaxBytes defines the maximum bytes the cache can hold.
	MaxBytes int64 // 0 = unlimited bytes
	// SizeOf is a function that calculates the byte usage of the value
	SizeOf func(key K, value V) int64 // required if MaxBytes > 0
	// OnEvict is a callback function that is called when an item is evicted from the cache.
	OnEvict func(key K, value V, reason EvictionReason)
	// SweepInterval defines the interval at which the cache will sweep for expired items.
	SweepInterval time.Duration
}

Options struct defines the configuration options for the LRU cache.

Jump to

Keyboard shortcuts

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