cache

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForgetLoad

func ForgetLoad(key string)

ForgetLoad 清除 singleflight 组内指定 key 的进行中状态(测试与手动失效用)。

func LoadOrStore

func LoadOrStore[T any](key string, loader func() (T, error)) (T, error)

LoadOrStore 防击穿:多个并发请求命中同一个 key 时只执行一次 loader, 其余请求共享结果。loader 返回错误时不会缓存错误结果,后续请求可重试。 典型用法:缓存 miss 后回源数据库时包装 loader。

Types

type Lock

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

Lock 表示一个已持有的分布式锁。 释放或续期时都会校验持有者 token,防止误删他人持有的锁。

func (*Lock) Renew

func (l *Lock) Renew(ctx context.Context, ttl time.Duration) error

Renew 续期锁;仅持有者 token 匹配时更新过期时间。

func (*Lock) Unlock

func (l *Lock) Unlock(ctx context.Context) error

Unlock 释放锁;只有持有者 token 匹配时才删除(防误删)。 重复释放安全。

type RedisCache

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

RedisCache 封装 go-redis 与本地缓存代理。

func GetGlobalCache

func GetGlobalCache() *RedisCache

GetGlobalCache 返回最近一次成功初始化的 Redis 缓存实例。 未初始化时返回 nil,调用方必须判空。

func Init

func Init(ctx context.Context, redisOptions RedisOptions) (*RedisCache, error)

Init 初始化 Redis 缓存客户端并执行连通性检查。 初始化失败时关闭已创建的客户端并返回错误;失败后可安全重试(不再被 sync.Once 锁死)。 成功后实例会写入进程级全局变量,供 GetGlobalCache 访问。

func (*RedisCache) Close

func (rc *RedisCache) Close() error

Close 关闭底层 Redis 连接。

func (*RedisCache) Get

func (rc *RedisCache) Get(key string, value interface{}) (err error)

Get 获取 key 对应的值,反序列化到 value。

func (*RedisCache) GetRedisClient

func (rc *RedisCache) GetRedisClient() *cache.Cache

GetRedisClient 返回底层缓存代理,用于高级操作。

func (*RedisCache) Health

func (rc *RedisCache) Health(ctx context.Context) error

Health 检查 Redis 连通性。

func (*RedisCache) IsExists

func (rc *RedisCache) IsExists(key string) bool

IsExists 判断 key 是否存在。

func (*RedisCache) Lock

func (rc *RedisCache) Lock(ctx context.Context, key string, ttl, waitTimeout time.Duration) (*Lock, error)

Lock 阻塞获取分布式锁,直到成功、超时或 Context 取消。 waitTimeout 非正数时使用 5 秒默认值。

func (*RedisCache) Set

func (rc *RedisCache) Set(Key string, value interface{}) (err error)

Set 使用默认 TTL 添加 key-value。

func (*RedisCache) SetDefaultTTL

func (rc *RedisCache) SetDefaultTTL(ttl time.Duration)

SetDefaultTTL 设置默认过期时间,影响后续未显式指定 TTL 的写入。

func (*RedisCache) SetTtl

func (rc *RedisCache) SetTtl(key string, value interface{}, ttl time.Duration) (err error)

SetTtl 设置 key 及过期时间;ttl 非正数时使用默认 TTL。

func (*RedisCache) SetTtlWithJitter

func (rc *RedisCache) SetTtlWithJitter(ctx context.Context, key string, value interface{}, baseTTL time.Duration) error

SetTtlWithJitter 设置带随机抖动的过期时间,用于避免缓存同时过期导致的雪崩。 实际 TTL 在 baseTTL 的 ±20% 范围内随机;写入失败返回错误。

func (*RedisCache) TryLock

func (rc *RedisCache) TryLock(ctx context.Context, key string, ttl time.Duration) (*Lock, error)

TryLock 尝试获取分布式锁,立即返回。 获取成功返回非 nil Lock;key 已被占用时返回 (nil, nil);错误时返回错误。

type RedisOptions

type RedisOptions redis.Options

RedisOptions 与 go-redis 的 Options 保持等价,供配置结构直接使用。

type Rediser

type Rediser interface {
	Get(key string, value interface{}) (err error)                       // 获取key-value
	GetRedisClient() *cache.Cache                                        // 操作redis客户端
	IsExists(key string) bool                                            // 判断key是否存在
	Set(Key string, value interface{}) (err error)                       // 添加key-value
	SetTtl(key string, value interface{}, ttl time.Duration) (err error) // 设置key超时时间
}

Rediser 定义缓存操作接口。

Jump to

Keyboard shortcuts

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