cache

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 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 添加Key前缀 @param key string 缓存键 @return @1 string 添加前缀后的完整键名

func Client

func Client() any

Client 获取客户端 @return @1 any 底层缓存客户端

func Close added in v2.3.0

func Close() error

Close 关闭缓存 @return @1 error 错误信息

func DecrFloat

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

DecrFloat 浮点数自减 @param ctx context.Context 上下文 @param key string 缓存键 @param value float64 自减步长 @return @1 float64 自减后的值 @return @2 error 错误信息

func DecrInt

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

DecrInt 整数自减 @param ctx context.Context 上下文 @param key string 缓存键 @param value int64 自减步长 @return @1 int64 自减后的值 @return @2 error 错误信息

func Delete

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

Delete 删除缓存 @param ctx context.Context 上下文 @param keys ...string 缓存键,可传入多个 @return @1 int64 实际删除的 key 数量 @return @2 error 错误信息

func Has

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

Has 检测缓存是否存在 @param ctx context.Context 上下文 @param key string 缓存键 @return @1 bool 缓存是否存在 @return @2 error 错误信息

func IncrFloat

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

IncrFloat 浮点数自增 @param ctx context.Context 上下文 @param key string 缓存键 @param value float64 自增步长 @return @1 float64 自增后的值 @return @2 error 错误信息

func IncrInt

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

IncrInt 整数自增 @param ctx context.Context 上下文 @param key string 缓存键 @param value int64 自增步长 @return @1 int64 自增后的值 @return @2 error 错误信息

func Set

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

Set 设置缓存值 @param ctx context.Context 上下文 @param key string 缓存键 @param value any 缓存值 @param expiration ...time.Duration 过期时间,省略表示使用过期时间范围随机,>0表示使用具体的过期时间,=-1表示保持原有过期时间,<-1表示永不过期 @return @1 error 错误信息

func SetCache

func SetCache(cache Cache)

SetCache 设置全局缓存实例 @param cache Cache 缓存实例

Types

type Cache

type Cache interface {
	// Has 检测缓存是否存在
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @return @1 bool 缓存是否存在
	// @return @2 error 错误信息
	Has(ctx context.Context, key string) (bool, error)
	// Get 获取缓存值
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param def ...any 可选默认值,当缓存不存在时返回该默认值
	// @return @1 Result 缓存结果
	Get(ctx context.Context, key string, def ...any) Result
	// Set 设置缓存值
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param value any 缓存值
	// @param expiration ...time.Duration 过期时间,省略表示使用过期时间范围随机,>0表示使用具体的过期时间,=-1表示保持原有过期时间,<-1表示永不过期
	// @return @1 error 错误信息
	Set(ctx context.Context, key string, value any, expiration ...time.Duration) error
	// GetSet 获取缓存值,若不存在则通过 fn 生成后写入并返回
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param fn SetValueFunc 缓存未命中时执行的回调,用于生成缓存值
	// @return @1 Result 缓存结果
	GetSet(ctx context.Context, key string, fn SetValueFunc) Result
	// Delete 删除缓存
	// @param ctx context.Context 上下文
	// @param keys ...string 缓存键,可传入多个
	// @return @1 int64 实际删除的 key 数量
	// @return @2 error 错误信息
	Delete(ctx context.Context, keys ...string) (int64, error)
	// IncrInt 整数自增
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param value int64 自增步长
	// @return @1 int64 自增后的值
	// @return @2 error 错误信息
	IncrInt(ctx context.Context, key string, value int64) (int64, error)
	// IncrFloat 浮点数自增
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param value float64 自增步长
	// @return @1 float64 自增后的值
	// @return @2 error 错误信息
	IncrFloat(ctx context.Context, key string, value float64) (float64, error)
	// DecrInt 整数自减
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param value int64 自减步长
	// @return @1 int64 自减后的值
	// @return @2 error 错误信息
	DecrInt(ctx context.Context, key string, value int64) (int64, error)
	// DecrFloat 浮点数自减
	// @param ctx context.Context 上下文
	// @param key string 缓存键
	// @param value float64 自减步长
	// @return @1 float64 自减后的值
	// @return @2 error 错误信息
	DecrFloat(ctx context.Context, key string, value float64) (float64, error)
	// AddPrefix 添加Key前缀
	// @param key string 缓存键
	// @return @1 string 添加前缀后的完整键名
	AddPrefix(key string) string
	// Client 获取客户端
	// @return @1 any 底层缓存客户端
	Client() any
	// Close 关闭缓存
	// @return @1 error 错误信息
	Close() error
}

