Documentation
¶
Overview ¶
Package redis implements storage.Cache and storage.Queue on top of Redis.
It lives in its own package so that importing storage or storage/cache does not drag in the Redis client. Note that sdk/config imports it unconditionally in order to build whichever backend the configuration selects, so an application using the SDK links the client whether or not it is configured.
Index ¶
- type Cache
- func (c *Cache) Close() error
- func (c *Cache) Del(ctx context.Context, keys ...string) error
- func (c *Cache) Expire(ctx context.Context, key string, ttl time.Duration) error
- func (c *Cache) Get(ctx context.Context, key string) (string, error)
- func (c *Cache) Incr(ctx context.Context, key string, delta int64) (int64, error)
- func (c *Cache) Set(ctx context.Context, key, val string, ttl time.Duration) error
- func (c *Cache) String() string
- type Queue
- type QueueOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a Redis-backed storage.Cache.
func New ¶
func New(client goredis.UniversalClient) *Cache
New returns a Cache backed by client. Close does not shut client down, because the caller may share it with other components.
func Open ¶
Open connects using a Redis URL, for example redis://user:password@localhost:6379/0. The returned Cache owns the connection and closes it.
func (*Cache) Close ¶
Close marks the cache unusable and, when it owns the client, shuts it down.
The lock is released before the client is closed: draining the connection pool can block, and concurrent callers should get ErrCacheClosed straight away rather than queueing behind the shutdown.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a storage.Queue backed by Redis streams.
Consumer groups are what make it usable from more than one instance: every instance reads under the same group, so each message is handled once, and a delivery whose handler fails stays pending until it is retried here or taken over by another instance. Delivery is therefore at least once; a handler must tolerate seeing the same message twice.
Topic names are expected to be a fixed, finite set. Publish caches the topics it has confirmed a consumer group for, so generating topic names per user or per tenant would grow that cache without bound.
func NewQueue ¶
func NewQueue(client goredis.UniversalClient, opts QueueOptions) *Queue
NewQueue returns a Queue backed by client. Close does not shut client down, because the caller may share it with other components.
func OpenQueue ¶
OpenQueue connects using a Redis URL, for example redis://user:password@localhost:6379/0. The returned Queue owns the connection and closes it.
func (*Queue) Close ¶
Close stops accepting messages, waits for in-flight deliveries and, when it owns the client, shuts it down. Nothing is drained: whatever is unhandled stays pending in Redis for the next instance to pick up, which is the point of using consumer groups.
type QueueOptions ¶
type QueueOptions struct {
// Group is the consumer group. Every instance of one application must use
// the same value; a distinct group receives its own copy of every message.
Group string
// Consumer identifies this instance inside the group and must be unique.
// The default combines the hostname with the process id.
Consumer string
// KeyPrefix is prepended to the topic to form the stream key, so that
// several applications can share one Redis database.
KeyPrefix string
// MaxAttempts bounds redelivery. A message delivered this many times
// without being acknowledged is left in the pending list rather than
// dropped, so that it stays visible to XPENDING.
MaxAttempts int
// Block is how long one read waits for new messages before looping. It also
// bounds how long Start takes to notice a cancellation, because a blocking
// read is not interrupted by one.
Block time.Duration
// ClaimMinIdle is how long a delivery must go unacknowledged before another
// consumer may take it over, and also the interval between retry sweeps.
// Set it above the running time of the slowest handler.
ClaimMinIdle time.Duration
// Batch is how many messages a single read or sweep may return.
Batch int64
}
QueueOptions configures a Queue. The zero value is usable.