Quick Overview
Redigo is a Go client for the Redis database. It offers a simple, efficient, and type-safe API for interacting with Redis, supporting various Redis commands and features. Redigo is widely used in Go applications for Redis integration.
Pros
- Simple and intuitive API
- High performance and efficient connection pooling
- Supports Redis pipelining and transactions
- Extensive documentation and community support
Cons
- Lacks some advanced features like automatic reconnection
- No built-in support for Redis Cluster
- Limited support for newer Redis features compared to some other clients
- Manual management of connections required in some cases
Code Examples
- Basic connection and key-value operations:
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
_, err = conn.Do("SET", "key", "value")
if err != nil {
log.Fatal(err)
}
value, err := redis.String(conn.Do("GET", "key"))
if err != nil {
log.Fatal(err)
}
fmt.Println("Value:", value)
- Using a connection pool:
pool := &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", "localhost:6379") },
}
conn := pool.Get()
defer conn.Close()
_, err := conn.Do("SET", "key", "value")
if err != nil {
log.Fatal(err)
}
- Pipelining commands:
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
conn.Send("SET", "key1", "value1")
conn.Send("SET", "key2", "value2")
conn.Send("GET", "key1")
conn.Send("GET", "key2")
conn.Flush()
for i := 0; i < 4; i++ {
reply, err := conn.Receive()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Reply %d: %v\n", i, reply)
}
Getting Started
To use Redigo in your Go project, follow these steps:
-
Install Redigo:
go get github.com/gomodule/redigo/redis
-
Import the package in your Go code:
import "github.com/gomodule/redigo/redis"
-
Create a connection to Redis:
conn, err := redis.Dial("tcp", "localhost:6379") if err != nil { log.Fatal(err) } defer conn.Close()
-
Start using Redis commands:
_, err = conn.Do("SET", "mykey", "myvalue") if err != nil { log.Fatal(err) }
Competitor Comparisons
Redis Go client
Pros of go-redis
- More feature-rich, supporting advanced Redis features like Streams and Pub/Sub
- Better support for Redis Cluster and Sentinel
- Active development with frequent updates and improvements
Cons of go-redis
- Slightly more complex API, which may have a steeper learning curve
- Higher memory usage due to additional features and abstractions
Code Comparison
redigo:
conn, err := redis.Dial("tcp", "localhost:6379")
defer conn.Close()
_, err = conn.Do("SET", "key", "value")
value, err := redis.String(conn.Do("GET", "key"))
go-redis:
client := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
defer client.Close()
err := client.Set("key", "value", 0).Err()
value, err := client.Get("key").Result()
Both libraries offer similar basic functionality, but go-redis provides a more idiomatic Go API with method chaining and built-in result parsing. redigo requires manual connection management and explicit type conversion for results.
While redigo is simpler and more lightweight, go-redis offers a richer feature set and better support for complex Redis deployments. The choice between them depends on your specific requirements and preferences.
Redis Go client
Pros of go-redis
- More feature-rich, supporting advanced Redis features like Streams and Pub/Sub
- Better support for Redis Cluster and Sentinel
- Active development with frequent updates and improvements
Cons of go-redis
- Slightly more complex API, which may have a steeper learning curve
- Higher memory usage due to additional features and abstractions
Code Comparison
redigo:
conn, err := redis.Dial("tcp", "localhost:6379")
defer conn.Close()
_, err = conn.Do("SET", "key", "value")
value, err := redis.String(conn.Do("GET", "key"))
go-redis:
client := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
defer client.Close()
err := client.Set("key", "value", 0).Err()
value, err := client.Get("key").Result()
Both libraries offer similar basic functionality, but go-redis provides a more idiomatic Go API with method chaining and built-in result parsing. redigo requires manual connection management and explicit type conversion for results.
While redigo is simpler and more lightweight, go-redis offers a richer feature set and better support for complex Redis deployments. The choice between them depends on your specific requirements and preferences.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Redigo
Redigo is a Go client for the Redis / Valkey database.
Features
- A Print-like API with support for all Redis commands.
- Pipelining, including pipelined transactions.
- Publish/Subscribe.
- Connection pooling.
- Script helper type with optimistic use of EVALSHA.
- Helper functions for working with command replies.
Documentation
Installation
Install Redigo using the "go get" command:
go get github.com/gomodule/redigo/redis
The Go distribution is Redigo's only dependency.
Related Projects
- rafaeljusto/redigomock - A mock library for Redigo.
- chasex/redis-go-cluster - A Redis cluster client implementation.
- FZambia/sentinel - Redis Sentinel support for Redigo
- mna/redisc - Redis Cluster client built on top of Redigo
- Alibaba/opentelemetry-go-auto-instrumentation - Get trace and metrics in OpenTelemetry format generated by the Redigo framework without changing any code
Contributing
See CONTRIBUTING.md.
License
Redigo is available under the Apache License, Version 2.0.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot