Documentation
¶
Index ¶
- Variables
- func AutoInitKeyBuilder(opts ...KeyBuilderOption)
- func Decr(ctx context.Context, key string) (int64, error)
- func DeleteWithPrefix(ctx context.Context, key string, prefix string) error
- func ExtendLock(ctx context.Context, token *LockToken, ttl time.Duration) error
- func ForceUnlock(ctx context.Context, key string) error
- func GetLockTTL(ctx context.Context, key string) (time.Duration, error)
- func GetRaw(ctx context.Context, key string) (string, error)
- func GetTTL(ctx context.Context, key string) (time.Duration, error)
- func GetWithPrefix(ctx context.Context, key string, dest any, prefix string) bool
- func Incr(ctx context.Context, key string) (int64, error)
- func IncrBy(ctx context.Context, key string, value int64) (int64, error)
- func Init()
- func InitKeyBuilder(prefix string, opts ...KeyBuilderOption)
- func IsLocked(ctx context.Context, key string) (bool, error)
- func K(key string) string
- func KCounter(key string) string
- func KLock(key string) string
- func KPerm(key string) string
- func KSession(key string) string
- func KTemp(key string) string
- func Lock(ctx context.Context, key string, ttl time.Duration) (bool, error)
- func LockWithPrefix(ctx context.Context, key string, ttl time.Duration, prefix string) (bool, error)
- func SetExpire(ctx context.Context, key string, ttl time.Duration) (bool, error)
- func SetRaw(ctx context.Context, key string, value string, ttl time.Duration) error
- func SetWithPrefix(ctx context.Context, key string, value any, ttl time.Duration, prefix string) error
- func Unlock(ctx context.Context, token *LockToken) error
- func UnlockByKey(ctx context.Context, key string) error
- func UnlockWithPrefix(ctx context.Context, key string, prefix string) error
- func WithLock(ctx context.Context, key string, ttl time.Duration, fn func() error) error
- func WithLockAutoExtend(ctx context.Context, key string, initialTTL time.Duration, ...) error
- type CacheService
- type KeyBuilder
- func (kb *KeyBuilder) Build(key string) string
- func (kb *KeyBuilder) BuildCounter(key string) string
- func (kb *KeyBuilder) BuildLock(key string) string
- func (kb *KeyBuilder) BuildPattern(pattern string) string
- func (kb *KeyBuilder) BuildPerm(key string) string
- func (kb *KeyBuilder) BuildSession(key string) string
- func (kb *KeyBuilder) BuildTemp(key string) string
- func (kb *KeyBuilder) GetPrefix() string
- func (kb *KeyBuilder) SetPrefix(prefix string) *KeyBuilder
- type KeyBuilderOption
- type LockToken
Constants ¶
This section is empty.
Variables ¶
var ( ErrLockNotHeld = errors.New("锁未被当前客户端持有") ErrLockExpired = errors.New("锁已过期") ErrRedisNotReady = errors.New("Redis 未初始化") )
分布式锁错误
Functions ¶
func AutoInitKeyBuilder ¶
func AutoInitKeyBuilder(opts ...KeyBuilderOption)
AutoInitKeyBuilder 自动从配置初始化键名构建器 配置示例:
app: site_name: "site_a" env: "prod"
func DeleteWithPrefix ¶
DeleteWithPrefix 带前缀的缓存删除
func ExtendLock ¶
ExtendLock 续期锁 参数: token 锁令牌,ttl 新的过期时间
func ForceUnlock ¶
ForceUnlock 强制释放锁(危险操作,仅用于管理场景) 注意: 此函数不检查 Token,强制删除锁
func GetLockTTL ¶
GetLockTTL 获取锁的剩余过期时间
func GetWithPrefix ¶
GetWithPrefix 带前缀的缓存获取
func InitKeyBuilder ¶
func InitKeyBuilder(prefix string, opts ...KeyBuilderOption)
InitKeyBuilder 初始化全局键名构建器 参数: prefix 站点别名,如果为空则自动从配置读取 示例:
InitKeyBuilder("site_a") // 手动指定
InitKeyBuilder("") // 自动从配置读取
InitKeyBuilder("", WithSeparator(":")) // 自动读取 + 自定义分隔符
func LockWithPrefix ¶
func LockWithPrefix(ctx context.Context, key string, ttl time.Duration, prefix string) (bool, error)
LockWithPrefix 带前缀的分布式锁
func SetWithPrefix ¶
func SetWithPrefix(ctx context.Context, key string, value any, ttl time.Duration, prefix string) error
SetWithPrefix 带前缀的缓存设置
func UnlockByKey ¶
UnlockByKey 按键名释放锁(不安全,仅用于旧代码兼容) 注意: 此函数不检查 Token,任何客户端都能释放锁
func UnlockWithPrefix ¶
UnlockWithPrefix 带前缀的锁释放 注意: 此函数不检查 Token,仅用于向后兼容,建议使用 NewLock/Unlock 组合
Types ¶
type CacheService ¶
type CacheService interface {
// Get 获取缓存值,如果存在则反序列化到 dest 并返回 true
Get(ctx context.Context, key string, dest any) bool
// Set 设置缓存值
Set(ctx context.Context, key string, value any, ttl time.Duration) error
// Delete 删除缓存
Delete(ctx context.Context, key string) error
// DeleteByPattern 按模式删除缓存
DeleteByPattern(ctx context.Context, pattern string) error
// Exists 检查缓存是否存在
Exists(ctx context.Context, key string) bool
}
CacheService 缓存服务接口
type KeyBuilder ¶
type KeyBuilder struct {
// contains filtered or unexported fields
}
KeyBuilder 缓存键名构建器 使用场景: 多个小项目共用一台 Redis 服务器,每个项目设置不同前缀
func (*KeyBuilder) Build ¶
func (kb *KeyBuilder) Build(key string) string
Build 构建完整键名 格式: {cacheType}{separator}{prefix}{separator}{key} 示例: kb.Build("user:1") -> "cache_site_a_user:1"
func (*KeyBuilder) BuildCounter ¶
func (kb *KeyBuilder) BuildCounter(key string) string
BuildCounter 构建计数器键名 格式: "counter{separator}{prefix}{separator}{key}"
func (*KeyBuilder) BuildLock ¶
func (kb *KeyBuilder) BuildLock(key string) string
BuildLock 构建分布式锁键名 格式: "lock{separator}{prefix}{separator}{key}"
func (*KeyBuilder) BuildPattern ¶
func (kb *KeyBuilder) BuildPattern(pattern string) string
BuildPattern 构建匹配模式(用于 SCAN/Keys) 示例: kb.BuildPattern("user:*") -> "cache_site_a_user:*"
func (*KeyBuilder) BuildPerm ¶
func (kb *KeyBuilder) BuildPerm(key string) string
BuildPerm 构建永久缓存键名(不带过期时间) 格式: "perm{separator}{prefix}{separator}{key}"
func (*KeyBuilder) BuildSession ¶
func (kb *KeyBuilder) BuildSession(key string) string
BuildSession 构建会话键名 格式: "session{separator}{prefix}{separator}{key}"
func (*KeyBuilder) BuildTemp ¶
func (kb *KeyBuilder) BuildTemp(key string) string
BuildTemp 构建临时缓存键名(带过期时间) 格式: "temp{separator}{prefix}{separator}{key}"
func (*KeyBuilder) SetPrefix ¶
func (kb *KeyBuilder) SetPrefix(prefix string) *KeyBuilder
SetPrefix 动态设置前缀
type KeyBuilderOption ¶
type KeyBuilderOption func(*KeyBuilder)
KeyBuilderOption 配置选项
func WithCacheType ¶
func WithCacheType(cacheType string) KeyBuilderOption
WithCacheType 设置缓存类型标识 示例: WithCacheType("session") -> "session_site_a_user:1"
func WithPrefix ¶
func WithPrefix(prefix string) KeyBuilderOption
WithPrefix 设置前缀(项目/站点别名) 示例: WithPrefix("site_a") -> 所有 key 自动添加 "site_a" 前缀
func WithSeparator ¶
func WithSeparator(separator string) KeyBuilderOption
WithSeparator 设置分隔符 示例: WithSeparator(":") -> "site_a:user:1"