Scripts2026年7月18日·1 分钟阅读

Go-Redis — Type-Safe Redis Client for Go

The official Go client for Redis, supporting all Redis commands, Sentinel, Cluster, pipelining, pub/sub, and streams with a clean type-safe API.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Go-Redis Overview
安全暂存命令
npx -y tokrepo@latest install 3755b7bf-8242-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Go-Redis is the official Redis client library for Go, maintained under the redis GitHub organization. It provides a comprehensive, type-safe API that covers every Redis command while keeping the developer experience clean and idiomatic to Go.

What Go-Redis Does

  • Connects to standalone Redis, Sentinel, and Cluster deployments with automatic failover
  • Supports pipelining and transactions for batching commands and reducing round trips
  • Implements Pub/Sub, Streams, and scripting with Lua for event-driven architectures
  • Provides connection pooling with configurable size, timeouts, and health checks
  • Offers OpenTelemetry instrumentation for distributed tracing out of the box

Architecture Overview

Go-Redis uses a connection pool managed per client instance. Commands are marshaled into the RESP protocol and sent over TCP. In cluster mode, the client maintains a slot-to-node mapping and routes commands automatically, handling MOVED and ASK redirections transparently. Pipelines batch multiple commands into a single write, and the client decodes responses in order.

Self-Hosting & Configuration

  • Install via go get github.com/redis/go-redis/v9 — no CGO required
  • Configure connection with redis.Options struct: Addr, Password, DB, PoolSize, DialTimeout
  • For Sentinel: use redis.NewFailoverClient with master name and sentinel addresses
  • For Cluster: use redis.NewClusterClient with a list of seed nodes
  • Enable TLS by setting the TLSConfig field in options

Key Features

  • Full Redis command coverage including modules like RedisJSON and RediSearch
  • Type-safe results — each command returns a typed result object, no manual casting
  • Automatic reconnection and retry with configurable backoff
  • Context-aware API with deadline and cancellation support
  • Built-in support for Redis Streams consumer groups

Comparison with Similar Tools

  • Redigo — lower-level API with manual connection management; go-redis offers connection pooling and type safety by default
  • Rueidis — newer client focused on client-side caching and RESP3; go-redis has broader adoption and ecosystem
  • go-redis/cache — a caching layer built on top of go-redis with local cache support
  • Garnet — a Redis-compatible server by Microsoft; go-redis works as the client for it

FAQ

Q: Does go-redis support Redis 7 features? A: Yes, including client-side caching, sharded pub/sub, and functions.

Q: How does connection pooling work? A: Each client maintains a pool of connections. The default pool size is 10 per CPU. Idle connections are closed after 30 minutes.

Q: Can I use go-redis with Redis Cluster? A: Yes, use redis.NewClusterClient. It handles slot routing, redirections, and read replicas automatically.

Q: Is go-redis thread-safe? A: Yes, a single client instance is safe for concurrent use across goroutines.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产