Documentation
¶
Index ¶
- func ForgetLoad(key string)
- func LoadOrStore[T any](key string, loader func() (T, error)) (T, error)
- type Lock
- type RedisCache
- func (rc *RedisCache) Close() error
- func (rc *RedisCache) Get(key string, value interface{}) (err error)
- func (rc *RedisCache) GetRedisClient() *cache.Cache
- func (rc *RedisCache) Health(ctx context.Context) error
- func (rc *RedisCache) IsExists(key string) bool
- func (rc *RedisCache) Lock(ctx context.Context, key string, ttl, waitTimeout time.Duration) (*Lock, error)
- func (rc *RedisCache) Set(Key string, value interface{}) (err error)
- func (rc *RedisCache) SetDefaultTTL(ttl time.Duration)
- func (rc *RedisCache) SetTtl(key string, value interface{}, ttl time.Duration) (err error)
- func (rc *RedisCache) SetTtlWithJitter(ctx context.Context, key string, value interface{}, baseTTL time.Duration) error
- func (rc *RedisCache) TryLock(ctx context.Context, key string, ttl time.Duration) (*Lock, error)
- type RedisOptions
- type Rediser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ForgetLoad ¶ added in v1.5.0
func ForgetLoad(key string)
ForgetLoad 清除 singleflight 组内指定 key 的进行中状态(测试与手动失效用)。
Types ¶
type Lock ¶ added in v1.5.0
type Lock struct {
// contains filtered or unexported fields
}
Lock 表示一个已持有的分布式锁。 释放或续期时都会校验持有者 token,防止误删他人持有的锁。
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache 封装 go-redis 与本地缓存代理。
func GetGlobalCache ¶ added in v1.3.0
func GetGlobalCache() *RedisCache
GetGlobalCache 返回最近一次成功初始化的 Redis 缓存实例。 未初始化时返回 nil,调用方必须判空。
func Init ¶
func Init(ctx context.Context, redisOptions RedisOptions) (*RedisCache, error)
Init 初始化 Redis 缓存客户端并执行连通性检查。 初始化失败时关闭已创建的客户端并返回错误;失败后可安全重试(不再被 sync.Once 锁死)。 成功后实例会写入进程级全局变量,供 GetGlobalCache 访问。
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 ¶ added in v1.3.0
func (rc *RedisCache) Health(ctx context.Context) error
Health 检查 Redis 连通性。
func (*RedisCache) Lock ¶ added in v1.5.0
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 ¶ added in v1.3.0
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 ¶ added in v1.5.0
func (rc *RedisCache) SetTtlWithJitter(ctx context.Context, key string, value interface{}, baseTTL time.Duration) error
SetTtlWithJitter 设置带随机抖动的过期时间,用于避免缓存同时过期导致的雪崩。 实际 TTL 在 baseTTL 的 ±20% 范围内随机;写入失败返回错误。
type RedisOptions ¶
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 定义缓存操作接口。