Documentation
¶
Index ¶
- Constants
- Variables
- func GetOrSet[T any](ctx context.Context, c *Client, key string, ttl time.Duration, ...) (T, error)
- func GetOrSetJSON[T any](ctx context.Context, c *Client, key string, ttl time.Duration, ...) (T, error)
- type Client
- func (c *Client) Del(ctx context.Context, keys ...string) error
- func (c *Client) Expire(ctx context.Context, key string, expiration time.Duration) error
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) GetDel(ctx context.Context, key string) (string, error)
- func (c *Client) Has(ctx context.Context, key string) bool
- func (c *Client) Keys(ctx context.Context, pattern string) ([]string, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) SAdd(ctx context.Context, key string, members ...any) error
- func (c *Client) SMembers(ctx context.Context, key string) ([]string, error)
- func (c *Client) Set(ctx context.Context, key string, value any, expiration time.Duration) error
- func (c *Client) TryLock(ctx context.Context, key string, ttl time.Duration) (*Lock, error)
- type Config
- type Lock
Constants ¶
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(缓存故障不阻塞业务)。
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Click to show internal directories.
Click to hide internal directories.