cache

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

Gousing Cache

介绍

开箱即用的 Golang Cache 工具,支持 Memory, Redis, L2Cache (Memory+Redis) 三种驱动, 缓存数据统一使用 msgpack 处理, 提供一致性 API 快速设置及读取缓存, 支持全局默认过期时间,单 KEY 过期时间, 支持 TAG 标签, LoadDo 单飞加载。

安装及使用
go get gitee.com/gousing/cache
import  "gitee.com/gousing/cache"
全局默认缓存实例

全局默认 Storage 缓存实例, 默认使用 MemoryCache 作为缓存驱动。

	// 全局默认缓存实例,默认为一个最大数据容量限制为 128M 的 MemoryCache
	// 使用全局默认过期时间
	cache.Set("key1", "value1")
	// 针对单个Key设置过期时间
	cache.Set("key2", "value2Expire", cache.WithExpire(time.Second*10))

	// 缓存读取
	value1 := cache.GetString("key1")
	fmt.Println(`cache.GetString("key1")`,value1)

  // TAG标签
	cache.Set("keyTag-1-1", 11, cache.WithTags("tag1"))
	cache.Set("keyTag-1-2", 12, cache.WithTags("tag1"))
	cache.Set("keyTag-2", 20, cache.WithTags("tag2"))


	// 自定义全局默认缓存仓库实例
	// SetDefault 设置全局默认缓存实例
	// cache.SetDefault(driver cache.Driver)
	// 例如:
	cacheMemory,err :=cache.NewMemory(nil)
	//cacheRedis,err :=cache.NewRedis(nil)
	//cacheL2Cache,err :=cache.NewL2Cache(nil)
	cache.SetDefault(cacheMemory)

自定义实例
MemoryCache

MemoryCache 由 NewMemory 创建,支持定义缓存大小,默认过期时间等配置。

cache.NewMemory(options *cache.MemoryOptions) (cache.Storager,error)
memCache,err :=cache.NewMemory(&cache.MemoryOptions{})

type MemoryOptions struct {
	MaxSize         int64                          // 缓存数据最大空间容量(最大占用内存, 仅包含缓存K/V数据, 不包含缓存结构管理数据),单位为MB, 默认为 128 MB, 最小为2
	CacheShardCount uint32                         // Memory LRU缓存默认的分片数, 分片数要求为 2 的幂(如 2,8,16,32,64,128,256,512,1024,2048), 默认为 128 个分片,最小为2, 最大为4096
	TagShardCount   uint32                         // TAG标签分片数, 分片数要求为 2 的幂,默认为 CacheShardCount/4, 如果小于2则设置为2
	DefaultExpire   time.Duration                  // 全局默认过期时间,默认为0表示永久不过期(实例生命周期内/长久存储), 最小为1秒, 过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间
	CleanupInterval time.Duration                  // 定时清理过期缓存项的间隔时间,单位为秒, 默认为60秒, 最小为60秒
}
RedisCache

RedisCache 由 NewRedis 创建,支持定义缓存大小,默认过期时间等配置,使用 go-redis 库驱动。

cache.NewRedis(options *cache.RedisOptions) (cache.Storager,error)
redisCache,err :=cache.NewRedis(&cache.RedisOptions{}),
// RedisOptions Redis缓存配置信息
type RedisOptions struct {
	// Client Redis服务器链接选项,使用 UniversalClient 统一客户端,UniversalClient是对 Client 、 ClusterClient 、 FailoverClient 客户端的包装。
	// 	- 根据不同的选项,客户端的类型如下:
	// 	- 如果指定了 MasterName 选项,则返回 FailoverClient 哨兵客户端。
	// 	- 如果 Addrs 是 2 个以上的地址,则返回 ClusterClient 集群客户端。
	// 	- 其他情况,返回 Client 单节点客户端。
	//  - github.com/redis/go-redis/v9
	// 	- https://redis.uptrace.dev/zh/guide/universal.html
	// 	- RedisUniversalOptions is an alias for [redis.UniversalOptions]
	Client RedisUniversalOptions
	// DefaultExpire 默认过期时间 0, 最小过期时间为1s time.Second
	// 过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间。
	DefaultExpire time.Duration
	MaxEntrySize  int64 // 单个缓存项最大数据大小,单位为MB, 默认为 32 MB
}
L2Cache

L2Cache 由 NewL2Cache 创建,参数为用户自定义的 local *MemoryCache 和 *RedisCache。

// NewL2Cache 创建并初始化一个 L2Cache 二级缓存实例
//   - 需要传入 local 和 remote 缓存驱动实例  (通常为 MemoryCache + RedisCache)
cache.NewL2Cache(local cache.Storager, remote cache.Storager, options *cache.L2CacheOptions) (*cache.D, error)
//	预留配置选项,后续扩展使用
type L2CacheOptions struct{}
Storager 接口
type Storager interface {
	// Scan 获取键对应的值, 并解码到 refVal
	//   - refVal 是一个指向具体数据类型的指针, 用于存放反序列化后的数据
	//   - 不存在或已经过期或反序列化失败则返回错误
	//   - 标量类型直接解析到 refVal
	//   - 其他返回数据为 msgpack 自动反序列化后解析到 refVal
	Scan(key string, refVal any) error
	// Get 获取键对应的值
	//   - 不存在或如果已经过期则返回错误
	//   - 存在则返回缓存值:
	//   - - 标量类型直接返回 Any 缓存数据值
	//   - - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值
	Get(key string) (any, error)
	// GetEntry 获取键对应的缓存项副本信息, 如果不存在或已经过期则返回错误
	GetEntry(key string) (entry *Entry, err error)
	// GetOrSet 获取键对应的值, 如不存在则设置为指定的值
	//   - 不存在或如果已经过期则进行设置并返回设置的值
	//   - 存在则返回旧值:
	//   - - 标量类型直接返回 Any 缓存数据值
	//   - - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值
	GetOrSet(key string, value any, opts ...EntryOptions) (any, error)
	// GetAndSet 获取键对应的旧值并设置新值
	//   - 不存在或如果已经过期则返回nil
	//   - 存在则返回旧值:
	//   - - 标量类型直接返回 Any 缓存数据值
	//   - - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值
	GetAndSet(key string, value any, opts ...EntryOptions) (any, error)
	// GetAndDelete 获取键对应的值, 并删除该键
	//   - 不存在或如果已经过期则返回错误
	//   - 存在则返回值:
	//   - - 标量类型直接返回 Any 缓存数据值
	//   - - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值
	GetAndDelete(key string) (any, error)

	// GetTags 获取所有Tag标签
	GetTags() []string
	// GetTagKeys 获取指定 Tag 标签对应的所有缓存 Keys 键名
	GetTagKeys(tag string) []string
	// GetKeyTags 获取指定键名 Key 对应的所有缓存 Tags 标签
	GetKeyTags(key string) []string

	// Set 设置键值对, 可以通过 EntryOption 设置 Expire, Tag 等选项
	//   - key 为空 或 value 为nil 则返回错误
	//   - 如缓存 key 已存在且 opts 为空, 则保持过期时间Tags等选项
	//   - 如缓存 key 已存在且 opts 不为空, 则更新过期时间Tags等opts选项(原Tag保留, 如有新Tag则追加)
	//   - 如缓存 key 不存在且 opts 为空, 则使用默认选项存储数据
	//   - 如缓存 key 不存在且 opts 不为空, 则使用opts选项存储数据
	Set(key string, value any, opts ...EntryOptions) error
	// SetWithExists 仅当缓存 Key 键存在时才更新设置值, 返回True表示更新成功
	SetWithExists(key string, value any, opts ...EntryOptions) (updated bool, err error)
	// SetWithNotExists 仅当缓存 Key 键不存在时才设置值, 返回True表示设置成功
	SetWithNotExists(key string, value any, opts ...EntryOptions) (added bool, err error)
	// Incr 原子性地将键对应的值增加+1, 并返回新的值
	//   - 如果键不存在, 则将原子值设为 1
	//   - 如果键存在, 则更新值 +1
	Incr(key string, opts ...EntryOptions) (int64, error)
	// IncrBy 原子性地将键对应的值增加 Step 步进值, 并返回新的值
	//   - 如果键不存在, 则将原子值设为 Step
	//   - 如果键存在, 则更新值 +Step
	IncrBy(key string, step int64, opts ...EntryOptions) (int64, error)
	// Decr 原子性地将键对应的值减少 -1, 并返回新的值
	//   - 如果键不存在, 则将原子值设为 -1
	//   - 如果键存在, 则更新值 -1
	Decr(key string, opts ...EntryOptions) (int64, error)
	// DecrBy 原子性地将键对应的值减少 Step 步进值, 并返回新的值
	//   - 如果键不存在, 则将原子值设为 -Step
	//   - 如果键存在, 则更新值 -Step
	DecrBy(key string, step int64, opts ...EntryOptions) (int64, error)

	// SStringAdd 向指定 key 的 String 集合添加一个或多个成员元素
	//   - 如果指定 key 的集合不存在, 则新建集合并添加值到集合
	//   - 如果指定 key 不是一个 String 集合, 则返回错误
	//   - 如果指定 key 的集合存在, 则添加值到集合
	//   - 如果 items为空, 则什么也不做, 直接返回nil
	SStringAdd(key string, items ...string) error
	// SStringRemove 从指定 key 的 String 集合中移除一个或多个成员
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
	//   - 如果items为空, 则什么也不做, 直接返回nil
	SStringRemove(key string, items ...string) error
	// SStringClear 清空指定 key 的 String 集合
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
	SStringClear(key string) error
	// SStringContains 判断一个或多个成员是否全部在指定 key 的 String 集合中
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回 false
	//   - 查询多个成员, 任意一个成员不在集合中, 则返回 false
	SStringContains(key string, items ...string) bool
	// SStringContainsSome 判断一个或多个成员的中任意一个成员是否在指定 key 的 String 集合中
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回 false
	//   - 查询多个成员, 任意一个成员存在集合中, 则返回 true
	SStringContainsSome(key string, items ...string) bool
	// SStringMembers 获取指定 key 的 String 集合的所有成员
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
	SStringMembers(key string) ([]string, error)
	// SStringTraverse 遍历指定 key 的 String 集合的所有成员
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
	//   - fn 签名是 iter.Seq[string], 迭代过程中, 返回 false 则停止遍历
	SStringTraverse(key string, fn func(string) bool) error
	// SStringSize 获取指定 key 的 String 集合的成员数量
	//   - 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
	SStringSize(key string) (int64, error)

	// SIntAdd 向指定 key 的 Int64 集合添加一个或多个成员元素
	//   - 如果指定 key 的集合不存在, 则新建集合并添加值到集合
	//   - 如果指定 key 不是一个 Int64 集合, 则返回错误
	//   - 如果指定 key 的集合存在, 则添加值到集合
	//   - 如果 items为空, 则什么也不做, 直接返回nil
	SIntAdd(key string, items ...int64) error
	// SIntRemove 从指定 key 的 Int64 集合中移除一个或多个成员
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
	//   - 如果items为空, 则什么也不做, 直接返回nil
	SIntRemove(key string, items ...int64) error
	// SIntClear 清空指定 key 的 Int64 集合
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
	SIntClear(key string) error
	// SIntContains 判断一个或多个成员是否全部在指定 key 的 Int64 集合中
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回 false
	//   - 查询多个成员, 任意一个成员不在集合中, 则返回 false
	SIntContains(key string, items ...int64) bool
	// SIntContainsSome 判断一个或多个成员的中任意一个成员是否在指定 key 的 Int64 集合中
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回 false
	//   - 查询多个成员, 任意一个成员存在集合中, 则返回 true
	SIntContainsSome(key string, items ...int64) bool
	// SIntMembers 获取指定 key 的 Int64 集合的所有成员
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
	SIntMembers(key string) ([]int64, error)
	// SIntTraverse 遍历指定 key 的 Int64 集合的所有成员
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
	//   - fn 签名是 iter.Seq[int64], 迭代过程中, 返回 false 则停止遍历
	SIntTraverse(key string, fn func(int64) bool) error
	// SIntSize 获取指定 key 的 Int64 集合的成员数量
	//   - 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
	SIntSize(key string) (int64, error)

	// LoadDo 获取键对应的值, 并反序列化解析后赋值到 refVal
	//   - refVal 是一个指向具体类型的指针, 用于存放反序列化后的数据
	//   - 缓存存在: 则反序列化解析后赋值到 refVal
	//   - 缓存不存在: 则执行 Fn 函数获取查询值后设置为缓存(Set Use Opts), 最后赋值到 refVal
	//   - 注意: 请确保 refVal(&T) 的类型和 do() 函数的返回值T类型一致
	LoadDo(key string, refVal any, do func() (setVal any, err error), opts ...EntryOptions) error
	// UpdateFn 更新键对应的值
	//   - 如果键不存在, 则执行 Fn 函数设置值并返回设置的默认值(Set Use Opts), 并返回设置的默认值
	//   - 如果键存在, 则执行 Fn 函数更新值并返回更新的新值(Set Without Opts), 并返回更新的新值
	UpdateFn(key string, fn func(getVal any, exits bool) (updateVal any, err error), opts ...EntryOptions) (any, error)
	// Expire 设置指定缓存 Key 的存活时间(精度为秒)
	Expire(key string, duration time.Duration) error
	// ExpireAt 设置指定缓存 Key 的存活时间(精度为秒)
	ExpireAt(key string, expireAt time.Time) error
	// TTL 获取键的剩余存活时间(精度为秒)
	//   - 不存在或如果已经过期则返回-1
	//   - 如果设置了永久存活则返回0
	TTL(key string) time.Duration
	// Contains 判断缓存 Key 是否存在, 支持多个键查询, 只要有一个键不存在则返回false
	Contains(keys ...string) bool
	// ContainsSome 判断缓存 Key 是否存在, 支持多个键查询, 如果有一个或多个键存在则返回true
	ContainsSome(keys ...string) bool
	// ContainsTags 检查 Tag 标签是否存在, 支持多个键查询, 只要有一个键不存在则返回false
	ContainsTags(tags ...string) bool
	// ContainsSomeTags 检查 Tag 标签是否存在, 支持多个键查询, 如果有一个或多个键存在则返回true
	ContainsSomeTags(tags ...string) bool
	// ContainsTagKey 检查 Tag/Key 标签对是否存在
	ContainsTagKey(tag, key string) bool
	// Metrics 返回监控指标
	Metrics() *Metrics
	// Cleanup 手动清理过期数据
	Cleanup() error
	// Delete 删除一个或多个键
	Delete(keys ...string) error
	// DeleteTags 依据缓存Tag标签删除缓存项, 同时tag 标签以及对应的 key <=> tags 和 tag <=> keys 的索引集合数据
	DeleteTags(tags ...string) error
	// Clear 清空所有数据(同步操作)
	Clear() error
	// ClearAsync 清空所有数据(异步操作)
	ClearAsync() error

	// TotalSize 返回当前缓存的总大小(单位: byte)
	TotalSize() int64
	// TotalCount 返回当前缓存项(Key)的总数量(单位: 个)
	TotalCount() int64
	// MaxSize 返回当前缓存实例的最大容量(单位: byte)
	MaxSize() int64
	// MaxEntrySize 获取单个缓存项数据限制的大小(单位: byte)
	MaxEntrySize() int64
	// StorageType 获取缓存存储驱动类型
	StorageType() StorageType
	// DefaultExpire 获取默认过期时间
	DefaultExpire() time.Duration
	// UseLocal 仅使用 Local 缓存进行操作( 通常为 MemoryCache )
	//   - 仅在 L2Cache 驱动模式下有效, 其他驱动模式返回自身驱动实例
	UseLocal() Storager
	// UseRemote 仅仅使用 Remote 进行缓存操作( 通常为 RedisCache )
	//   - 仅在 L2Cache 驱动模式下有效, 其他驱动模式返回自身驱动实例
	UseRemote() Storager
	// LiveState 获取当前缓存实例的运行状态
	LiveState() LiveState
	// IsGlobal 是否为全局缓存实例
	IsGlobal() bool
	// setGlobal 设置实例为全局缓存标记
	SetGlobal()
}

Global 接口

全局默认 Storage 缓存实例提供以下基本标量数据类型和泛型化 Slice/Map 的快速读取接口,基于 Gousing Values, 支持基本数据类型互通互转.

type Storager interface {
	Driver
	// GetString 获取 string 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 Stringer 接口的类型
	GetString(key string) string
	// GetStringD 获取 string 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值
	//   - 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 Stringer 接口的类型
	GetStringD(key string, defaultVal string) string
	// GetStringD 获取 string 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息
	//   - 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 Stringer 接口的类型
	GetStringE(key string) (string, error)
	// GetBool 获取 bool 类型的缓存值, 如果不存在或已经过期则返回false
	//   - 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 values.Booler 适配接口的类型
	GetBool(key string) bool
	// GetBoolD 获取 bool 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值
	GetBoolD(key string, defaultVal bool) bool
	// GetBoolE 获取 bool 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息
	GetBoolE(key string) (bool, error)
	// GetInt 获取 int 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型
	GetInt(key string) int
	// GetIntD 获取 int 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值
	GetIntD(key string, defaultVal int) int
	// GetIntE 获取 int 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息
	GetIntE(key string) (int, error)
	GetInt8(key string) int8
	GetInt8D(key string, defaultVal int8) int8
	GetInt8E(key string) (int8, error)
	GetInt16(key string) int16
	GetInt16D(key string, defaultVal int16) int16
	GetInt16E(key string) (int16, error)
	GetInt32(key string) int32
	GetInt32D(key string, defaultVal int32) int32
	GetInt32E(key string) (int32, error)
	GetInt64(key string) int64
	GetInt64D(key string, defaultVal int64) int64
	GetInt64E(key string) (int64, error)
	GetUint(key string) uint
	GetUintD(key string, defaultVal uint) uint
	GetUintE(key string) (uint, error)
	GetUint8(key string) uint8
	GetUint8D(key string, defaultVal uint8) uint8
	GetUint8E(key string) (uint8, error)
	GetUint16(key string) uint16
	GetUint16D(key string, defaultVal uint16) uint16
	GetUint16E(key string) (uint16, error)
	GetUint32(key string) uint32
	GetUint32D(key string, defaultVal uint32) uint32
	GetUint32E(key string) (uint32, error)
	GetUint64(key string) uint64
	GetUint64D(key string, defaultVal uint64) uint64
	GetUint64E(key string) (uint64, error)
	GetFloat32(key string) float32
	GetFloat32D(key string, defaultVal float32) float32
	GetFloat32E(key string) (float32, error)
	GetFloat64(key string) float64
	GetFloat64D(key string, defaultVal float64) float64
	GetFloat64E(key string) (float64, error)
	// GetTime 获取 time.Time 类型的缓存值, 如果不存在或已经过期则返回false
	//   - 如待转换目标是数值(当做Unix秒级时间戳), 则使用 Server/Local 时区
	//   - 如待转换目标是字符串(日期时间)未包含时区信息时, 则使用 Server/Local 时区
	//   - 支持自动转换类型: string, intX, uintX, floatX 以及实现了 Timer 适配接口的类型
	GetTime(key string) time.Time
	GetTimeD(key string, defaultVal time.Time) time.Time
	GetTimeE(key string) (time.Time, error)
	// GetTimeWith 获取指定的时区 time.Time 类型的缓存值, 如果不存在或已经过期则返回false
	//   - 如待转换目标是数值(当做Unix秒级时间戳), 则使用 location 指定的时区
	//   - 如待转换目标是字符串(日期时间)未包含时区信息时, 则使用 location 指定的时区
	//   - 支持自动转换类型: string, intX, uintX, floatX 以及实现了 Timer 适配接口的类型
	GetTimeWith(key string, location *time.Location) (time.Time, error)
	GetTimeDuration(key string) time.Duration
	GetTimeDurationD(key string, defaultVal time.Duration) time.Duration
	GetTimeDurationE(key string) (time.Duration, error)
	GetTimeMonth(key string) time.Month
	GetTimeMonthD(key string, defaultVal time.Month) time.Month
	GetTimeMonthE(key string) (time.Month, error)
	GetTimeWeekday(key string) time.Weekday
	GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday
	GetTimeWeekdayE(key string) (time.Weekday, error)
	// GetComplex128 获取 complex128 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持自动转换类型: string, complex64, complex128 类型转换(注意: complex64/128互转精度损失问题)
	GetComplex128(key string) complex128
	GetComplex128D(key string, defaultVal complex128) complex128
	GetComplex128E(key string) (complex128, error)
	GetComplex64(key string) complex64
	GetComplex64D(key string, defaultVal complex64) complex64
	GetComplex64E(key string) (complex64, error)
	// GetIP 获取 net.IP 类型的缓存值, 如果不存在或已经过期则返回false
	//   - 支持自动转换类型: string, intX, uintX ( 支持数字类型的 IPv4 地址 比如: 3232235521 => 192.168.0.1 )
	GetIP(key string) net.IP
	GetIPD(key string, defaultVal net.IP) net.IP
	GetIPE(key string) (net.IP, error)
	// GetSliceAny 获取 []any 类型的缓存值, 如果不存在或已经过期则返回空值
	GetSliceAny(key string) []any
	GetSliceAnyD(key string, defaultVal []any) []any
	GetSliceAnyE(key string) ([]any, error)
	// GetSliceAs 获取 []T 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持转换目标为任意类型的 []TypeBase 和 [][]TypeBase 切片, 如果转换失败, 则返回空的 []T 切片
	//   - TypeBase: string | bool
	//   - int | int8 | int16 | int32 | int64 |
	//   - uint | uint8 | uint16 | uint32 | uint64 |
	//   - float32 | float64
	//   - time.Time | time.Duration | time.Month | time.Weekday
	GetSliceAs[T values.TypeBase | values.TypeBaseSlice](key string) []T
	GetSliceAsD[T values.TypeBase | values.TypeBaseSlice](key string, defaultVal []T) []T
	GetSliceAsE[T values.TypeBase | values.TypeBaseSlice](key string) ([]T, error)
	// GetSliceMap 获取 []map[K]V 类型的缓存值, 如果不存在或已经过期则返回空值
	GetSliceMap[K, V values.TypeBase](key string) []map[K]V
	GetSliceMapD[K, V values.TypeBase](key string, defaultVal []map[K]V) []map[K]V
	GetSliceMapE[K, V values.TypeBase](key string) ([]map[K]V, error)
	// GetMapAny 获取 map[K]any 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持转换目标为任意类型的 map[TypeBase]any
	GetMapAny[K values.TypeBase](key string) map[K]any
	GetMapAnyD[K values.TypeBase](key string, defaultVal map[K]any) map[K]any
	GetMapAnyE[K values.TypeBase](key string) (map[K]any, error)
	// GetMapAny 获取 map[K]V 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持转换目标为任意类型的 map[TypeBase]TypeBase
	GetMapAs[K, V values.TypeBase](key string) map[K]V
	GetMapAsD[K, V values.TypeBase](key string, defaultVal map[K]V) map[K]V
	GetMapAsE[K, V values.TypeBase](key string) (map[K]V, error)
	// GetMapSlice 获取 map[K][]V 类型的缓存值, 如果不存在或已经过期则返回空值
	//   - 支持转换目标为任意类型的 map[TypeBase][]TypeBase
	GetMapSlice[K, V values.TypeBase](key string) map[K][]V
	GetMapSliceD[K, V values.TypeBase](key string, defaultVal map[K][]V) map[K][]V
	GetMapSliceE[K, V values.TypeBase](key string) (map[K][]V, error)
}

