Documentation
¶
Index ¶
- func AddPrefix(key string) string
- func Client() any
- func Close() error
- func DecrFloat(ctx context.Context, key string, value float64) (float64, error)
- func DecrInt(ctx context.Context, key string, value int64) (int64, error)
- func Delete(ctx context.Context, keys ...string) (int64, error)
- func Has(ctx context.Context, key string) (bool, error)
- func IncrFloat(ctx context.Context, key string, value float64) (float64, error)
- func IncrInt(ctx context.Context, key string, value int64) (int64, error)
- func Set(ctx context.Context, key string, value any, expiration ...time.Duration) error
- func SetCache(cache Cache)
- type Cache
- type Result
- type SetValueFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecrFloat ¶
DecrFloat 浮点数自减 @param ctx context.Context 上下文 @param key string 缓存键 @param value float64 自减步长 @return @1 float64 自减后的值 @return @2 error 错误信息
func DecrInt ¶
DecrInt 整数自减 @param ctx context.Context 上下文 @param key string 缓存键 @param value int64 自减步长 @return @1 int64 自减后的值 @return @2 error 错误信息
func Delete ¶
Delete 删除缓存 @param ctx context.Context 上下文 @param keys ...string 缓存键,可传入多个 @return @1 int64 实际删除的 key 数量 @return @2 error 错误信息
func Has ¶
Has 检测缓存是否存在 @param ctx context.Context 上下文 @param key string 缓存键 @return @1 bool 缓存是否存在 @return @2 error 错误信息
func IncrFloat ¶
IncrFloat 浮点数自增 @param ctx context.Context 上下文 @param key string 缓存键 @param value float64 自增步长 @return @1 float64 自增后的值 @return @2 error 错误信息
func IncrInt ¶
IncrInt 整数自增 @param ctx context.Context 上下文 @param key string 缓存键 @param value int64 自增步长 @return @1 int64 自增后的值 @return @2 error 错误信息
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
}
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 ¶
Get 获取缓存值 @param ctx context.Context 上下文 @param key string 缓存键 @param def ...any 可选默认值,当缓存不存在时返回该默认值 @return @1 Result 缓存结果
type SetValueFunc ¶
SetValueFunc 设置缓存值的回调函数类型 @return @1 any 生成的缓存值 @return @2 error 错误信息