cache

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTTL = 1 * time.Hour

Functions

func Get

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

Types

type Cache

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

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 interface {
	Add(key string, value any, 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

Jump to

Keyboard shortcuts

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