redis

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package redis provides a client for interacting with Redis key-value stores.This package allows creating and managing Redis clients, executing Redis commands, and handling connections to Redis databases.

Package redis is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPubSub

func NewPubSub(conf config.Config, logger datasource.Logger, metrics Metrics) pubsub.Client

NewPubSub creates a new PubSub client that implements pubsub.Client interface. This allows Redis PubSub to be initialized directly without type assertion, aligning with the pattern used by Kafka, MQTT, and Google PubSub implementations.

Types

type Config

type Config struct {
	HostName string
	Username string
	Password string
	Port     int
	DB       int
	Options  *redis.Options
	TLS      *tls.Config

	// PubSub configuration
	PubSubMode          string // "pubsub" or "streams"
	PubSubStreamsConfig *StreamsConfig
	PubSubBufferSize    int           // Message buffer size for channels (default: 100)
	PubSubQueryTimeout  time.Duration // Default query timeout (default: 5s)
	PubSubQueryLimit    int           // Default query message limit (default: 10)
}

type Metrics

type Metrics interface {
	RecordHistogram(ctx context.Context, name string, value float64, labels ...string)
	IncrementCounter(ctx context.Context, name string, labels ...string)
}

type MockMetrics

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

MockMetrics is a mock of Metrics interface.

func NewMockMetrics

func NewMockMetrics(ctrl *gomock.Controller) *MockMetrics

NewMockMetrics creates a new mock instance.

func (*MockMetrics) EXPECT

func (m *MockMetrics) EXPECT() *MockMetricsMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockMetrics) IncrementCounter

func (m *MockMetrics) IncrementCounter(ctx context.Context, name string, labels ...string)

IncrementCounter mocks base method.

func (*MockMetrics) RecordHistogram

func (m *MockMetrics) RecordHistogram(ctx context.Context, name string, value float64, labels ...string)

RecordHistogram mocks base method.

type MockMetricsMockRecorder

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

MockMetricsMockRecorder is the mock recorder for MockMetrics.

func (*MockMetricsMockRecorder) IncrementCounter

func (mr *MockMetricsMockRecorder) IncrementCounter(ctx, name any, labels ...any) *gomock.Call

IncrementCounter indicates an expected call of IncrementCounter.

func (*MockMetricsMockRecorder) RecordHistogram

func (mr *MockMetricsMockRecorder) RecordHistogram(ctx, name, value any, labels ...any) *gomock.Call

RecordHistogram indicates an expected call of RecordHistogram.

type PubSub

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

PubSub handles Redis PubSub operations.

func (*PubSub) Close

func (ps *PubSub) Close() error

Close closes all active subscriptions and cleans up resources.

func (*PubSub) CreateTopic

func (ps *PubSub) CreateTopic(ctx context.Context, name string) error

CreateTopic is a no-op for Redis PubSub (channels are created on first publish/subscribe). For Redis Streams, it creates the stream and consumer group.

func (*PubSub) DeleteTopic

func (ps *PubSub) DeleteTopic(ctx context.Context, topic string) error

DeleteTopic unsubscribes all active subscriptions for the given topic/channel.

func (*PubSub) Health

func (ps *PubSub) Health() datasource.Health

Health returns the health status of the Redis PubSub connection.

func (*PubSub) Publish

func (ps *PubSub) Publish(ctx context.Context, topic string, message []byte) error

Publish publishes a message to a Redis channel or stream.

func (*PubSub) Query

func (ps *PubSub) Query(ctx context.Context, query string, args ...any) ([]byte, error)

Query retrieves messages from a Redis channel or stream.

func (*PubSub) Subscribe

func (ps *PubSub) Subscribe(ctx context.Context, topic string) (*pubsub.Message, error)

Subscribe subscribes to a Redis channel or stream and returns a single message.

type QueryLog

type QueryLog struct {
	Query    string `json:"query"`
	Duration int64  `json:"duration"`
	Args     any    `json:"args,omitempty"`
}

QueryLog represents a logged Redis query.

func (*QueryLog) PrettyPrint

func (ql *QueryLog) PrettyPrint(writer io.Writer)

func (*QueryLog) String

func (ql *QueryLog) String() string

type Redis

type Redis struct {
	*redis.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(c config.Config, logger datasource.Logger, metrics Metrics) *Redis

NewClient returns a Redis client if connection is successful based on Config. Supports both plain and TLS connections. TLS is configured via REDIS_TLS_ENABLED and related environment variables. In case of error, it returns an error as second parameter.

func (*Redis) Close

func (r *Redis) Close() error

Close shuts down the Redis client, ensuring the current dataset is saved before exiting.

func (*Redis) HealthCheck

func (r *Redis) HealthCheck() datasource.Health

HealthCheck returns the health status of the Redis connection.

type StreamsConfig

type StreamsConfig struct {
	// ConsumerGroup is the name of the consumer group (required for Streams)
	ConsumerGroup string

	// ConsumerName is the name of the consumer (optional, auto-generated if empty)
	ConsumerName string

	// MaxLen is the maximum length of the stream (optional)
	// If > 0, the stream will be trimmed to this length on publish
	MaxLen int64

	// Block is the blocking duration for XREADGROUP (optional)
	// If > 0, calls will block for this duration waiting for new messages
	Block time.Duration

	// PELRatio is the ratio of PEL (pending) messages to read (0.0-1.0)
	// Default: 0.7 (70% PEL, 30% new messages)
	// 0.0 = only new messages, 1.0 = only PEL messages
	PELRatio float64
}

StreamsConfig holds configuration for Redis Streams.

Jump to

Keyboard shortcuts

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