driver

package
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: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRedisLockRepeated   = errors.New("cache redisLock: tryLock of repeated locking")
	ErrRedisLockTimeout    = errors.New("cache redisLock: tryLock timeout to acquire")
	ErrRedisLockRecycled   = errors.New("cache redisLock: tryLock of recycled")
	ErrRedisLockFailed     = errors.New("cache redisLock: tryLock failed to acquire")
	ErrRedisUnlockFailed   = errors.New("cache redisLock: unlock failed to release")
	ErrRedisUnlockReleased = errors.New("cache redisLock: unlock of repeated release")
)

Functions

func GetMemoryEntryFromPool

func GetMemoryEntryFromPool() *memoryEntry

GetMemoryEntryFromPool 从 memoryEntrys 池获取一个新的 memoryEntry 对象, 并重置其值

Types

type L2Cache

type L2Cache struct {
	// contains filtered or unexported fields
}

L2Cache Local+Remote 二级缓存实例结构体 (通常为 MemoryCache + RedisCache)

func NewL2Cache

func NewL2Cache(local storage.Storager, remote storage.Storager, options *L2CacheOptions) (*L2Cache, error)

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

  • 需要传入 local 和 remote 缓存驱动实例 (通常为 MemoryCache + RedisCache)

func NewL2CacheDefualt

func NewL2CacheDefualt() (*L2Cache, error)

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

  • local 默认使用 NewMemoryDefault 创建
  • remote 默认使用 NewRedisDefault 创建

func (*L2Cache) Cleanup

func (c *L2Cache) Cleanup() error

Cleanup 手动清理过期数据

func (*L2Cache) Clear

func (c *L2Cache) Clear() error

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

  • 注意: 仅清空内存缓存部分, Redis 部分不会清除

func (*L2Cache) ClearAsync

func (c *L2Cache) ClearAsync() error

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

  • 注意: 仅清空内存缓存部分, Redis 部分不会清除

func (*L2Cache) Contains

func (c *L2Cache) Contains(keys ...string) bool

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

func (*L2Cache) ContainsSome

func (c *L2Cache) ContainsSome(keys ...string) bool

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

func (*L2Cache) ContainsSomeTags

func (c *L2Cache) ContainsSomeTags(tags ...string) bool

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

func (*L2Cache) ContainsTagKey

func (c *L2Cache) ContainsTagKey(tag, key string) bool

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

func (*L2Cache) ContainsTags

func (c *L2Cache) ContainsTags(tags ...string) bool

ContainsTags 检查 Tag 标签是否存在

func (*L2Cache) Decr

