cache

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTTL = 1 * time.Hour
View Source
var ErrNotFound = errors.New("key not found")

ErrNotFound is returned by backends when a key is missing or expired. Backends may alias this or wrap it; callers should check with errors.Is.

Functions

func Get

func Get[K ~string, V any](
	cache Cache[K, V],
	key K,
	fetcher func() (V, error),
	options ...CacheConfigFunc,
) (V, error)

Types

type Cache

type Cache[K ~string, V any] interface {
	Claimer[K, V]
	Set(key K, value V, ttl time.Duration) error
	Get(key K) (V, error)
	Remove(key ...K) error
	Pop(key K) (V, error)
	Keys(prefix K) []K
}

type CacheConfigFunc

type CacheConfigFunc func(*config)

func AsyncSetter

func AsyncSetter(value bool) CacheConfigFunc

func OnError

func OnError(fn func(err error)) CacheConfigFunc

func TTL

func TTL(duration time.Duration) CacheConfigFunc

type Claimer added in v0.11.0

type Claimer[K ~string, V any] interface {
	Add(key K, value V, ttl time.Duration) (bool, error)
}

Claimer is an optional capability for caches that can perform atomic set-if-absent. Backends that cannot provide this guarantee should not implement it.

Add inserts key→value only if the key is absent or expired. It returns true if this caller won the claim, false if another caller already holds an unexpired entry. Use this for deduplication or leader-election where a Get-then-Set sequence would race.

Directories

Path Synopsis
Package pgx provides a PostgreSQL implementation of cache.Cache.
Package pgx provides a PostgreSQL implementation of cache.Cache.
Package redis provides a Redis implementation of cache.Cache.
Package redis provides a Redis implementation of cache.Cache.

Jump to

Keyboard shortcuts

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