Documentation
¶
Overview ¶
Package lru provides a generic in-memory LRU cache with optional TTL (expiration). It uses hashicorp/golang-lru/v2 as the underlying LRU implementation and adds per-entry expiration on top.
The Cache[K, V] type is generic over key and value types. It implements cache.Cache[K, V].
Basic usage:
c, _ := lru.New[string, string](1024, lru.WithDefaultTTL(10*time.Second)) c.Set(context.Background(), "foo", "bar", 0) val, _ := c.Get(context.Background(), "foo")
Index ¶
- type Cache
- func (c *Cache[K, V]) Clear(ctx context.Context) error
- func (c *Cache[K, V]) Close() error
- func (c *Cache[K, V]) Contains(key K) bool
- func (c *Cache[K, V]) Delete(ctx context.Context, key K) error
- func (c *Cache[K, V]) Exists(ctx context.Context, key K) (bool, error)
- func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, error)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration) error
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a generic in-memory LRU cache with optional per-entry TTL. It implements cache.Cache[K, V].
func New ¶
New creates an LRU cache with the given capacity (number of entries) and options. capacity must be greater than zero.
WithDefaultTTL sets a TTL applied to entries when Set is called with ttl == 0. A zero default TTL means entries never expire unless a positive ttl is passed to Set.
WithCleanupInterval enables a background goroutine that periodically purges expired entries. Pass 0 to disable (expired entries are still removed on access).
func (*Cache[K, V]) Get ¶
Get returns the value for key, or cache.ErrNotFound if missing or expired.
type Option ¶
type Option func(*config)
Option configures an LRU cache.
func WithCleanupInterval ¶
WithCleanupInterval sets how often expired entries are purged. Pass 0 to disable background cleanup (expired entries are still removed on access).
func WithDefaultTTL ¶
WithDefaultTTL sets the default TTL used when Set is called with ttl == 0.