redis

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDialTimeout  = 5 * time.Second
	DefaultReadTimeout  = 3 * time.Second
	DefaultWriteTimeout = 3 * time.Second
)

Variables

View Source
var (
	// ErrLockNotAcquired 表示锁已被其他持有者占用。
	ErrLockNotAcquired = errors.New("redis: lock not acquired")
	// ErrLockNotHeld 表示当前实例不持有该锁(已过期或 token 不匹配)。
	ErrLockNotHeld = errors.New("redis: lock not held")
)

Functions

func GetOrSet

func GetOrSet[T any](
	ctx context.Context,
	c *Client,
	key string,
	ttl time.Duration,
	loader func(ctx context.Context) (T, error),
	marshal func(T) (string, error),
	unmarshal func(string) (T, error),
) (T, error)

GetOrSet 实现 Cache-aside 模式。 查缓存 → 未命中调用 loader → 写回缓存。 Redis 读取失败时降级为直接调用 loader(缓存故障不阻塞业务)。

func GetOrSetJSON

func GetOrSetJSON[T any](
	ctx context.Context,
	c *Client,
	key string,
	ttl time.Duration,
	loader func(ctx context.Context) (T, error),
) (T, error)

GetOrSetJSON 是 GetOrSet 的 JSON 便捷版本。 内置 JSON 序列化/反序列化,适用于最常见的缓存场景。

Types

type Client

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

func NewClient

func NewClient(cfg *Config, l *slog.Logger) (*Client, func(), error)

func (*Client) Del

func (c *Client) Del(ctx context.Context, keys ...string) error

Del 删除键

func (*Client) Expire

func (c *Client) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置键过期时间

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (string, error)

Get 获取值

func (*Client) GetDel

func (c *Client) GetDel(ctx context.Context, key string) (string, error)

GetDel atomically gets the value and deletes the key (Redis >= 6.2).

func (*Client) Has

func (c *Client) Has(ctx context.Context, key string) bool

Has 判断键是否存在

func (*Client) Keys

func (c *Client) Keys(ctx context.Context, pattern string) ([]string, error)

Keys 按模式查找键

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping 测试连接

func (*Client) SAdd

func (c *Client) SAdd(ctx context.Context, key string, members ...any) error

SAdd 向集合添加成员

func (*Client) SMembers

func (c *Client) SMembers(ctx context.Context, key string) ([]string, error)

SMembers 获取集合所有成员

func (*Client) Set

func (c *Client) Set(ctx context.Context, key string, value any, expiration time.Duration) error

Set 存储键值对

func (*Client) TryLock

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

TryLock 尝试获取分布式锁。 基于 Redis SET NX 实现,使用随机 token 标识持有者。 如果锁已被占用,返回 ErrLockNotAcquired。

type Config

type Config struct {
	Addr         string
	Username     string
	Password     string
	DB           int
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

func NewConfigFromProto

func NewConfigFromProto(cfg *redispb.Redis) *Config

type Lock

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

Lock 表示一个已获取的分布式锁。

func (*Lock) Unlock

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

Unlock 释放分布式锁。 使用 Lua 脚本原子性地验证 token 并删除 key,防止误释放他人持有的锁。 如果锁已过期或 token 不匹配,返回 ErrLockNotHeld。

Jump to

Keyboard shortcuts

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