func GetCache

func GetCache() Cache

GetCache 获取全局缓存实例 @return @1 Cache 缓存实例

type Result

type Result interface {
	// Err 返回错误信息
	// @return @1 error 错误信息
	Err() error
	// Result 返回原始值与错误
	// @return @1 value.Value 原始值
	// @return @2 error 错误信息
	Result() (value.Value, error)
	// Int 以 int 类型返回缓存值
	// @return @1 int 转换后的值
	// @return @2 error 错误信息
	Int() (int, error)
	// Int8 以 int8 类型返回缓存值
	// @return @1 int8 转换后的值
	// @return @2 error 错误信息
	Int8() (int8, error)
	// Int16 以 int16 类型返回缓存值
	// @return @1 int16 转换后的值
	// @return @2 error 错误信息
	Int16() (int16, error)
	// Int32 以 int32 类型返回缓存值
	// @return @1 int32 转换后的值
	// @return @2 error 错误信息
	Int32() (int32, error)
	// Int64 以 int64 类型返回缓存值
	// @return @1 int64 转换后的值
	// @return @2 error 错误信息
	Int64() (int64, error)
	// Uint 以 uint 类型返回缓存值
	// @return @1 uint 转换后的值
	// @return @2 error 错误信息
	Uint() (uint, error)
	// Uint8 以 uint8 类型返回缓存值
	// @return @1 uint8 转换后的值
	// @return @2 error 错误信息
	Uint8() (uint8, error)
	// Uint16 以 uint16 类型返回缓存值
	// @return @1 uint16 转换后的值
	// @return @2 error 错误信息
	Uint16() (uint16, error)
	// Uint32 以 uint32 类型返回缓存值
	// @return @1 uint32 转换后的值
	// @return @2 error 错误信息
	Uint32() (uint32, error)
	// Uint64 以 uint64 类型返回缓存值
	// @return @1 uint64 转换后的值
	// @return @2 error 错误信息
	Uint64() (uint64, error)
	// Float32 以 float32 类型返回缓存值
	// @return @1 float32 转换后的值
	// @return @2 error 错误信息
	Float32() (float32, error)
	// Float64 以 float64 类型返回缓存值
	// @return @1 float64 转换后的值
	// @return @2 error 错误信息
	Float64() (float64, error)
	// Bool 以 bool 类型返回缓存值
	// @return @1 bool 转换后的值
	// @return @2 error 错误信息
	Bool() (bool, error)
	// String 以 string 类型返回缓存值
	// @return @1 string 转换后的值
	// @return @2 error 错误信息
	String() (string, error)
	// Duration 以 time.Duration 类型返回缓存值
	// @return @1 time.Duration 转换后的值
	// @return @2 error 错误信息
	Duration() (time.Duration, error)
	// Ints 以 []int 切片类型返回缓存值
	// @return @1 []int 转换后的值
	// @return @2 error 错误信息
	Ints() ([]int, error)
	// Int8s 以 []int8 切片类型返回缓存值
	// @return @1 []int8 转换后的值
	// @return @2 error 错误信息
	Int8s() ([]int8, error)
	// Int16s 以 []int16 切片类型返回缓存值
	// @return @1 []int16 转换后的值
	// @return @2 error 错误信息
	Int16s() ([]int16, error)
	// Int32s 以 []int32 切片类型返回缓存值
	// @return @1 []int32 转换后的值
	// @return @2 error 错误信息
	Int32s() ([]int32, error)
	// Int64s 以 []int64 切片类型返回缓存值
	// @return @1 []int64 转换后的值
	// @return @2 error 错误信息
	Int64s() ([]int64, error)
	// Uints 以 []uint 切片类型返回缓存值
	// @return @1 []uint 转换后的值
	// @return @2 error 错误信息
	Uints() ([]uint, error)
	// Uint8s 以 []uint8 切片类型返回缓存值
	// @return @1 []uint8 转换后的值
	// @return @2 error 错误信息
	Uint8s() ([]uint8, error)
	// Uint16s 以 []uint16 切片类型返回缓存值
	// @return @1 []uint16 转换后的值
	// @return @2 error 错误信息
	Uint16s() ([]uint16, error)
	// Uint32s 以 []uint32 切片类型返回缓存值
	// @return @1 []uint32 转换后的值
	// @return @2 error 错误信息
	Uint32s() ([]uint32, error)
	// Uint64s 以 []uint64 切片类型返回缓存值
	// @return @1 []uint64 转换后的值
	// @return @2 error 错误信息
	Uint64s() ([]uint64, error)
	// Float32s 以 []float32 切片类型返回缓存值
	// @return @1 []float32 转换后的值
	// @return @2 error 错误信息
	Float32s() ([]float32, error)
	// Float64s 以 []float64 切片类型返回缓存值
	// @return @1 []float64 转换后的值
	// @return @2 error 错误信息
	Float64s() ([]float64, error)
	// Bools 以 []bool 切片类型返回缓存值
	// @return @1 []bool 转换后的值
	// @return @2 error 错误信息
	Bools() ([]bool, error)
	// Strings 以 []string 切片类型返回缓存值
	// @return @1 []string 转换后的值
	// @return @2 error 错误信息
	Strings() ([]string, error)
	// Bytes 以 []byte 切片类型返回缓存值
	// @return @1 []byte 转换后的值
	// @return @2 error 错误信息
	Bytes() ([]byte, error)
	// Durations 以 []time.Duration 切片类型返回缓存值
	// @return @1 []time.Duration 转换后的值
	// @return @2 error 错误信息
	Durations() ([]time.Duration, error)
	// Slice 以 []any 切片类型返回缓存值
	// @return @1 []any 转换后的值
	// @return @2 error 错误信息
	Slice() ([]any, error)
	// Map 以 map[string]any 类型返回缓存值
	// @return @1 map[string]any 转换后的值
	// @return @2 error 错误信息
	Map() (map[string]any, error)
	// Scan 将缓存值扫描到目标对象
	// @param pointer any 目标对象指针
	// @return @1 error 错误信息
	Scan(pointer any) error
}

Result 缓存读取结果接口,提供多种类型转换方法

func Get

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

Get 获取缓存值 @param ctx context.Context 上下文 @param key string 缓存键 @param def ...any 可选默认值,当缓存不存在时返回该默认值 @return @1 Result 缓存结果

func GetSet

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

GetSet 获取缓存值,若不存在则通过 fn 生成后写入并返回 @param ctx context.Context 上下文 @param key string 缓存键 @param fn SetValueFunc 缓存未命中时执行的回调,用于生成缓存值 @return @1 Result 缓存结果

func NewResult

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

NewResult 创建一个缓存读取结果 @param val any 缓存值 @param err ...error 可选错误信息 @return @1 Result 缓存读取结果

type SetValueFunc

type SetValueFunc func() (any, error)

SetValueFunc 设置缓存值的回调函数类型 @return @1 any 生成的缓存值 @return @2 error 错误信息

Jump to

Keyboard shortcuts

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