Gousing 通用选项
自定义 Logger
// Cache 日志默认使用Golang标准库slog.Default()全局日志记录日志
// cache.SetLogger(*slog.Logger)
myLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
cache.SetLogger(myLogger)
感谢

Cache Value 数据序列化使用 msgpack(v5) github.com/vmihailenco/msgpack Redis 使用 go-redis(v9) 驱动 github.com/redis/go-redis Memory V1 版本使用 freecache 驱动, v2 版使用内置的 LRU 缓存算法实现

Benchmark

统一便捷的 IO 接口封装后性能损失极低:

goos: windows
goarch: amd64
pkg: gitee.com/gousing/cache
cpu: Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz
benchmarkFile: driver/driver_benchmark_test.go
Benchmark
Benchmark_Set Memory - - - -
Set Default 11086432 104.2 ns/op 0 B/op 0 allocs/op
Set WithExpire 9114572 128.1 ns/op 16 B/op 1 allocs/op
Set WithTag 1860351 632.6 ns/op 112 B/op 2 allocs/op
Set WithExpireAndTag 1764434 648.2 ns/op 122 B/op 3 allocs/op
Benchmark_Set Redis - - - -
Set Default 10182 86376 ns/op 767 B/op 16 allocs/op
Set WithExpire 8930 56110 ns/op 834 B/op 18 allocs/op
Set WithTag 4390 94174 ns/op 1099 B/op 41 allocs/op
Set WithExpireAndTag 4704 98105 ns/op 1115 B/op 42 allocs/op
Benchmark_Set L2Cache - - - -
Set Default 9644 114312 ns/op 1342 B/op 55 allocs/op
Set WithExpire 9032 134972 ns/op 1649 B/op 56 allocs/op
Set WithTag 3363 249422 ns/op 3777 B/op 81 allocs/op
Set WithExpireAndTag 3094 256713 ns/op 3216 B/op 82 allocs/op
Benchmark_Get - - - -
Get Memory 16441148 68.22 ns/op 0 B/op 0 allocs/op
Ge Redis 18483 55679 ns/op 247 B/op 8 allocs/op
Get L2Cache 219788 6444 ns/op 34 B/op 1 allocs/op
Benchmark_SetGet - - - -
SetGet Memory 7075971 170.1 ns/op 1 B/op 0 allocs/op
SetGet Redis 35792 32033 ns/op 490 B/op 27 allocs/op
SetGet L2Cache 29581 37823 ns/op 568 B/op 15 allocs/op
Benchmark_Decr - - - -
Decr Memory 11700352 105.2 ns/op 8 B/op 1 allocs/op
Decr Redis 7957 148673 ns/op 1639 B/op 39 allocs/op
Decr L2Cache 9146 141679 ns/op 1360 B/op 32 allocs/op
Benchmark_Incr - - - -
Incr Memory 11521378 98.24 ns/op 7 B/op 1 allocs/op
Incr Redis 8455 140898 ns/op 1350 B/op 31 allocs/op
Incr L2Cache 8457 142733 ns/op 1360 B/op 32 allocs/op
Benchmark_SString - - - -
Incr Memory 20155498 54.87 ns/op 0 B/op 0 allocs/op
Incr Redis 9417 128608 ns/op 1362 B/op 32 allocs/op
Incr L2Cache 9956 133143 ns/op 1372 B/op 33 allocs/op
Benchmark_SInt - - - -
Incr Memory 26483361 44.98 ns/op 0 B/op 0 allocs/op
Incr Redis 9127 133734 ns/op 1380 B/op 35 allocs/op
Incr L2Cache 9416 127906 ns/op 1372 B/op 36 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewL2Cache = driver.NewL2Cache

NewL2Cache 创建并初始化一个 L2Cache 二级缓存实例

  • 需要传入 local 和 remote 缓存驱动实例 (通常为 MemoryCache + RedisCache)
View Source
var NewL2CacheDefualt = driver.NewL2CacheDefualt

NewL2CacheDefualt 创建一个新的 L2Cache 二级缓存实例, 使用默认配置信息

  • local 默认使用 NewMemoryDefault 创建
  • remote 默认使用 NewRedisDefault 创建
View Source
var NewMemory = driver.NewMemory

NewMemory 创建一个新的 Memcache 实例

View Source
var NewMemoryDefault = driver.NewMemoryDefault

NewMemoryDefault 创建一个新的 Memcache 实例, 使用默认配置信息

  • MaxSize: 128 // MB
  • CacheShardCount: 128
  • TagShardCount: 32
  • DefaultExpire: 0
  • CleanupInterval: time.Minute
View Source
var NewRedis = driver.NewRedis

NewRedis 创建一个新的 Redis 实例, 使用默认配置信息

View Source
var NewRedisDefault = driver.NewRedisDefault

NewRedisDefault 创建一个新的 Redis 实例, 使用默认配置信息

  • Addrs: []string{"localhost:6379"} 无密码连接
View Source
var WithExpire = storage.WithExpire

WithExpire 设置过期时间, 最小为1秒, 0 表示不过期, 默认为 Cache Driver => DefaultExpire

View Source
var WithTags = storage.WithTags

WithTags 设置缓存 Tags 标签, 可以设置多个标签, 默认为空切片 []string{}

View Source
var WithTimeout = storage.WithTimeout

WithTimeout 设置超时时间, 最小为1秒, 默认为3s (部分场景使用, 如: LoadDo, UpdateFn 等场景使用), 默认为3s

Functions

func Cleanup added in v1.2.0

func Cleanup() error

Cleanup 手动清理过期数据

func Clear

func Clear() error

Clear 清空所有数据(同步操作)

func ClearAsync added in v1.2.0

func ClearAsync() error

ClearAsync 清空所有数据(异步操作)

func Contains added in v1.2.0

func Contains(keys ...string) bool

Contains 判断缓存 Key 是否存在, 支持多个键查询, 只要有一个键不存在则返回false

func ContainsSome added in v1.2.0

func ContainsSome(keys ...string) bool

ContainsSome 判断缓存 Key 是否存在, 支持多个键查询, 如果有一个或多个键存在则返回true

func ContainsSomeTags added in v1.2.0

func ContainsSomeTags(tags ...string) bool

ContainsSomeTags 检查 Tag 标签是否存在, 支持多个键查询, 如果有一个或多个键存在则返回true

func ContainsTagKey added in v1.2.0

func ContainsTagKey(tag, key string) bool

ContainsTagKey 检查 Tag/Key 标签对是否存在

func ContainsTags added in v1.2.0

func ContainsTags(tags ...string) bool

ContainsTags 检查 Tag 标签是否存在, 支持多个键查询, 只要有一个键不存在则返回false

func Decr added in v1.2.0

func Decr(key string, opts ...EntryOptions) (int64, error)

Decr 原子性地将键对应的值减少 -1, 并返回新的值

  • 如果键不存在, 则将原子值设为 -1
  • 如果键存在, 则更新值 -1

func DecrBy added in v1.2.0

func DecrBy(key string, step int64, opts ...EntryOptions) (int64, error)

DecrBy 原子性地将键对应的值减少 Step 步进值, 并返回新的值

  • 如果键不存在, 则将原子值设为 -Step
  • 如果键存在, 则更新值 -Step

func DefaultExpire added in v1.2.0

func DefaultExpire() time.Duration

DefaultExpire 获取默认过期时间

func Delete added in v1.2.0

func Delete(keys ...string) error

Delete 删除一个或多个键

func DeleteTags added in v1.2.0

func DeleteTags(tags ...string) error

DeleteTags 依据缓存Tag标签删除缓存项, 同时tag 标签以及对应的 key <=> tags 和 tag <=> keys 的索引集合数据

func Expire added in v1.2.0

func Expire(key string, duration time.Duration) error

Expire 设置指定缓存 Key 的存活时间(精度为秒)

func ExpireAt added in v1.2.0

func ExpireAt(key string, expireAt time.Time) error

ExpireAt 设置指定缓存 Key 的存活时间(精度为秒)

func Get

func Get(key string) (any, error)

Get 获取键对应的值

  • 不存在或如果已经过期则返回错误
  • 存在则返回缓存值:
  • - 标量类型直接返回 Any 缓存数据值
  • - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值

func GetAndDelete added in v1.2.0

func GetAndDelete(key string) (any, error)

GetAndDelete 获取键对应的值, 并删除该键

  • 不存在或如果已经过期则返回错误
  • 存在则返回值:
  • - 标量类型直接返回 Any 缓存数据值
  • - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值

func GetAndSet added in v1.2.0

func GetAndSet(key string, value any, opts ...EntryOptions) (any, error)

GetAndSet 获取键对应的旧值并设置新值

  • 不存在或如果已经过期则返回nil
  • 存在则返回旧值:
  • - 标量类型直接返回 Any 缓存数据值
  • - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值

func GetBool

func GetBool(key string) bool

GetBool 获取 bool 类型的缓存值, 如果不存在或已经过期则返回false

  • 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 values.Booler 适配接口的类型

func GetBoolD

func GetBoolD(key string, defaultVal bool) bool

GetBoolD 获取 bool 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 values.Booler 适配接口的类型

func GetBoolE added in v1.2.0

func GetBoolE(key string) (bool, error)

GetBoolE 获取 bool 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 values.Booler 适配接口的类型

func GetComplex128 added in v1.2.3

func GetComplex128(key string) complex128

GetComplex128 获取 complex128 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, complex64, complex128 类型转换(注意: complex64/128互转精度损失问题)

func GetComplex128D added in v1.2.3

func GetComplex128D(key string, defaultVal complex128) complex128

GetComplex128D 获取 bool 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, complex64, complex128(注意: complex64/128互转精度损失问题)

func GetComplex128E added in v1.2.3

func GetComplex128E(key string) (complex128, error)

GetComplex128E 获取 bool 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, complex64, complex128(注意: complex64/128互转精度损失问题)

func GetComplex64 added in v1.2.3

func GetComplex64(key string) complex64

GetComplex64 获取 complex128 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, complex64, complex128(注意: complex64/128互转精度损失问题)

func GetComplex64D added in v1.2.3

func GetComplex64D(key string, defaultVal complex64) complex64

GetComplex64D 获取 bool 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, complex64, complex128(注意: complex64/128互转精度损失问题)

func GetComplex64E added in v1.2.3

func GetComplex64E(key string) (complex64, error)

GetComplex64E 获取 bool 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, complex64, complex128(注意: complex64/128互转精度损失问题)

func GetDefault added in v1.1.0

func GetDefault() storage.Storager

GetDefault 获取全局默认缓存实例

  • 用户可通过 `SetDefault` 设置自定义默认缓存实例
  • 如果默认实例未配置(未初始化),将自动尝试使用默认参数进行初始化( MemoryCache )
  • 注意: 此函数禁止内联优化,内联后会导致全局缓存 RedisCache 实例 Stop/Restart 连接异常

func GetEntry added in v1.2.0

func GetEntry(key string) (entry *storage.Entry, err error)

GetEntry 获取键对应的缓存项副本信息, 如果不存在或已经过期则返回错误

func GetFloat32

func GetFloat32(key string) float32

GetFloat32 获取 float32 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.FloatX 适配接口的类型

func GetFloat32D

func GetFloat32D(key string, defaultVal float32) float32

GetFloat32D 获取 float32 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.FloatX 适配接口的类型

func GetFloat32E added in v1.2.0

func GetFloat32E(key string) (float32, error)

GetFloat32E 获取 float32 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.FloatX 适配接口的类型

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 获取 float64 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.FloatX 适配接口的类型

func GetFloat64D

func GetFloat64D(key string, defaultVal float64) float64

GetFloat64D 获取 float64 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.FloatX 适配接口的类型

func GetFloat64E added in v1.2.0

func GetFloat64E(key string) (float64, error)

GetFloat64E 获取 float64 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.FloatX 适配接口的类型

func GetIP

func GetIP(key string) net.IP

GetIP 获取 net.IP 类型的缓存值, 如果不存在或已经过期则返回false

  • 支持自动转换类型: string, intX, uintX ( 支持数字类型的 IPv4 地址 比如: 3232235521 => 192.168.0.1 )

func GetIPD

func GetIPD(key string, defaultVal net.IP) net.IP

GetIPD 获取 net.IP 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX ( 支持数字类型的 IPv4 地址 比如: 3232235521 => 192.168.0.1 )

func GetIPE added in v1.2.0

func GetIPE(key string) (net.IP, error)

GetIPE 获取 net.IP 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX ( 支持数字类型的 IPv4 地址 比如: 3232235521 => 192.168.0.1 )

func GetInt

func GetInt(key string) int

GetInt 获取 int 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt16

func GetInt16(key string) int16

GetInt16 获取 int16 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt16D

func GetInt16D(key string, defaultVal int16) int16

GetInt16D 获取 int16 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt16E added in v1.2.0

func GetInt16E(key string) (int16, error)

GetInt16E 获取 int16 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt32

func GetInt32(key string) int32

GetInt32 获取 int32 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt32D

func GetInt32D(key string, defaultVal int32) int32

GetInt32D 获取 int32 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt32E added in v1.2.0

func GetInt32E(key string) (int32, error)

GetInt32E 获取 int32 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt64

func GetInt64(key string) int64

GetInt64 获取 int64 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt64D

func GetInt64D(key string, defaultVal int64) int64

GetInt64D 获取 int64 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt64E added in v1.2.0

func GetInt64E(key string) (int64, error)

GetInt64E 获取 int64 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt8

func GetInt8(key string) int8

GetInt8 获取 int8 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt8D

func GetInt8D(key string, defaultVal int8) int8

GetInt8D 获取 int8 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetInt8E added in v1.2.0

func GetInt8E(key string) (int8, error)

GetInt8E 获取 int8 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetIntD

func GetIntD(key string, defaultVal int) int

GetIntD 获取 int 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetIntE added in v1.2.0

func GetIntE(key string) (int, error)

GetIntE 获取 int 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetKeyTags added in v1.2.0

func GetKeyTags(key string) []string

GetKeyTags 获取指定键名 Key 对应的所有缓存 Tags 标签

func GetLogger

func GetLogger() *slog.Logger

GetLogger 获取全局日志实例, 默认使用 slog.Default()

func GetMapAny added in v1.2.0

func GetMapAny[K values.TypeBase](key string) map[K]any

GetMapAny 获取 map[K]any 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持转换目标为任意类型的 map[TypeBase]any
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapAnyD added in v1.2.0

func GetMapAnyD[K values.TypeBase](key string, defaultVal map[K]any) map[K]any

GetMapAnyD 获取 map[K]any 类型的缓存值, 如果不存在或已经过期则返回默认值

  • 支持转换目标为任意类型的 map[TypeBase]any
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapAnyE added in v1.2.0

func GetMapAnyE[K values.TypeBase](key string) (map[K]any, error)

GetMapAnyE 获取 map[K]any 类型的缓存值, 如果不存在或已经过期则返回空值 map[K]any 切片和错误信息

  • 支持转换目标为任意类型的 map[TypeBase]any
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapAs added in v1.2.0

func GetMapAs[K, V values.TypeBase](key string) map[K]V

GetMapAny 获取 map[K]V 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持转换目标为任意类型的 map[TypeBase]TypeBase
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapAsD added in v1.2.0

func GetMapAsD[K, V values.TypeBase](key string, defaultVal map[K]V) map[K]V

GetMapAsD 获取 map[K]V 类型的缓存值, 如果不存在或已经过期则返回默认值

  • 支持转换目标为任意类型的 map[TypeBase]TypeBase
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapAsE added in v1.2.0

func GetMapAsE[K, V values.TypeBase](key string) (map[K]V, error)

GetMapAsE 获取 map[K]V 类型的缓存值, 如果不存在或已经过期则返回空值 map[K]V 和错误信息

  • 支持转换目标为任意类型的 map[TypeBase]TypeBase
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapSlice added in v1.2.0

func GetMapSlice[K, V values.TypeBase](key string) map[K][]V

GetMapSlice 获取 map[K][]V 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持转换目标为任意类型的 map[TypeBase][]TypeBase
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapSliceD added in v1.2.0

func GetMapSliceD[K, V values.TypeBase](key string, defaultVal map[K][]V) map[K][]V

GetMapSliceD 获取 map[K][]V 类型的缓存值, 如果不存在或已经过期则返回默认值

  • 支持转换目标为任意类型的 map[TypeBase][]TypeBase
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetMapSliceE added in v1.2.0

func GetMapSliceE[K, V values.TypeBase](key string) (map[K][]V, error)

GetMapSliceE 获取 map[K][]V 类型的缓存值, 如果不存在或已经过期则返回空值 map[K][]V 和错误信息

  • 支持转换目标为任意类型的 map[TypeBase][]TypeBase
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration| time.Month | time.Weekday

func GetOrSet added in v1.2.0

func GetOrSet(key string, value any, opts ...EntryOptions) (any, error)

GetOrSet 获取键对应的值, 如不存在则设置为指定的值

  • 不存在或如果已经过期则进行设置并返回设置的值
  • 存在则返回旧值:
  • - 标量类型直接返回 Any 缓存数据值
  • - 其他返回数据为 msgpack 自动反序列化后的 Any 缓存数据值

func GetSliceAny added in v1.2.0

func GetSliceAny(key string) []any

GetSliceAny 获取 []any 类型的缓存值, 如果不存在或已经过期则返回空值

func GetSliceAnyD added in v1.2.0

func GetSliceAnyD(key string, defaultVal []any) []any

GetSliceAnyD 获取 []any 类型的缓存值, 如果不存在或已经过期则返回defaultVal指定的默认值

func GetSliceAnyE added in v1.2.0

func GetSliceAnyE(key string) ([]any, error)

GetSliceAnyD 获取 []any 类型的缓存值, 如果不存在或已经过期则返回 []any 空值切片和错误信息

func GetSliceAs added in v1.2.0

func GetSliceAs[T values.TypeBase | values.TypeBaseSlice](key string) []T

GetSliceAs 获取 []T 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持转换目标为任意类型的 []TypeBase 和 [][]TypeBase 切片, 如果转换失败, 则返回空的 []T 切片
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration | time.Month | time.Weekday

func GetSliceAsD added in v1.2.0

func GetSliceAsD[T values.TypeBase | values.TypeBaseSlice](key string, defaultVal []T) []T

GetSliceAsD 获取 []T 类型的缓存值, 如果不存在或已经过期则返回默认值

  • 支持转换目标为任意类型的 []TypeBase 和 [][]TypeBase 切片, 如果转换失败, 则返回空的 []T 切片
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration | time.Month | time.Weekday

func GetSliceAsE added in v1.2.0

func GetSliceAsE[T values.TypeBase | values.TypeBaseSlice](key string) ([]T, error)

GetSliceAsE 获取 []T 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持转换目标为任意类型的 []TypeBase 和 [][]TypeBase 切片, 如果转换失败, 则返回空的 []T 切片
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration | time.Month | time.Weekday

func GetSliceMap added in v1.2.0

func GetSliceMap[K, V values.TypeBase](key string) []map[K]V

GetSliceMap 获取 []map[K]V 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持转换目标为任意类型的 []map[K]V 切片, 如果转换失败, 则返回空的 []map[K]V 切片
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration | time.Month | time.Weekday

func GetSliceMapD added in v1.2.0

func GetSliceMapD[K, V values.TypeBase](key string, defaultVal []map[K]V) []map[K]V

GetSliceMapD 获取 []map[K]V 类型的缓存值, 如果不存在或已经过期则返回默认值

  • 支持转换目标为任意类型的 []map[K]V 切片, 如果转换失败, 则返回空的 []map[K]V 切片
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration | time.Month | time.Weekday

func GetSliceMapE added in v1.2.0

func GetSliceMapE[K, V values.TypeBase](key string) ([]map[K]V, error)

GetSliceMapE 获取 []map[K]V 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持转换目标为任意类型的 []map[K]V 切片, 如果转换失败, 则返回空的 []map[K]V 切片
  • TypeBase: string | bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Time | time.Duration | time.Month | time.Weekday

func GetString

func GetString(key string) string

GetString 获取 string 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 Stringer 接口的类型

func GetStringD

func GetStringD(key string, defaultVal string) string

GetStringD 获取 string 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 Stringer 接口的类型

func GetStringE added in v1.2.0

func GetStringE(key string) (string, error)

GetStringD 获取 string 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: bool, string, intX, uintX, floatX 以及实现了 Stringer 接口的类型

func GetTagKeys added in v1.2.0

func GetTagKeys(tag string) []string

GetTagKeys 获取指定 Tag 标签对应的所有缓存 Keys 键名

func GetTags added in v1.2.0

func GetTags() []string

GetTags 获取所有Tag标签

func GetTime

func GetTime(key string) time.Time

GetTime 获取 time.Time 类型的缓存值, 如果不存在或已经过期则返回false

  • 如待转换目标是数值(当做Unix秒级时间戳), 则使用 Server/Local 时区
  • 如待转换目标是字符串(日期时间)未包含时区信息时, 则使用 Server/Local 时区
  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeD

func GetTimeD(key string, defaultVal time.Time) time.Time

GetTimeD 获取 time.Time 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 如待转换目标是数值(当做Unix秒级时间戳), 则使用 Server/Local 时区
  • 如待转换目标是字符串(日期时间)未包含时区信息时, 则使用 Server/Local 时区
  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeDuration added in v1.2.0

func GetTimeDuration(key string) time.Duration

GetTimeDuration 获取 time.Duration 类型的缓存值, 如果不存在或已经过期则返回false

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeDurationD added in v1.2.0

func GetTimeDurationD(key string, defaultVal time.Duration) time.Duration

GetTimeDurationD 获取 time.Duration 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeDurationE added in v1.2.0

func GetTimeDurationE(key string) (time.Duration, error)

GetTimeE 获取 time.Duration 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeE added in v1.2.0

func GetTimeE(key string) (time.Time, error)

GetTimeE 获取 time.Time 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 如待转换目标是数值(当做Unix秒级时间戳), 则使用 Server/Local 时区
  • 如待转换目标是字符串(日期时间)未包含时区信息时, 则使用 Server/Local 时区
  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeMonth

func GetTimeMonth(key string) time.Month

GetTimeMonth 获取 time.Month 类型的缓存值, 如果不存在或已经过期则返回false

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeMonthD

func GetTimeMonthD(key string, defaultVal time.Month) time.Month

GetTimeMonthD 获取 time.Month 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeMonthE added in v1.2.0

func GetTimeMonthE(key string) (time.Month, error)

GetTimeMonthE 获取 time.Month 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeWeekday

func GetTimeWeekday(key string) time.Weekday

GetTimeWeekday 获取 time.Weekday 类型的缓存值, 如果不存在或已经过期则返回false

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeWeekdayD

func GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday

GetTimeWeekdayD 获取 time.Weekday 型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeWeekdayE added in v1.2.0

func GetTimeWeekdayE(key string) (time.Weekday, error)

GetTimeWeekdayE 获取 time.Weekday 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetTimeWith added in v1.2.0

func GetTimeWith(key string, location *time.Location) (time.Time, error)

GetTimeWith 获取指定的时区 time.Time 类型的缓存值, 如果不存在或已经过期则返回false

  • 如待转换目标是数值(当做Unix秒级时间戳), 则使用 location 指定的时区
  • 如待转换目标是字符串(日期时间)未包含时区信息时, 则使用 location 指定的时区
  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.Timer 适配接口的类型

func GetUint

func GetUint(key string) uint

GetUint 获取 uint 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint16

func GetUint16(key string) uint16

GetUint16 获取 uint16 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint16D

func GetUint16D(key string, defaultVal uint16) uint16

GetUint16D 获取 uint16 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint16E added in v1.2.0

func GetUint16E(key string) (uint16, error)

GetUint16E 获取 uint16 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint32

func GetUint32(key string) uint32

GetUint32 获取 uint32 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint32D

func GetUint32D(key string, defaultVal uint32) uint32

GetUint32D 获取 uint32 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint32E added in v1.2.0

func GetUint32E(key string) (uint32, error)

GetUint32E 获取 uint32 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint64

func GetUint64(key string) uint64

GetUint64 获取 uint64 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint64D

func GetUint64D(key string, defaultVal uint64) uint64

GetUint64D 获取 uint64 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint64E added in v1.2.0

func GetUint64E(key string) (uint64, error)

GetUint64E 获取 uint64 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint8

func GetUint8(key string) uint8

GetUint8 获取 uint8 类型的缓存值, 如果不存在或已经过期则返回空值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint8D

func GetUint8D(key string, defaultVal uint8) uint8

GetUint8D 获取 uint8 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUint8E added in v1.2.0

func GetUint8E(key string) (uint8, error)

GetUint8E 获取 uint8 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUintD

func GetUintD(key string, defaultVal uint) uint

GetUintD 获取 uint 类型的缓存值, 如果不存在或已经过期则返回指定的 defaultVal 默认值

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func GetUintE added in v1.2.0

func GetUintE(key string) (uint, error)

GetUintE 获取 uint 类型的缓存值, 如果不存在或已经过期则返回空值和错误信息

  • 支持自动转换类型: string, intX, uintX, floatX 以及实现了 values.IntX/UintX 适配接口的类型

func Incr added in v1.2.0

func Incr(key string, opts ...EntryOptions) (int64, error)

Incr 原子性地将键对应的值增加+1, 并返回新的值

  • 如果键不存在, 则将原子值设为 1
  • 如果键存在, 则更新值 +1

func IncrBy added in v1.2.0

func IncrBy(key string, step int64, opts ...EntryOptions) (int64, error)

IncrBy 原子性地将键对应的值增加 Step 步进值, 并返回新的值

  • 如果键不存在, 则将原子值设为 Step
  • 如果键存在, 则更新值 +Step

func LoadDo added in v1.2.0

func LoadDo(key string, refVal any, do func() (setVal any, err error), opts ...EntryOptions) error

LoadDo 获取键对应的值, 并反序列化解析后赋值到 refVal

  • refVal 是一个指向具体类型的指针, 用于存放反序列化后的数据
  • 缓存存在: 则反序列化解析后赋值到 refVal
  • 缓存不存在: 则执行 Fn 函数获取查询值后设置为缓存(Set Use Opts), 最后赋值到 refVal
  • 注意: 请确保 refVal(&T) 的类型和 do() 函数的返回值T类型一致

func MaxEntrySize added in v1.2.0

func MaxEntrySize() int64

MaxEntrySize 获取单个缓存项数据限制的大小(单位: byte)

func MaxSize added in v1.2.0

func MaxSize() int64

MaxSize 返回当前缓存实例的最大容量(单位: byte)

func Metrics added in v1.2.0

func Metrics() *storage.Metrics

Metrics 返回监控指标

func SIntAdd added in v1.2.1

func SIntAdd(key string, items ...int64) error

SIntAdd 向指定 key 的 Int64 集合添加一个或多个成员元素

  • 如果指定 key 的集合不存在, 则新建集合并添加值到集合
  • 如果指定 key 不是一个 Int64 集合, 则返回错误
  • 如果指定 key 的集合存在, 则添加值到集合
  • 如果 items为空, 则什么也不做, 直接返回nil

func SIntClear added in v1.2.2

func SIntClear(key string) error

SIntClear 清空指定 key 的 Int64 集合

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误

func SIntContains added in v1.2.1

func SIntContains(key string, items ...int64) bool

SIntContains 判断一个或多个成员是否全部在指定 key 的 Int64 集合中

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回 false
  • 查询多个成员, 任意一个成员不在集合中, 则返回 false

func SIntContainsSome added in v1.2.1

func SIntContainsSome(key string, items ...int64) bool

SIntContainsSome 判断一个或多个成员的中任意一个成员是否在指定 key 的 Int64 集合中

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回 false
  • 查询多个成员, 任意一个成员存在集合中, 则返回 true

func SIntMembers added in v1.2.1

func SIntMembers(key string) ([]int64, error)

SIntMembers 获取指定 key 的 Int64 集合的所有成员

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误

func SIntRemove added in v1.2.1

func SIntRemove(key string, items ...int64) error

SIntRemove 从指定 key 的 Int64 集合中移除一个或多个成员

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
  • 如果items为空, 则什么也不做, 直接返回nil

func SIntSize added in v1.2.1

func SIntSize(key string) (int64, error)

SIntSize 获取指定 key 的 Int64 集合的成员数量

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误

func SIntTraverse added in v1.2.3

func SIntTraverse(key string, fn func(int64) bool) error

SIntTraverse 遍历指定 key 的 Int64 集合的所有成员

  • 如果指定 key 的集合不存或不是一个 Int64 集合, 则返回错误
  • fn 签名是 iter.Seq[int64], 迭代过程中, 返回 false 则停止遍历

func SStringAdd added in v1.2.1

func SStringAdd(key string, items ...string) error

SStringAdd 向指定 key 的 String 集合添加一个或多个成员元素

  • 如果指定 key 的集合不存在, 则新建集合并添加值到集合
  • 如果指定 key 不是一个 String 集合, 则返回错误
  • 如果指定 key 的集合存在, 则添加值到集合
  • 如果 items为空, 则什么也不做, 直接返回nil

func SStringClear added in v1.2.2

func SStringClear(key string) error

SStringClear 清空指定 key 的 String 集合

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误

func SStringContains added in v1.2.1

func SStringContains(key string, items ...string) bool

SStringContains 判断一个或多个成员是否全部在指定 key 的 String 集合中

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回 false
  • 查询多个成员, 任意一个成员不在集合中, 则返回 false

func SStringContainsSome added in v1.2.1

func SStringContainsSome(key string, items ...string) bool

SStringContainsSome 判断一个或多个成员的中任意一个成员是否在指定 key 的 String 集合中

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回 false
  • 查询多个成员, 任意一个成员存在集合中, 则返回 true

func SStringMembers added in v1.2.1

func SStringMembers(key string) ([]string, error)

SStringMembers 获取指定 key 的 String 集合的所有成员

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误

func SStringRemove added in v1.2.1

func SStringRemove(key string, items ...string) error

SStringRemove 从指定 key 的 String 集合中移除一个或多个成员

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
  • 如果items为空, 则什么也不做, 直接返回nil

func SStringSize added in v1.2.1

func SStringSize(key string) (int64, error)

SStringSize 获取指定 key 的 String 集合的成员数量

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误

func SStringTraverse added in v1.2.3

func SStringTraverse(key string, fn func(string) bool) error

SStringTraverse 遍历指定 key 的 String 集合的所有成员

  • 如果指定 key 的集合不存或不是一个 String 集合, 则返回错误
  • fn 签名是 iter.Seq[string], 迭代过程中, 返回 false 则停止遍历

func Scan

func Scan(key string, refVal any) error

Scan 获取键对应的值, 并解码到 refVal

  • refVal 是一个指向具体数据类型的指针, 用于存放反序列化后的数据
  • 不存在或已经过期或反序列化失败则返回错误
  • 标量类型直接解析到 refVal
  • 其他返回数据为 msgpack 自动反序列化后解析到 refVal

func Set

func Set(key string, value any, opts ...EntryOptions) error

Set 设置键对应的值, 可以通过 EntryOption 设置 Expire, Tag 等选项

func SetDefault added in v1.2.0

func SetDefault(driver storage.Storager)

SetDefault 设置全局默认缓存实例

  • 注意: 此函数禁止内联优化,内联后会导致全局缓存 RedisCache 实例 Stop/Restart 连接异常

func SetLogger

func SetLogger(log *slog.Logger)

func SetWithExists added in v1.2.0

func SetWithExists(key string, value any, opts ...EntryOptions) (updated bool, err error)

SetWithExists 仅当缓存 Key 键存在时才更新设置值, 返回True表示更新成功

func SetWithNotExists added in v1.2.0

func SetWithNotExists(key string, value any, opts ...EntryOptions) (added bool, err error)

SetWithNotExists 仅当缓存 Key 键不存在时才设置值, 返回True表示设置成功

func StorageType added in v1.2.0

func StorageType() storage.StorageType

StorageType 获取缓存存储驱动类型

func TTL added in v1.2.0

func TTL(key string) time.Duration

TTL 获取键的剩余存活时间(精度为秒)

  • 不存在或如果已经过期则返回-1
  • 如果设置了永久存活则返回0

func TotalCount added in v1.2.0

func TotalCount() int64

TotalCount 返回当前缓存项(Key)的总数量(单位: 个)

func TotalSize added in v1.2.0

func TotalSize() int64

TotalSize 返回当前缓存的总大小(单位: byte)

func UpdateFn added in v1.2.0

func UpdateFn(key string, fn func(getVal any, exits bool) (updateVal any, err error), opts ...EntryOptions) (any, error)

UpdateFn 更新键对应的值

  • 如果键不存在, 则执行 Fn 函数设置值并返回设置的默认值(Set Use Opts), 并返回设置的默认值
  • 如果键存在, 则执行 Fn 函数更新值并返回更新的新值(Set Without Opts), 并返回更新的新值

func UseLocal added in v1.2.3

func UseLocal() storage.Storager

UseLocal 仅使用 Local 缓存进行操作( 通常为 MemoryCache )

  • 仅在 L2Cache 驱动模式下有效, 其他驱动模式返回自身驱动实例

func UseRemote added in v1.2.3

func UseRemote() storage.Storager

UseRemote 仅仅使用 Remote 进行缓存操作( 通常为 RedisCache )

  • 仅在 L2Cache 驱动模式下有效, 其他驱动模式返回自身驱动实例

Types

type EntryOptions added in v1.2.3

type EntryOptions = storage.EntryOptions

EntryOptions 缓存项选项, 用于配置缓存行为, 如过期时间、标签等

type L2CacheOptions added in v1.2.0

type L2CacheOptions = driver.L2CacheOptions

L2CacheOptions 缓存配置选项,用于初始化 L2 缓存实例时设置相关参数

预留配置选项,后续扩展使用

type MemoryOptions

type MemoryOptions = driver.MemoryOptions

MemoryOptions 缓存配置选项结构体

  • 关于单个缓存项最大数据大小限制为 MaxSize/CacheShardCount
  • 如 MaxSize 为 128MB, CacheShardCount 为 128 个分片,则单个缓存项最大数据大小为 128*1024*1024/128 = 1MB
  • 如 MaxSize 为 64MB, CacheShardCount 为 128 个分片,则单个缓存项最大数据大小为 64*1024*1024/128 = 512KB
  • 如 MaxSize 为 32MB, CacheShardCount 为 128 个分片,则单个缓存项最大数据大小为 32*1024*1024/128 = 256KB

type RedisOptions

type RedisOptions = driver.RedisOptions

RedisOptions Redis缓存配置信息

type RedisUniversalOptions added in v1.1.0

type RedisUniversalOptions = driver.RedisUniversalOptions

type Storager added in v1.2.3

type Storager = storage.Storager

Storager 缓存驱动统一接口

Directories

Path Synopsis
example
global command
l2cache command
memory command
redis command

Jump to

Keyboard shortcuts

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