Documentation
¶
Overview ¶
Package redis provides isolated go-redis integration and ownership-token locks.
Index ¶
- Constants
- Variables
- func Classify(e error) error
- type Client
- func (c *Client) Acquire(ctx context.Context, key string, ttl time.Duration) (*Lock, error)
- func (c *Client) CacheAside(ctx context.Context, key string, dst any, ttl time.Duration, ...) error
- func (c *Client) Close() error
- func (c *Client) ConsumeGroup(ctx context.Context, cfg StreamConsumerConfig, ...) error
- func (c *Client) Decrement(ctx context.Context, key string) (int64, error)
- func (c *Client) Delete(ctx context.Context, keys ...string) (int64, error)
- func (c *Client) Eval(ctx context.Context, script string, keys []string, args ...any) (any, error)
- func (c *Client) EvalSHA(ctx context.Context, sha string, keys []string, args ...any) (any, error)
- func (c *Client) Exists(ctx context.Context, keys ...string) (int64, error)
- func (c *Client) Expire(ctx context.Context, key string, ttl time.Duration) (bool, error)
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) GetAndDelete(ctx context.Context, key string) (string, error)
- func (c *Client) GetBytes(ctx context.Context, key string) ([]byte, error)
- func (c *Client) GetJSON(ctx context.Context, key string, dst any) error
- func (c *Client) HDel(ctx context.Context, key string, fields ...string) (int64, error)
- func (c *Client) HExists(ctx context.Context, key, field string) (bool, error)
- func (c *Client) HGet(ctx context.Context, key, field string) (string, error)
- func (c *Client) HGetAll(ctx context.Context, key string) (map[string]string, error)
- func (c *Client) HIncrBy(ctx context.Context, key, field string, n int64) (int64, error)
- func (c *Client) HSet(ctx context.Context, key string, values ...any) (int64, error)
- func (c *Client) Increment(ctx context.Context, key string) (int64, error)
- func (c *Client) IncrementBy(ctx context.Context, key string, n int64) (int64, error)
- func (c *Client) LPop(ctx context.Context, key string) (string, error)
- func (c *Client) LPush(ctx context.Context, key string, values ...any) (int64, error)
- func (c *Client) LRange(ctx context.Context, key string, start, stop int64) ([]string, error)
- func (c *Client) LTrim(ctx context.Context, key string, start, stop int64) error
- func (c *Client) Native() r.UniversalClient
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Pipeline(ctx context.Context, fn func(r.Pipeliner) error) error
- func (c *Client) Publish(ctx context.Context, channel string, value any) (int64, error)
- func (c *Client) RPop(ctx context.Context, key string) (string, error)
- func (c *Client) RPush(ctx context.Context, key string, values ...any) (int64, error)
- func (c *Client) RateLimit(ctx context.Context, key string, limit int64, window time.Duration) (allowed bool, remaining int64, retry time.Duration, err error)
- func (c *Client) SAdd(ctx context.Context, key string, members ...any) (int64, error)
- func (c *Client) SCard(ctx context.Context, key string) (int64, error)
- func (c *Client) SIsMember(ctx context.Context, key string, member any) (bool, error)
- func (c *Client) SMembers(ctx context.Context, key string) ([]string, error)
- func (c *Client) SRem(ctx context.Context, key string, members ...any) (int64, error)
- func (c *Client) ScriptLoad(ctx context.Context, script string) (string, error)
- func (c *Client) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *Client) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *Client) SetNX(ctx context.Context, key string, value any, ttl time.Duration) (bool, error)
- func (c *Client) Stats() *PoolStats
- func (c *Client) Subscribe(ctx context.Context, patterns bool, ...) error
- func (c *Client) TTL(ctx context.Context, key string) (time.Duration, error)
- func (c *Client) Transaction(ctx context.Context, fn func(r.Pipeliner) error) error
- func (c *Client) Watch(ctx context.Context, fn func(*r.Tx) error, keys ...string) error
- func (c *Client) XAck(ctx context.Context, key, group string, ids ...string) (int64, error)
- func (c *Client) XAdd(ctx context.Context, key string, values map[string]any) (string, error)
- func (c *Client) XAutoClaim(ctx context.Context, key, group, consumer, start string, idle time.Duration, ...) ([]StreamMessage, string, error)
- func (c *Client) XClaim(ctx context.Context, key, group, consumer string, idle time.Duration, ...) ([]StreamMessage, error)
- func (c *Client) XDelete(ctx context.Context, key string, ids ...string) (int64, error)
- func (c *Client) XGroupCreate(ctx context.Context, key, group, start string) error
- func (c *Client) XGroupDestroy(ctx context.Context, key, group string) (int64, error)
- func (c *Client) XPending(ctx context.Context, key, group string) (*Pending, error)
- func (c *Client) XRead(ctx context.Context, key, lastID string, count int64, block time.Duration) ([]Stream, error)
- func (c *Client) XReadGroup(ctx context.Context, key, group, consumer, lastID string, count int64, ...) ([]Stream, error)
- func (c *Client) ZAdd(ctx context.Context, key string, members ...ScoredMember) (int64, error)
- func (c *Client) ZCard(ctx context.Context, key string) (int64, error)
- func (c *Client) ZRange(ctx context.Context, key string, start, stop int64) ([]string, error)
- func (c *Client) ZRangeByScore(ctx context.Context, key string, min, max string) ([]string, error)
- func (c *Client) ZRem(ctx context.Context, key string, members ...any) (int64, error)
- func (c *Client) ZScore(ctx context.Context, key, member string) (float64, error)
- type Config
- type IdempotencyStore
- func (s IdempotencyStore) Begin(ctx context.Context, key, fingerprint string, lease time.Duration) (idempotency.Record, error)
- func (s IdempotencyStore) Complete(ctx context.Context, key, token string, status int, ttl time.Duration) error
- func (s IdempotencyStore) Fail(ctx context.Context, key, token string, ttl time.Duration) error
- func (s IdempotencyStore) Renew(ctx context.Context, key, token string, ttl time.Duration) error
- type Limiter
- type Lock
- type Message
- type Mode
- type Pending
- type PoolStats
- type ScoredMember
- type Stream
- type StreamConsumerConfig
- type StreamMessage
Constants ¶
const KeepTTL time.Duration = r.KeepTTL
KeepTTL preserves an existing key lifetime on Set, following Redis KEEPTTL semantics.
Variables ¶
var ErrLockBusy = errors.New("redis: lock busy")
var ErrNotFound = r.Nil
var ErrOwnership = errors.New("redis: lock ownership lost")
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client owns its configured infrastructure client and supports concurrent operations.
func New ¶
New constructs and validates an explicitly owned instance. See package documentation for defaults and cleanup.
func (*Client) Acquire ¶
Acquire attempts to acquire an expiring lock with a cryptographic ownership token.
func (*Client) CacheAside ¶
func (c *Client) CacheAside(ctx context.Context, key string, dst any, ttl time.Duration, load func(context.Context) (any, error)) error
CacheAside may run load concurrently for misses. Use external coordination for expensive duplicate loads.
func (*Client) Close ¶
Close releases owned resources. See package documentation for shutdown ordering.
func (*Client) ConsumeGroup ¶
func (c *Client) ConsumeGroup(ctx context.Context, cfg StreamConsumerConfig, handler func(context.Context, StreamMessage) error) error
ConsumeGroup reads new group messages serially and acknowledges only after successful processing. A handler failure/panic leaves the entry pending; callers choose retry or reclaim policy explicitly.
func (*Client) GetAndDelete ¶
GetAndDelete atomically reads and removes a key.
func (*Client) IncrementBy ¶
IncrementBy atomically adds the requested integer.
func (*Client) Native ¶
func (c *Client) Native() r.UniversalClient
Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.
func (*Client) Pipeline ¶
Pipeline is an advanced native callback. Keys inside the callback are not automatically namespaced.
func (*Client) RateLimit ¶
func (c *Client) RateLimit(ctx context.Context, key string, limit int64, window time.Duration) (allowed bool, remaining int64, retry time.Duration, err error)
RateLimit is an atomic fixed-window counter. A key must describe one policy/window.
func (*Client) ScriptLoad ¶
ScriptLoad loads a script into the Redis script cache.
func (*Client) Subscribe ¶
func (c *Client) Subscribe(ctx context.Context, patterns bool, handler func(context.Context, Message) error, channels ...string) error
Subscribe owns its subscription until ctx cancellation or handler error. It waits for the subscription acknowledgment.
func (*Client) Transaction ¶
Transaction executes an advanced native MULTI/EXEC pipeline callback.
func (*Client) XAutoClaim ¶
func (c *Client) XAutoClaim(ctx context.Context, key, group, consumer, start string, idle time.Duration, count int64) ([]StreamMessage, string, error)
XAutoClaim claims idle pending entries and returns the next scan position.
func (*Client) XClaim ¶
func (c *Client) XClaim(ctx context.Context, key, group, consumer string, idle time.Duration, ids ...string) ([]StreamMessage, error)
XClaim transfers ownership of sufficiently idle pending entries.
func (*Client) XGroupCreate ¶
XGroupCreate creates a stream consumer group, creating the stream if needed.
func (*Client) XGroupDestroy ¶
XGroupDestroy removes a stream consumer group.
func (*Client) XRead ¶
func (c *Client) XRead(ctx context.Context, key, lastID string, count int64, block time.Duration) ([]Stream, error)
XRead reads a stream from the supplied entry ID.
func (*Client) XReadGroup ¶
func (c *Client) XReadGroup(ctx context.Context, key, group, consumer, lastID string, count int64, block time.Duration) ([]Stream, error)
XReadGroup reads stream entries for a consumer group.
func (*Client) ZRangeByScore ¶
ZRangeByScore reads a sorted-set score range.
type Config ¶
type Config struct {
Mode Mode
SentinelUsername, SentinelPassword string
AllowPlaintext bool
Observer instrument.Hook
Addresses []string
MasterName, Username, Password, Prefix string
DB int
// PoolSize is the hard command-connection bound per Redis node. Zero uses
// 10*GOMAXPROCS, matching the native base size. Pipelines share this pool;
// dedicated subscriptions are separately owned and must be closed by callers.
PoolSize int
MinIdleConnections, MaxRetries int
DialTimeout, ReadTimeout, WriteTimeout time.Duration
TLS *tls.Config
Serializer encodingx.Serializer
}
Config contains explicit constructor settings; see package documentation for defaults.
type IdempotencyStore ¶
type IdempotencyStore struct{ Client *Client }
IdempotencyStore stores only fingerprints, ownership tokens and response status, never response bodies.
func (IdempotencyStore) Begin ¶
func (s IdempotencyStore) Begin(ctx context.Context, key, fingerprint string, lease time.Duration) (idempotency.Record, error)
Begin atomically attempts to acquire a fingerprint-bound processing lease.
func (IdempotencyStore) Complete ¶
func (s IdempotencyStore) Complete(ctx context.Context, key, token string, status int, ttl time.Duration) error
Complete records successful processing only for the current ownership token.
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock identifies a time-limited Redis ownership lease, without fencing guarantees.
type Message ¶
type Message struct{ Channel, Pattern, Payload string }
Message contains transport-independent message data.
type Mode ¶
type Mode string
Mode explicitly selects a universal-client topology, including cluster discovery from a single seed.
type StreamConsumerConfig ¶
StreamConsumerConfig names an existing consumer group. Pending-entry reclamation is explicitly managed with XAutoClaim.