Documentation
¶
Overview ¶
Package cache provides a simple key-value cache interface with LRU eviction and TTL support.
The package defines two interfaces:
- Cache: Untyped cache storing values as any
- TypedCache: Generic type-safe wrapper via NewTyped
Implementations ¶
LRU provides an in-memory LRU cache that is safe for concurrent use. It runs a background goroutine for cache operations, ensuring thread safety without external locking.
cache := cache.NewLRU(cache.LRUOpts{Size: 1000})
defer cache.Close()
cache.Put("key", value, cache.WithTTL(5*time.Minute))
if val, ok := cache.Get("key"); ok {
// Use val
}
Type-Safe Usage ¶
Use NewTyped for compile-time type safety:
userCache := cache.NewTyped[*User](lruCache)
userCache.Put("user:123", user)
if user, ok := userCache.Get("user:123"); ok {
// user is *User, no type assertion needed
}
TTL Support ¶
Use WithTTL to set per-entry expiration:
cache.Put("session", data, cache.WithTTL(30*time.Minute))
Expired entries are lazily evicted on access.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value by key. Returns false if not found or expired.
Get(key string) (any, bool)
// Put stores a value with optional TTL.
Put(key string, val any, opts ...PutOption)
// Delete removes a value by key.
Delete(key string)
}
Cache is the interface for a key-value cache with string keys and any values.
type LRU ¶
type LRU struct {
// contains filtered or unexported fields
}
LRU is a thread-safe in-memory cache with LRU eviction and TTL support. It processes all operations through a single goroutine to ensure consistency without external synchronization.
Create with NewLRU and call LRU.Close when done to stop the background goroutine.
func NewLRU ¶
NewLRU creates a new LRU cache with the specified options. It starts a background goroutine that must be stopped by calling LRU.Close.
func (*LRU) Close ¶
func (L *LRU) Close()
Close stops the background goroutine. After Close returns, all operations become no-ops. Close is idempotent.
type LRUOpts ¶
type LRUOpts struct {
// Size is the maximum number of entries in the cache.
// When exceeded, the least recently used entry is evicted.
// Defaults to 128 if not specified or <= 0.
Size int
}
LRUOpts configures the LRU cache behavior.
type PutOption ¶
type PutOption func(*PutOptions)
PutOption is a functional option for configuring cache Put operations.
type PutOptions ¶
type PutOptions struct {
// TTL specifies how long the entry should live before expiring.
// Zero means no expiration.
TTL time.Duration
}
PutOptions configures cache entry storage behavior.
type TypedCache ¶
type TypedCache[T any] interface { // Put stores a value with optional TTL. Put(key string, val T, opts ...PutOption) // Get retrieves a value by key. Returns false if not found, expired, or wrong type. Get(key string) (T, bool) // Delete removes a value by key. Delete(key string) }
TypedCache is a type-safe cache interface parameterized by value type T.
func NewTyped ¶
func NewTyped[T any](c Cache) TypedCache[T]
NewTyped wraps a Cache with type-safe operations for type T. Values stored via the returned TypedCache can only be retrieved as type T.