redis

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

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

func Open(ctx context.Context, url string) (*Cache, error)

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

func (c *Cache) Close() error

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.

func (*Cache) Del

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

func (*Cache) Expire

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

func (*Cache) Get

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

func (*Cache) Incr

func (c *Cache) Incr(ctx context.Context, key string, delta int64) (int64, error)

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key, val string, ttl time.Duration) error

func (*Cache) String

func (c *Cache) String() string

String identifies the backend, which is what the deprecated AdapterCache interface reports through storage.LegacyAdapter.

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

func OpenQueue(ctx context.Context, url string, opts QueueOptions) (*Queue, error)

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

func (q *Queue) Close() error

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.

func (*Queue) Publish

func (q *Queue) Publish(ctx context.Context, msg storage.Message) error

func (*Queue) Start

func (q *Queue) Start(ctx context.Context) error

func (*Queue) String

func (q *Queue) String() string

String identifies the backend, which is what the deprecated AdapterQueue interface reports through storage.LegacyQueueAdapter.

func (*Queue) Subscribe

func (q *Queue) Subscribe(topic string, h storage.Handler) error

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.

Jump to

Keyboard shortcuts

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