Documentation
¶
Overview ¶
Package cache defines a unified generic caching interface and shared errors for in-memory and distributed cache backends.
The Cache[K, V] interface is generic over key and value types. In-memory backends (lru, memory, ristretto) can use arbitrary types; distributed backends (redis, memcache, bigcache, freecache) typically use Cache[string, []byte].
Index ¶
- Variables
- func GetJSON(ctx context.Context, c ByteCache, key string, dest any) error
- func GetOrSet(ctx context.Context, c ByteCache, key string, ttl time.Duration, ...) ([]byte, error)
- func GetString(ctx context.Context, c ByteCache, key string) (string, error)
- func SetJSON(ctx context.Context, c ByteCache, key string, value any, ttl time.Duration) error
- func SetString(ctx context.Context, c ByteCache, key, value string, ttl time.Duration) error
- type ByteCache
- type Cache
- type Getter
- type Option
- type Options
- type Setter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned when a key does not exist or has expired. ErrNotFound = errors.New("cache: key not found") // ErrClosed is returned when an operation is attempted on a closed cache. ErrClosed = errors.New("cache: closed") // ErrInvalidCapacity is returned when capacity is less than or equal to zero. ErrInvalidCapacity = errors.New("cache: capacity must be greater than zero") // ErrEmptyKey is returned when a key is empty. ErrEmptyKey = errors.New("cache: key must not be empty") )
Functions ¶
func GetOrSet ¶
func GetOrSet(ctx context.Context, c ByteCache, key string, ttl time.Duration, fn func(context.Context) ([]byte, error)) ([]byte, error)
GetOrSet returns the cached value for key, or calls fn to produce and store it.
Types ¶
type ByteCache ¶
ByteCache is a type alias for the common distributed-cache specialization: string keys and []byte values.
type Cache ¶
type Cache[K comparable, V any] interface { // Get returns the value for key. Returns ErrNotFound if missing or expired. Get(ctx context.Context, key K) (V, error) // Set stores value under key. A ttl of 0 means no expiration (backend permitting). Set(ctx context.Context, key K, value V, ttl time.Duration) error // Delete removes key. It is not an error if the key does not exist. Delete(ctx context.Context, key K) error // Exists reports whether key is present and not expired. Exists(ctx context.Context, key K) (bool, error) // Clear removes all entries from the cache. Clear(ctx context.Context) error // Close releases resources held by the cache. Close() error }
Cache is the common generic interface implemented by all cache backends.
In-memory backends can use arbitrary K and V types; distributed backends typically use Cache[string, []byte].
type Getter ¶
type Getter[K comparable, V any] interface { Get(ctx context.Context, key K) (V, error) Exists(ctx context.Context, key K) (bool, error) }
Getter is a read-only view of a cache.
type Option ¶
type Option func(*Options)
Option mutates Options.
func WithDefaultTTL ¶
WithDefaultTTL sets the default TTL used when Set is called with ttl == 0.
type Options ¶
type Options struct {
// Prefix is prepended to every key before it is sent to the backend.
Prefix string
// DefaultTTL is applied by Set when the caller passes ttl == 0 and the
// backend supports expiration. A zero DefaultTTL means "no expiration".
DefaultTTL time.Duration
}
Options holds shared configuration knobs used by cache backends.
func ApplyOptions ¶
ApplyOptions builds Options from the given Option list.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package lru provides a generic in-memory LRU cache with optional TTL (expiration).
|
Package lru provides a generic in-memory LRU cache with optional TTL (expiration). |
|
Package memory provides a simple concurrent in-memory cache with TTL.
|
Package memory provides a simple concurrent in-memory cache with TTL. |
|
Package multilevel composes an L1 (local) and L2 (remote) cache.
|
Package multilevel composes an L1 (local) and L2 (remote) cache. |
|
Package noop provides a no-op cache useful for tests and feature flags.
|
Package noop provides a no-op cache useful for tests and feature flags. |