cache

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides a thread-safe LRU cache with TTL expiry, schema versioning, and singleflight de-duplication of concurrent loads.

WaxTap can run many concurrent extractions that need the same player data, so the cache keeps multiple entries and collapses concurrent loads for the same key into one loader call.

Schema versioning lets a format change invalidate every stale entry at once: bump SchemaVersion and old entries are treated as misses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	MaxEntries    int           // 0 => default
	TTL           time.Duration // 0 => default
	SchemaVersion int           // entries from other versions are misses
}

Options configures a Store. NewStore fills zero values with defaults.

type Store

type Store[V any] struct {
	// contains filtered or unexported fields
}

Store is a generic LRU+TTL cache safe for concurrent use.

func NewStore

func NewStore[V any](opts Options) *Store[V]

NewStore returns a Store with defaults applied.

func (*Store[V]) Get

func (s *Store[V]) Get(key string) (V, bool)

Get returns the cached value for key if present, unexpired, and from the current schema version.

func (*Store[V]) GetOrLoad

func (s *Store[V]) GetOrLoad(ctx context.Context, key string, load func(context.Context) (V, error)) (V, error)

GetOrLoad returns the cached value for key, or loads it exactly once across concurrent callers (singleflight) and caches the result. The loader's error is returned to all waiters and is not cached.

func (*Store[V]) Len

func (s *Store[V]) Len() int

Len returns the number of live entries (including any expired-but-not-yet- evicted ones).

func (*Store[V]) Put

func (s *Store[V]) Put(key string, val V)

Put stores val under key with the configured TTL, evicting the least-recently used entry if at capacity.

Jump to

Keyboard shortcuts

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