cache

package
v0.30.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides a simple key-value cache interface with LRU eviction and TTL support.

The package defines two interfaces:

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

func NewLRU(opts LRUOpts) *LRU

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.

func (*LRU) Delete

func (L *LRU) Delete(key string)

Delete removes an entry by key. No-op if the key doesn't exist.

func (*LRU) Get

func (L *LRU) Get(key string) (any, bool)

Get retrieves a value by key. Returns false if not found or expired. Accessing an entry moves it to the front of the LRU list.

func (*LRU) Put

func (L *LRU) Put(key string, val any, opts ...PutOption)

Put stores a value with optional TTL. If the key already exists, the value is updated and moved to the front of the LRU list. If the cache is full, the least recently used entry is evicted.

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 Nop

type Nop struct{}

func NewNop

func NewNop() *Nop

func (*Nop) Delete

func (n *Nop) Delete(key string)

func (*Nop) Get

func (n *Nop) Get(key string) (any, bool)

func (*Nop) Put

func (n *Nop) Put(key string, val any, opts ...PutOption)

type PutOption

type PutOption func(*PutOptions)

PutOption is a functional option for configuring cache Put operations.

func WithTTL

func WithTTL(ttl time.Duration) PutOption

WithTTL sets the time-to-live for a cache entry. After the TTL expires, the entry will not be returned by Get.

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.

Jump to

Keyboard shortcuts

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