ttlcache

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ttlcache provides a TTL-based cache for values that can be refreshed on expiry (e.g. OAuth tokens, API metadata).

It solves the problem of caching values with a time-to-live and optional refresh: Item wraps a ValueRetriever and returns the cached value until it expires, then re-invokes the retriever. Used by Langfuse, MCP, and other components that need short-lived cached data. Without this package, each would implement its own caching and expiry logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GetOption

type GetOption struct {
	ForceRefresh bool
}

type Item

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

func NewItem

func NewItem[V any](retriever ValueRetriever[V], duration time.Duration) *Item[V]

func (*Item[V]) GetValue

func (i *Item[V]) GetValue(ctx context.Context) (V, error)

func (*Item[V]) GetValueWithOptions

func (i *Item[V]) GetValueWithOptions(ctx context.Context, opts GetOption) (V, error)

func (*Item[V]) KeepItFresh

func (i *Item[V]) KeepItFresh(ctx context.Context) error

type TTLMap

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

TTLMap is a thread-safe, bounded key-value cache with per-entry TTL and LRU eviction. Entries expire after DefaultTTL and are lazily cleaned up on access. When the map reaches MaxSize, the least-recently-used entry is evicted to make room.

Usage:

m := ttlcache.NewTTLMap[string](128, 2*time.Minute)
m.Set("key", "value")
val, ok := m.Get("key") // ok=true if present and not expired

func NewTTLMap

func NewTTLMap[V any](maxSize int, defaultTTL time.Duration) *TTLMap[V]

NewTTLMap creates a TTLMap that holds at most maxSize entries and assigns the given defaultTTL to every new entry.

func (*TTLMap[V]) Delete

func (m *TTLMap[V]) Delete(key string)

Delete removes a key from the cache.

func (*TTLMap[V]) Get

func (m *TTLMap[V]) Get(key string) (V, bool)

Get retrieves the value for key. Returns (value, true) if the key exists and has not expired, or (zero, false) otherwise. Expired entries are removed lazily on access. A successful Get promotes the entry to the front (MRU).

func (*TTLMap[V]) Len

func (m *TTLMap[V]) Len() int

Len returns the number of entries (including expired but not yet cleaned up).

func (*TTLMap[V]) Set

func (m *TTLMap[V]) Set(key string, value V)

Set stores a value for the given key with the default TTL. If the key already exists, the value and expiry are updated and the entry is promoted. If the map is at capacity, the least-recently-used entry is evicted.

func (*TTLMap[V]) SetWithTTL

func (m *TTLMap[V]) SetWithTTL(key string, value V, ttl time.Duration)

SetWithTTL stores a value with a custom TTL.

type ValueRetriever

type ValueRetriever[V any] func(context.Context) (V, error)

ValueRetriever is a function type used to retrieve or refresh cached values. It is called by the cache to obtain the value for an item, and may be invoked multiple times throughout the cache item's lifetime, such as when the value expires or needs to be refreshed.

Jump to

Keyboard shortcuts

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