redis

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 7 Imported by: 0

README

redis

Redis client helpers.

Usage

client, err := redis.NewClient(redis.Config{
	Addr: "localhost:6379",
})
if err != nil {
	return err
}

Automatic pipelining

NewClient returns the underlying *redis.Client. For workloads with many concurrent, small, independent commands, opt in to go-redis automatic pipelining through that client:

client, err := redis.NewClient(redis.Config{
	Addr: "localhost:6379",
})
if err != nil {
	return err
}
defer client.Close()

commands, err := client.AutoPipeline()
if err != nil {
	return err
}

value, err := commands.Get(ctx, "key").Result()

The default blocking autopipeliner keeps the normal synchronous command shape and an ordered batch stream. It improves throughput when commands from multiple goroutines overlap; sequential commands from one goroutine still wait for each preceding result.

Automatic pipelining is experimental in go-redis v9.22.0 and changes two important command contracts:

  • Once queued, a command runs on the autopipeliner's long-lived context. Use the original client when the command must honor its own deadline or cancellation.
  • A failed batch may be retried as a whole. Use the original client for non-idempotent commands such as INCR or state-changing scripts that must not execute more than once.

Closing client also closes its shared autopipeliner.

TLS

Config.TLSConfig is optional. Leave it nil for local Redis on loopback or another trusted private link.

Set TLSConfig when Redis is reached across an untrusted network or when a managed Redis provider requires TLS:

client, err := redis.NewClient(redis.Config{
	Addr: "redis.example.com:6379",
	TLSConfig: &tls.Config{
		ServerName: "redis.example.com",
	},
})

Notes

  • Addr is required.
  • Ping expects an explicit non-nil context.Context.

Documentation

Overview

Package redis provides Redis client helpers. Package redis 提供 Redis 客户端辅助函数。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(cfg Config) (*redis.Client, error)

NewClient returns a Redis client. Call NewClient(Config{Addr: "host:port"}). NewClient 返回 Redis client。 调用 NewClient(Config{Addr: "host:port"})。

Example / 示例:

client, _ := redis.NewClient(redis.Config{Addr: "localhost:6379"})

func Ping

func Ping(ctx context.Context, client *redis.Client) error

Ping checks Redis connectivity. Ping 检查 Redis 连通性。

Types

type Config

type Config struct {
	Addr         string
	Username     string
	Password     string
	DB           int
	PoolSize     int
	MinIdleConns int
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	TLSConfig    *tls.Config
}

Config configures NewClient. Config 配置 NewClient。

Jump to

Keyboard shortcuts

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