func (c *L2Cache) Decr(key string, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为 -1 (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*L2Cache) DecrBy

func (c *L2Cache) DecrBy(key string, step int64, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为 -step (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*L2Cache) DefaultExpire

func (c *L2Cache) DefaultExpire() time.Duration

DefaultExpire 获取默认过期时间

  • 注意: 仅返回内存缓存部分的默认过期时间, Redis 部分不会计算在内

func (*L2Cache) Delete

func (c *L2Cache) Delete(keys ...string) error

Delete 依据缓存key删除缓存项

func (*L2Cache) DeleteTags

func (c *L2Cache) DeleteTags(tags ...string) error

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

func (*L2Cache) Expire

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

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

func (*L2Cache) ExpireAt

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

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

func (*L2Cache) Get

func (c *L2Cache) Get(key string) (any, error)

Get 获取键对应的值

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

func (*L2Cache) GetAndDelete

func (c *L2Cache) GetAndDelete(key string) (any, error)

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

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

func (*L2Cache) GetAndSet

func (c *L2Cache) GetAndSet(key string, value any, opts ...storage.EntryOptions) (oldVal any, err error)

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

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

func (*L2Cache) GetEntry

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

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

func (*L2Cache) GetKeyTags

func (c *L2Cache) GetKeyTags(key string) []string

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

func (*L2Cache) GetOrSet

func (c *L2Cache) GetOrSet(key string, value any, opts ...storage.EntryOptions) (any, error)

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

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

func (*L2Cache) GetTagKeys

func (c *L2Cache) GetTagKeys(tag string) []string

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

func (*L2Cache) GetTags

func (c *L2Cache) GetTags() []string

GetTags 获取所有Tag标签

func (*L2Cache) Incr

func (c *L2Cache) Incr(key string, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为1 (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*L2Cache) IncrBy

func (c *L2Cache) IncrBy(key string, step int64, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为Step (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*L2Cache) IsGlobal

func (c *L2Cache) IsGlobal() bool

IsGlobal 是否为全局缓存实例

func (*L2Cache) LiveState

func (c *L2Cache) LiveState() storage.LiveState

LiveState 获取当前缓存实例的运行状态

func (*L2Cache) LoadDo

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

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

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

func (*L2Cache) MaxEntrySize

func (c *L2Cache) MaxEntrySize() int64

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

  • 注意: 仅返回内存缓存部分的容量, Redis 部分不会计算在内

func (*L2Cache) MaxSize

func (c *L2Cache) MaxSize() int64

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

  • 注意: 仅返回内存缓存部分的容量, Redis 部分不会计算在内

func (*L2Cache) Metrics

func (c *L2Cache) Metrics() *storage.Metrics

Metrics 返回监控指标

  • 注意: 仅返回内存缓存部分的监控指标, Redis 部分不会计算在内

func (*L2Cache) SIntAdd

func (c *L2Cache) SIntAdd(key string, items ...int64) error

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

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

func (*L2Cache) SIntClear

func (c *L2Cache) SIntClear(key string) error

SIntClear 清空指定 key 的 Int64 集合

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

func (*L2Cache) SIntContains

func (c *L2Cache) SIntContains(key string, items ...int64) bool

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

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

func (*L2Cache) SIntContainsSome

func (c *L2Cache) SIntContainsSome(key string, items ...int64) bool

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

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

func (*L2Cache) SIntMembers

func (c *L2Cache) SIntMembers(key string) ([]int64, error)

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

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

func (*L2Cache) SIntRemove

func (c *L2Cache) SIntRemove(key string, items ...int64) error

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

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

func (*L2Cache) SIntSize

func (c *L2Cache) SIntSize(key string) (int64, error)

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

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

func (*L2Cache) SIntTraverse

func (c *L2Cache) SIntTraverse(key string, fn func(int64) bool) error

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

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

func (*L2Cache) SStringAdd

func (c *L2Cache) SStringAdd(key string, items ...string) error

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

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

func (*L2Cache) SStringClear

func (c *L2Cache) SStringClear(key string) error

SStringClear 清空指定 key 的 String 集合

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

func (*L2Cache) SStringContains

func (c *L2Cache) SStringContains(key string, items ...string) bool

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

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

func (*L2Cache) SStringContainsSome

func (c *L2Cache) SStringContainsSome(key string, items ...string) bool

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

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

func (*L2Cache) SStringMembers

func (c *L2Cache) SStringMembers(key string) ([]string, error)

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

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

func (*L2Cache) SStringRemove

func (c *L2Cache) SStringRemove(key string, items ...string) error

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

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

func (*L2Cache) SStringSize

func (c *L2Cache) SStringSize(key string) (int64, error)

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

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

func (*L2Cache) SStringTraverse

func (c *L2Cache) SStringTraverse(key string, fn func(string) bool) error

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

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

func (*L2Cache) Scan

func (c *L2Cache) Scan(key string, refVal any) error

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

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

func (*L2Cache) Set

func (c *L2Cache) Set(key string, value any, opts ...storage.EntryOptions) error

Set 设置键值对,如果空间不够则按 LRU 机制删除最少使用的数据

func (*L2Cache) SetGlobal

func (c *L2Cache) SetGlobal()

setGlobal 设置实例为全局缓存标记

func (*L2Cache) SetWithExists

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

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

func (*L2Cache) SetWithNotExists

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

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

func (*L2Cache) StorageType

func (c *L2Cache) StorageType() storage.StorageType

StorageType 获取缓存存储驱动类型

func (*L2Cache) TTL

func (c *L2Cache) TTL(key string) time.Duration

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

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

func (*L2Cache) TotalCount

func (c *L2Cache) TotalCount() int64

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

  • 注意: 仅返回内存缓存部分的项数量, Redis 部分不会计算在内

func (*L2Cache) TotalSize

func (c *L2Cache) TotalSize() int64

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

  • 注意: 仅返回内存缓存部分的总大小, Redis 部分不会计算在内

func (*L2Cache) UpdateFn

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

UpdateFn 更新键对应的值

  • 如果键不存在, 则执行 Fn 函数设置值并返回设置的默认值(Set Use Opts), 并返回设置的默认值
  • 如果键存在, 则执行 Fn 函数更新值并返回更新的新值(Set Without Opts), 并返回更新的新值(过期时间/Tags保存不变)

func (*L2Cache) UseLocal

func (c *L2Cache) UseLocal() storage.Storager

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

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

func (*L2Cache) UseRemote

func (c *L2Cache) UseRemote() storage.Storager

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

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

type L2CacheOptions

type L2CacheOptions struct{}

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

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

type MemoryCache

type MemoryCache struct {
	// contains filtered or unexported fields
}

MemoryCache 内存缓存实例结构体

func NewMemory

func NewMemory(ops *MemoryOptions) (*MemoryCache, error)

NewMemory 创建一个新的 Memcache 实例

  • maxSize: 缓存的最大大小,单位为MB, 如果设置为0或负数,则默认为128MB。

func NewMemoryDefault

func NewMemoryDefault() (*MemoryCache, error)

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

  • MaxSize: 128 // MB
  • CacheShardCount: 128
  • TagShardCount: 32
  • DefaultExpire: 0
  • CleanupInterval: time.Minute

func (*MemoryCache) Cleanup

func (c *MemoryCache) Cleanup() error

Cleanup 手动清理过期数据

func (*MemoryCache) Clear

func (c *MemoryCache) Clear() error

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

func (*MemoryCache) ClearAsync

func (c *MemoryCache) ClearAsync() error

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

func (*MemoryCache) Contains

func (c *MemoryCache) Contains(keys ...string) bool

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

func (*MemoryCache) ContainsSome

func (c *MemoryCache) ContainsSome(keys ...string) bool

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

func (*MemoryCache) ContainsSomeTags

func (c *MemoryCache) ContainsSomeTags(tags ...string) bool

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

func (*MemoryCache) ContainsTagKey

func (c *MemoryCache) ContainsTagKey(tag, key string) bool

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

func (*MemoryCache) ContainsTags

func (c *MemoryCache) ContainsTags(tags ...string) bool

ContainsTags 检查 Tag 标签是否存在

func (*MemoryCache) Decr

func (c *MemoryCache) Decr(key string, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为 -1 (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*MemoryCache) DecrBy

func (c *MemoryCache) DecrBy(key string, step int64, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为 -Step (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*MemoryCache) DefaultExpire

func (c *MemoryCache) DefaultExpire() time.Duration

DefaultExpire 获取默认过期时间

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(keys ...string) error

Delete 依据缓存key删除缓存项

func (*MemoryCache) DeleteTags

func (c *MemoryCache) DeleteTags(tags ...string) error

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

func (*MemoryCache) Expire

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

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

func (*MemoryCache) ExpireAt

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

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

func (*MemoryCache) Get

func (c *MemoryCache) Get(key string) (any, error)

Get 获取键对应的值

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

func (*MemoryCache) GetAndDelete

func (c *MemoryCache) GetAndDelete(key string) (any, error)

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

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

func (*MemoryCache) GetAndSet

func (c *MemoryCache) GetAndSet(key string, value any, opts ...storage.EntryOptions) (oldVal any, err error)

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

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

func (*MemoryCache) GetEntry

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

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

func (*MemoryCache) GetKeyTags

func (c *MemoryCache) GetKeyTags(key string) []string

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

func (*MemoryCache) GetOrSet

func (c *MemoryCache) GetOrSet(key string, value any, opts ...storage.EntryOptions) (any, error)

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

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

func (*MemoryCache) GetTagKeys

func (c *MemoryCache) GetTagKeys(tag string) []string

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

func (*MemoryCache) GetTags

func (c *MemoryCache) GetTags() []string

GetTags 获取所有Tag标签

func (*MemoryCache) Incr

func (c *MemoryCache) Incr(key string, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为1 (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*MemoryCache) IncrBy

func (c *MemoryCache) IncrBy(key string, step int64, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为Step (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*MemoryCache) IsGlobal

func (c *MemoryCache) IsGlobal() bool

IsGlobal 是否为全局缓存实例

func (*MemoryCache) Kind

func (c *MemoryCache) Kind(key string) (storage.Kind, error)

Kind 获取缓存 Key 的类型信息

  • 不存在或已经过期则返回错误

func (*MemoryCache) LiveState

func (c *MemoryCache) LiveState() storage.LiveState

LiveState 获取当前缓存实例的运行状态

func (*MemoryCache) LoadDo

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

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

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

func (*MemoryCache) MaxEntrySize

func (c *MemoryCache) MaxEntrySize() int64

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

func (*MemoryCache) MaxSize

func (c *MemoryCache) MaxSize() int64

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

func (*MemoryCache) Metrics

func (c *MemoryCache) Metrics() *storage.Metrics

Metrics 返回监控指标

func (*MemoryCache) SIntAdd

func (c *MemoryCache) SIntAdd(key string, items ...int64) error

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

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

func (*MemoryCache) SIntClear

func (c *MemoryCache) SIntClear(key string) error

SIntClear 清空指定 key 的 Int64 集合

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

func (*MemoryCache) SIntContains

func (c *MemoryCache) SIntContains(key string, items ...int64) bool

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

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

func (*MemoryCache) SIntContainsSome

func (c *MemoryCache) SIntContainsSome(key string, items ...int64) bool

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

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

func (*MemoryCache) SIntMembers

func (c *MemoryCache) SIntMembers(key string) ([]int64, error)

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

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

func (*MemoryCache) SIntRemove

func (c *MemoryCache) SIntRemove(key string, items ...int64) error

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

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

func (*MemoryCache) SIntSize

func (c *MemoryCache) SIntSize(key string) (int64, error)

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

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

func (*MemoryCache) SIntTraverse

func (c *MemoryCache) SIntTraverse(key string, fn func(int64) bool) error

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

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

func (*MemoryCache) SStringAdd

func (c *MemoryCache) SStringAdd(key string, items ...string) error

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

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

func (*MemoryCache) SStringClear

func (c *MemoryCache) SStringClear(key string) error

SStringClear 清空指定 key 的 String 集合

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

func (*MemoryCache) SStringContains

func (c *MemoryCache) SStringContains(key string, items ...string) bool

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

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

func (*MemoryCache) SStringContainsSome

func (c *MemoryCache) SStringContainsSome(key string, items ...string) bool

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

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

func (*MemoryCache) SStringMembers

func (c *MemoryCache) SStringMembers(key string) ([]string, error)

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

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

func (*MemoryCache) SStringRemove

func (c *MemoryCache) SStringRemove(key string, items ...string) error

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

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

func (*MemoryCache) SStringSize

func (c *MemoryCache) SStringSize(key string) (int64, error)

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

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

func (*MemoryCache) SStringTraverse

func (c *MemoryCache) SStringTraverse(key string, fn func(string) bool) error

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

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

func (*MemoryCache) Scan

func (c *MemoryCache) Scan(key string, refVal any) error

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

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

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, value any, opts ...storage.EntryOptions) error

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

  • key 为空 或 value 为nil 则返回错误
  • 如缓存 key 已存在且 opts 为空, 则保持过期时间Tags等选项
  • 如缓存 key 已存在且 opts 不为空, 则更新过期时间Tags等opts选项(原Tag保留, 如有新Tag则追加)
  • 如缓存 key 不存在且 opts 为空, 则使用默认选项存储数据
  • 如缓存 key 不存在且 opts 不为空, 则使用opts选项存储数据

func (*MemoryCache) SetGlobal

func (c *MemoryCache) SetGlobal()

SetGlobal 设置实例为全局缓存标记

func (*MemoryCache) SetWithExists

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

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

func (*MemoryCache) SetWithNotExists

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

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

func (*MemoryCache) StorageType

func (c *MemoryCache) StorageType() storage.StorageType

StorageType 获取缓存存储驱动类型

func (*MemoryCache) TTL

func (c *MemoryCache) TTL(key string) time.Duration

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

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

func (*MemoryCache) TotalCount

func (c *MemoryCache) TotalCount() int64

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

func (*MemoryCache) TotalSize

func (c *MemoryCache) TotalSize() int64

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

func (*MemoryCache) UpdateFn

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

UpdateFn 更新键对应的值

  • 如果键不存在, 则执行 Fn 函数设置值并返回设置的默认值(Set Use Opts), 并返回设置的默认值
  • 如果键存在, 则执行 Fn 函数更新值并返回更新的新值(Set Without Opts), 并返回更新的新值(过期时间/Tags保存不变)

func (*MemoryCache) UseLocal

func (c *MemoryCache) UseLocal() storage.Storager

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

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

func (*MemoryCache) UseRemote

func (c *MemoryCache) UseRemote() storage.Storager

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

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

type 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秒
}

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 RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache Redis缓存驱动实现

func NewRedis

func NewRedis(options *RedisOptions) (*RedisCache, error)

NewRedis 创建一个 RedisCache 缓存驱动实例

  • options RedisOptions

func NewRedisDefault

func NewRedisDefault() (*RedisCache, error)

NewRedisDefault 使用默认配置信息创建一个 RedisCache 缓存驱动实例

  • Addrs: []string{"localhost:6379"} 无密码连接

func (*RedisCache) Cleanup

func (c *RedisCache) Cleanup() error

Cleanup 手动清理过期数据

func (*RedisCache) Clear

func (c *RedisCache) Clear() error

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

func (*RedisCache) ClearAsync

func (c *RedisCache) ClearAsync() error

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

func (*RedisCache) Contains

func (c *RedisCache) Contains(keys ...string) bool

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

func (*RedisCache) ContainsSome

func (c *RedisCache) ContainsSome(keys ...string) bool

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

func (*RedisCache) ContainsSomeTags

func (c *RedisCache) ContainsSomeTags(tags ...string) bool

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

func (*RedisCache) ContainsTagKey

func (c *RedisCache) ContainsTagKey(tag, key string) bool

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

func (*RedisCache) ContainsTags

func (c *RedisCache) ContainsTags(tags ...string) bool

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

func (*RedisCache) Decr

func (c *RedisCache) Decr(key string, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为 -1 (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*RedisCache) DecrBy

func (c *RedisCache) DecrBy(key string, step int64, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为 -step (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*RedisCache) DefaultExpire

func (c *RedisCache) DefaultExpire() time.Duration

DefaultExpire 获取默认过期时间(精度为秒)

func (*RedisCache) Delete

func (c *RedisCache) Delete(keys ...string) error

Delete 删除一个或多个键

func (*RedisCache) DeleteTags

func (c *RedisCache) DeleteTags(tags ...string) error

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

func (*RedisCache) Expire

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

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

func (*RedisCache) ExpireAt

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

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

func (*RedisCache) Get

func (c *RedisCache) Get(key string) (any, error)

Get 获取键对应的值

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

func (*RedisCache) GetAndDelete

func (c *RedisCache) GetAndDelete(key string) (any, error)

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

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

func (*RedisCache) GetAndSet

func (c *RedisCache) GetAndSet(key string, value any, opts ...storage.EntryOptions) (any, error)

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

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

func (*RedisCache) GetEntry

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

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

func (*RedisCache) GetKeyTags

func (c *RedisCache) GetKeyTags(key string) []string

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

func (*RedisCache) GetOrSet

func (c *RedisCache) GetOrSet(key string, value any, opts ...storage.EntryOptions) (any, error)

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

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

func (*RedisCache) GetTagKeys

func (c *RedisCache) GetTagKeys(tag string) []string

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

func (*RedisCache) GetTags

func (c *RedisCache) GetTags() []string

GetTags 获取所有Tag标签

func (*RedisCache) Incr

func (c *RedisCache) Incr(key string, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为1 (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*RedisCache) IncrBy

func (c *RedisCache) IncrBy(key string, step int64, opts ...storage.EntryOptions) (int64, error)

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

  • 如果键不存在, 则将原子值设为Step (Set Use Opts)
  • 如果键存在, 则更新的新值(Set Without Opts)

func (*RedisCache) IsGlobal

func (c *RedisCache) IsGlobal() bool

IsGlobal 是否为全局缓存实例

func (*RedisCache) LiveState

func (c *RedisCache) LiveState() storage.LiveState

LiveState 获取当前缓存实例的运行状态

func (*RedisCache) LoadDo

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

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

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

func (*RedisCache) MaxEntrySize

func (c *RedisCache) MaxEntrySize() int64

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

func (*RedisCache) MaxSize

func (c *RedisCache) MaxSize() int64

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

  • Redis 如果返回值为 0, 则表示没有限制

func (*RedisCache) Metrics

func (c *RedisCache) Metrics() *storage.Metrics

Metrics 返回监控指标

func (*RedisCache) RedisInfo

func (c *RedisCache) RedisInfo(section string, sub string) (string, error)

RedisInfo 获取当前链接的 Redis 服务器信息

  • section: 要获取信息组名称(不区分大小写),例如 "server" 或 "clients"
  • sub: 要获取信息子项(不区分大小写),例如 "redis_version" 或 "connected_clients"
  • RedisInfo("server", "redis_version")
  • RedisInfo("memory", "used_memory")
  • https://redis.ac.cn/docs/latest/commands/info/

func (*RedisCache) RedisVerGreaterEqual

func (c *RedisCache) RedisVerGreaterEqual(verArgs ...int) bool

RedisVerGreaterEqual 检查指定版本号是否大于等于当前客户端链接的服务器Redis版本

  • (6) 检查当前版本是否大于等于 "6.X.X"
  • (6,2) 检查当前版本是否大于等于 "6.2.X"
  • (6,2,14) 检查当前版本是否大于等于 "6.2.14"

func (*RedisCache) RedisVerLessEqual

func (c *RedisCache) RedisVerLessEqual(verArgs ...int) bool

RedisVerLessEqual 检查指定版本号是否小于等于当前客户端链接的服务器Redis版本

  • (6) 检查当前版本是否小于等于 "6.X.X"
  • (6,2) 检查当前版本是否小于等于 "6.2.X"
  • (6,2,14) 检查当前版本是否小于等于 "6.2.14"

func (*RedisCache) SIntAdd

func (c *RedisCache) SIntAdd(key string, items ...int64) error

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

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

func (*RedisCache) SIntClear

func (c *RedisCache) SIntClear(key string) error

SIntClear 清空指定 key 的 Int64 集合

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

func (*RedisCache) SIntContains

func (c *RedisCache) SIntContains(key string, items ...int64) bool

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

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

func (*RedisCache) SIntContainsSome

func (c *RedisCache) SIntContainsSome(key string, items ...int64) bool

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

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

func (*RedisCache) SIntMembers

func (c *RedisCache) SIntMembers(key string) ([]int64, error)

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

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

func (*RedisCache) SIntRemove

func (c *RedisCache) SIntRemove(key string, items ...int64) error

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

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

func (*RedisCache) SIntSize

func (c *RedisCache) SIntSize(key string) (int64, error)

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

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

func (*RedisCache) SIntTraverse

func (c *RedisCache) SIntTraverse(key string, fn func(int64) bool) error

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

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

func (*RedisCache) SStringAdd

func (c *RedisCache) SStringAdd(key string, items ...string) error

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

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

func (*RedisCache) SStringClear

func (c *RedisCache) SStringClear(key string) error

SStringClear 清空指定 key 的 String 集合

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

func (*RedisCache) SStringContains

func (c *RedisCache) SStringContains(key string, items ...string) bool

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

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

func (*RedisCache) SStringContainsSome

func (c *RedisCache) SStringContainsSome(key string, items ...string) bool

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

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

func (*RedisCache) SStringMembers

func (c *RedisCache) SStringMembers(key string) ([]string, error)

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

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

func (*RedisCache) SStringRemove

func (c *RedisCache) SStringRemove(key string, items ...string) error

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

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

func (*RedisCache) SStringSize

func (c *RedisCache) SStringSize(key string) (int64, error)

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

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

func (*RedisCache) SStringTraverse

func (c *RedisCache) SStringTraverse(key string, fn func(string) bool) error

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

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

func (*RedisCache) Scan

func (c *RedisCache) Scan(key string, refVal any) (err error)

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

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

func (*RedisCache) Set

func (c *RedisCache) Set(key string, value any, opts ...storage.EntryOptions) error

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

  • key 为空 或 value 为nil 则返回错误
  • 如缓存 key 已存在且 opts 为空, 则保持过期时间Tags等选项
  • 如缓存 key 已存在且 opts 不为空, 则更新过期时间Tags等opts选项(原Tag保留, 如有新Tag则追加)
  • 如缓存 key 不存在且 opts 为空, 则使用默认选项存储数据
  • 如缓存 key 不存在且 opts 不为空, 则使用opts选项存储数据

func (*RedisCache) SetGlobal

func (c *RedisCache) SetGlobal()

SetGlobal 设置实例为全局缓存标记

func (*RedisCache) SetWithExists

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

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

func (*RedisCache) SetWithNotExists

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

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

func (*RedisCache) StorageType

func (c *RedisCache) StorageType() storage.StorageType

StorageType 获取缓存存储驱动类型

func (*RedisCache) TTL

func (c *RedisCache) TTL(key string) time.Duration

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

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

func (*RedisCache) TotalCount

func (c *RedisCache) TotalCount() int64

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

func (*RedisCache) TotalSize

func (c *RedisCache) TotalSize() int64

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

func (*RedisCache) UpdateFn

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

UpdateFn 更新键对应的值

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

func (*RedisCache) UseLocal

func (c *RedisCache) UseLocal() storage.Storager

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

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

func (*RedisCache) UseRemote

func (c *RedisCache) UseRemote() storage.Storager

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

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

type RedisOptions

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
}

RedisOptions Redis缓存配置信息

type RedisUniversalOptions

type RedisUniversalOptions = redis.UniversalOptions

Client Redis服务器链接选项,使用 UniversalClient 统一客户端,UniversalClient是对 Client 、 ClusterClient 、 FailoverClient 客户端的包装。

Jump to

Keyboard shortcuts

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