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 Item ¶
type Item[V any] struct { // contains filtered or unexported fields }
func (*Item[V]) GetValueWithOptions ¶
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 ¶
NewTTLMap creates a TTLMap that holds at most maxSize entries and assigns the given defaultTTL to every new entry.
func (*TTLMap[V]) Get ¶
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 ¶
Len returns the number of entries (including expired but not yet cleaned up).
type ValueRetriever ¶
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.