redis

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 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
}

NewClient enables ContextTimeoutEnabled: network I/O observes the earlier of the context deadline and the configured socket timeout. The caller owns the client and closes it with Close.

Session storage

auth/store/redissession.Options.ReadTimeout and WriteTimeout bound whole store operations, including calls that execute multiple Redis commands. Client socket timeouts apply to individual network reads and writes.

Clients created directly with github.com/redis/go-redis/v9 (aliased as goredis below) must enable context deadlines before being passed to the session store:

client := goredis.NewClient(&goredis.Options{
	Addr:                  "localhost:6379",
	ContextTimeoutEnabled: true,
})
defer client.Close()

store := redissession.New(client, redissession.Options{})

ClearAllSessions deletes session records and leaves stale index entries for Sessions to prune or the index TTL to expire. Concurrent logins may survive the scan and remain listed; cleanup does not provide an atomic cutoff. Until pruned, stale entries consume index space and add work to session listing.

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. Network I/O observes context deadlines as well as the configured socket timeouts. Call NewClient(Config{Addr: "host:port"}). NewClient 返回 Redis client。 网络 I/O 同时遵守 context deadline 和配置的 socket 超时。 调用 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