cache

package
v2.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPrefix

func AddPrefix(key string) string

AddPrefix returns key prefixed with the global cache's key prefix.

func Client

func Client() any

Client returns the underlying client of the global cache.

func Close added in v2.3.0

func Close() error

Close closes the global cache.

func DecrFloat

func DecrFloat(ctx context.Context, key string, value float64) (float64, error)

DecrFloat decrements the float stored at key in the global cache by value and returns the resulting value.

func DecrInt

func DecrInt(ctx context.Context, key string, value int64) (int64, error)

DecrInt decrements the integer stored at key in the global cache by value and returns the resulting value.

func Delete

func Delete(ctx context.Context, keys ...string) (int64, error)

Delete removes the given keys from the global cache and reports how many keys were actually deleted.

func Has

func Has(ctx context.Context, key string) (bool, error)

Has reports whether the entry identified by key exists in the global cache.

func IncrFloat

func IncrFloat(ctx context.Context, key string, value float64) (float64, error)

IncrFloat increments the float stored at key in the global cache by value and returns the resulting value.

func IncrInt

func IncrInt(ctx context.Context, key string, value int64) (int64, error)

IncrInt increments the integer stored at key in the global cache by value and returns the resulting value.

func Set

func Set(ctx context.Context, key string, value any, expiration ...time.Duration) error

Set stores value under key in the global cache. The optional expiration controls the entry's time to live: when it is omitted a random duration within the configured expiration range is used, a value greater than 0 sets an explicit expiration, -1 keeps the existing expiration and a value less than -1 makes the entry never expire.

func SetCache

func SetCache(cache Cache)

SetCache sets the global cache instance. It closes the previously installed instance, if any, and ignores a nil cache.

Types

type Cache

type Cache interface {
	// Has reports whether the entry identified by key exists.
	Has(ctx context.Context, key string) (bool, error)
	// Get returns the value of key. The optional def is returned when the entry does not exist.
	Get(ctx context.Context, key string, def ...any) Result
	// Set stores value under key. The optional expiration controls the entry's time to live: when
	// it is omitted a random duration within the configured expiration range is used, a value
	// greater than 0 sets an explicit expiration, -1 keeps the existing expiration and a value
	// less than -1 makes the entry never expire.
	Set(ctx context.Context, key string, value any, expiration ...time.Duration) error
	// GetSet returns the value of key, generating it with fn and storing it when the entry does not
	// exist.
	GetSet(ctx context.Context, key string, fn SetValueFunc) Result
	// Delete removes the given keys and reports how many keys were actually deleted.
	Delete(ctx context.Context, keys ...string) (int64, error)
	// IncrInt increments the integer stored at key by value and returns the resulting value.
	IncrInt(ctx context.Context, key string, value int64) (int64, error)
	// IncrFloat increments the float stored at key by value and returns the resulting value.
	IncrFloat(ctx context.Context, key string, value float64) (float64, error)
	// DecrInt decrements the integer stored at key by value and returns the resulting value.
	DecrInt(ctx context.Context, key string, value int64) (int64, error)
	// DecrFloat decrements the float stored at key by value and returns the resulting value.
	DecrFloat(ctx context.Context, key string, value float64) (float64, error)
	// AddPrefix returns key prefixed with the cache's key prefix.
	AddPrefix(key string) string
	// Client returns the underlying cache client.
	Client() any
	// Close closes the cache.
	Close() error
}

Cache is the interface implemented by cache backends.

func GetCache

func GetCache() Cache

GetCache returns the global cache instance.

type Result

type Result interface {
	// Err returns the error of the result, if any.
	Err() error
	// Result returns the raw value together with the error, if any.
	Result() (value.Value, error)
	// Int returns the cached value as an int.
	Int() (int, error)
	// Int8 returns the cached value as an int8.
	Int8() (int8, error)
	// Int16 returns the cached value as an int16.
	Int16() (int16, error)
	// Int32 returns the cached value as an int32.
	Int32() (int32, error)
	// Int64 returns the cached value as an int64.
	Int64() (int64, error)
	// Uint returns the cached value as a uint.
	Uint() (uint, error)
	// Uint8 returns the cached value as a uint8.
	Uint8() (uint8, error)
	// Uint16 returns the cached value as a uint16.
	Uint16() (uint16, error)
	// Uint32 returns the cached value as a uint32.
	Uint32() (uint32, error)
	// Uint64 returns the cached value as a uint64.
	Uint64() (uint64, error)
	// Float32 returns the cached value as a float32.
	Float32() (float32, error)
	// Float64 returns the cached value as a float64.
	Float64() (float64, error)
	// Bool returns the cached value as a bool.
	Bool() (bool, error)
	// String returns the cached value as a string.
	String() (string, error)
	// Duration returns the cached value as a time.Duration.
	Duration() (time.Duration, error)
	// Ints returns the cached value as a []int.
	Ints() ([]int, error)
	// Int8s returns the cached value as a []int8.
	Int8s() ([]int8, error)
	// Int16s returns the cached value as a []int16.
	Int16s() ([]int16, error)
	// Int32s returns the cached value as a []int32.
	Int32s() ([]int32, error)
	// Int64s returns the cached value as a []int64.
	Int64s() ([]int64, error)
	// Uints returns the cached value as a []uint.
	Uints() ([]uint, error)
	// Uint8s returns the cached value as a []uint8.
	Uint8s() ([]uint8, error)
	// Uint16s returns the cached value as a []uint16.
	Uint16s() ([]uint16, error)
	// Uint32s returns the cached value as a []uint32.
	Uint32s() ([]uint32, error)
	// Uint64s returns the cached value as a []uint64.
	Uint64s() ([]uint64, error)
	// Float32s returns the cached value as a []float32.
	Float32s() ([]float32, error)
	// Float64s returns the cached value as a []float64.
	Float64s() ([]float64, error)
	// Bools returns the cached value as a []bool.
	Bools() ([]bool, error)
	// Strings returns the cached value as a []string.
	Strings() ([]string, error)
	// Bytes returns the cached value as a []byte.
	Bytes() ([]byte, error)
	// Durations returns the cached value as a []time.Duration.
	Durations() ([]time.Duration, error)
	// Slice returns the cached value as a []any.
	Slice() ([]any, error)
	// Map returns the cached value as a map[string]any.
	Map() (map[string]any, error)
	// Scan scans the cached value into the object pointed to by pointer.
	Scan(pointer any) error
}

Result is the interface that represents a cache read result. It provides methods to convert the cached value into the supported types.

func Get

func Get(ctx context.Context, key string, def ...any) Result

Get returns the value of key from the global cache. The optional def is returned when the entry does not exist.

func GetSet

func GetSet(ctx context.Context, key string, fn SetValueFunc) Result

GetSet returns the value of key from the global cache, generating it with fn and storing it when the entry does not exist.

func NewResult

func NewResult(val any, err ...error) Result

NewResult returns a new Result that holds val and the optional error.

type SetValueFunc

type SetValueFunc func() (any, error)

SetValueFunc is the callback used to generate a cache value. It returns the generated value and an error.

Jump to

Keyboard shortcuts

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