redis

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockAcquisitionTimeout = errors.New("redis: lock acquisition timeout")
	ErrLockReleaseFailed      = errors.New("redis: lock release failed")
)

Functions

func HealthCheck

func HealthCheck(ctx context.Context, client redis.UniversalClient) error

HealthCheck verifies that the Redis client can respond to a PING.

Types

type Client

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

Client wraps the go-redis UniversalClient and adds Astra-specific advanced features.

func NewClient

func NewClient(cfg config.RedisConfig, emitter *event.Emitter) (*Client, error)

NewClient creates a new Astra Redis client with ultra-fast optimizations.

func (*Client) FlushPipeline

func (c *Client) FlushPipeline(ctx context.Context) error

FlushPipeline executes any pending pipeline operations

func (*Client) MGetBatch

func (c *Client) MGetBatch(ctx context.Context, keys ...string) ([]interface{}, error)

MGetBatch ultra-fast multiple get operation

func (*Client) MSetBatch

func (c *Client) MSetBatch(ctx context.Context, pairs ...string) error

MSetBatch ultra-fast multiple set operation

func (*Client) Name

func (c *Client) Name() string

Name returns the service name.

func (*Client) NewLeaderElector

func (c *Client) NewLeaderElector(name string) *LeaderElector

NewLeaderElector creates a new LeaderElector instance.

func (*Client) NewLock

func (c *Client) NewLock(name string, ttl time.Duration) *Lock

NewLock creates a new Lock instance.

func (*Client) NewRateLimiter

func (c *Client) NewRateLimiter() *RateLimiter

NewRateLimiter creates a new RateLimiter instance.

func (*Client) PSubscribe

func (c *Client) PSubscribe(ctx context.Context, pattern string, handler func(channel string, payload []byte) error) (*redis.PubSub, error)

PSubscribe listens for messages on channels matching a pattern.

func (*Client) PipelineBatch

func (c *Client) PipelineBatch(ctx context.Context, cmds ...redis.Cmder) error

PipelineBatch executes commands in a pipeline for ultra-fast performance

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, channel string, message any) error

Publish sends a JSON-serialized message to a Redis channel.

func (*Client) Stop

func (c *Client) Stop(ctx context.Context) error

Stop closes the underlying Redis client.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, channel string, handler func(payload []byte) error) (*redis.PubSub, error)

Subscribe listens for messages on a channel and handles them with a callback.

func (*Client) WithLock

func (c *Client) WithLock(ctx context.Context, name string, ttl time.Duration, timeout time.Duration, fn func(ctx context.Context) error) error

WithLock executes the given function while holding the lock. It automatically retries for the given timeout and handles release.

func (*Client) XAck

func (c *Client) XAck(ctx context.Context, stream, group string, ids ...string) error

XAck acknowledges one or more messages in a consumer group.

func (*Client) XAdd

func (c *Client) XAdd(ctx context.Context, stream string, values map[string]any) (string, error)

XAdd adds a message to a Redis stream.

func (*Client) XGroupCreate

func (c *Client) XGroupCreate(ctx context.Context, stream, group, start string) error

XGroupCreate creates a consumer group for a stream.

func (*Client) XRead

func (c *Client) XRead(ctx context.Context, streams []string, ids []string, count int64, block time.Duration) ([]redis.XStream, error)

XRead reads messages from one or more streams.

func (*Client) XReadGroup

func (c *Client) XReadGroup(ctx context.Context, group, consumer string, streams []string, ids []string, count int64, block time.Duration) ([]redis.XStream, error)

XReadGroup reads messages from a stream using a consumer group.

type LeaderElector

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

LeaderElector provides distributed leader election.

func (*LeaderElector) Elect

func (le *LeaderElector) Elect(ctx context.Context, ttl time.Duration, leaderFunc func(ctx context.Context))

Elect attempts to become the leader and maintain leadership. It calls the leaderFunc when it becomes the leader.

type Lock

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

Lock represents an advanced distributed lock.

func (*Lock) Acquire

func (l *Lock) Acquire(ctx context.Context) (bool, error)

Acquire attempts to acquire the lock.

func (*Lock) Extend

func (l *Lock) Extend(ctx context.Context, additionalTTL time.Duration) (bool, error)

Extend extends the lock's TTL if it's still held by this instance.

func (*Lock) Release

func (l *Lock) Release(ctx context.Context) (bool, error)

Release releases the lock if it's held by this instance.

type Manager

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

Manager handles multiple Redis connections and their lifecycle.

func NewManager

func NewManager(cfg config.RedisConfig, emitter *event.Emitter) *Manager

NewManager creates a new Redis manager with the given initial config for the "default" connection.

func (*Manager) AddConfig

func (m *Manager) AddConfig(name string, cfg config.RedisConfig)

AddConfig adds a named Redis configuration.

func (*Manager) Client

func (m *Manager) Client() (*Client, error)

Client returns the default Redis client.

func (*Manager) Close

func (m *Manager) Close(ctx context.Context) error

Close gracefully closes all active Redis connections.

func (*Manager) Connect

func (m *Manager) Connect(ctx context.Context) error

Connect initializes and pings all configured Redis connections.

func (*Manager) ConnectAndGet

func (m *Manager) ConnectAndGet(ctx context.Context) (redis.UniversalClient, error)

ConnectAndGet is a helper to connect and return the client.

func (*Manager) Connection

func (m *Manager) Connection(name string) (*Client, error)

Connection returns a named Redis connection. It connects if not already started.

func (*Manager) Default

func (m *Manager) Default() (*Client, error)

Default returns the "default" Redis client.

type RateLimiter

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

RateLimiter provides rate limiting functionality.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(ctx context.Context, key string, limit int, window time.Duration) (bool, int, error)

Allow checks if an action should be allowed based on a rate limit. It uses a sliding window algorithm implemented via a Redis Lua script.

type RedisProvider

type RedisProvider struct {
	engine.BaseProvider
	// contains filtered or unexported fields
}

RedisProvider implements engine.Provider for Redis services.

func (*RedisProvider) Boot

func (p *RedisProvider) Boot(a *engine.App) error

Boot connects to Redis, pings, and initializes Redis-backed services.

func (*RedisProvider) Register

func (p *RedisProvider) Register(a *engine.App) error

Register binds the Redis manager to the container without performing network operations.

func (*RedisProvider) Shutdown

func (p *RedisProvider) Shutdown(ctx context.Context, a *engine.App) error

Shutdown gracefully closes all Redis connections.

Jump to

Keyboard shortcuts

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