go get github.com/redis/go-redis/v9
Instance ID:Password format. For custom accounts, use the Username@Password format. Password-free authentication instances do not require a password.package mainimport ("context""fmt""time""github.com/redis/go-redis/v9")func main() {ctx := context.Background()// Create a Redis clientrdb := redis.NewClient(&redis.Options{Addr: "192.xx.xx.2:6379", // Instance VIP addressPassword: "crs-xxxx:password", // Default account password format: Instance ID:PasswordDB: 0, // Database numberPoolSize: 200, // Connection pool sizeMinIdleConns: 50, // Minimum number of idle connectionsMaxIdleConns: 200, // Maximum number of idle connectionsDialTimeout: 2 * time.Second, // Connection timeoutReadTimeout: 2 * time.Second, // Read timeoutWriteTimeout: 2 * time.Second, // Write timeoutPoolTimeout: 3 * time.Second, // Connection pool acquisition timeoutConnMaxIdleTime: 30 * time.Minute, // Maximum idle connection lifetime})// Test the connection.pong, err := rdb.Ping(ctx).Result()if err != nil {panic(fmt.Sprintf("Redis connection failed: %v", err))}fmt.Println("Redis connected:", pong)// Write data.err = rdb.Set(ctx, "name", "Distributed Cache", 0).Err()if err != nil {panic(err)}// Read data.val, err := rdb.Get(ctx, "name").Result()if err != nil {panic(err)}fmt.Println("get name:", val)// Set a Key with an expiration time.err = rdb.Set(ctx, "session:user123", "session_data", 3600*time.Second).Err()if err != nil {panic(err)}// Hash operations.err = rdb.HSet(ctx, "user:1001", map[string]interface{}{"name": "Zhang San","age": "28",}).Err()if err != nil {panic(err)}name, err := rdb.HGet(ctx, "user:1001", "name").Result()if err != nil {panic(err)}fmt.Println("user name:", name)// List operations.err = rdb.LPush(ctx, "queue:tasks", "task1", "task2", "task3").Err()if err != nil {panic(err)}task, err := rdb.RPop(ctx, "queue:tasks").Result()if err != nil {panic(err)}fmt.Println("dequeued task:", task)// Pipeline batch operations.pipe := rdb.Pipeline()pipe.Set(ctx, "key1", "value1", 0)pipe.Set(ctx, "key2", "value2", 0)pipe.Set(ctx, "key3", "value3", 0)pipe.Get(ctx, "key1")pipe.Get(ctx, "key2")pipe.Get(ctx, "key3")cmds, err := pipe.Exec(ctx)if err != nil {panic(err)}for _, cmd := range cmds {fmt.Printf("pipeline result: %v\\n", cmd)}// Close the connection.rdb.Close()}
package mainimport ("context""fmt""time""github.com/redis/go-redis/v9")func main() {ctx := context.Background()rdb := redis.NewClient(&redis.Options{Addr: "192.xx.xx.2:6379",Password: "myuser@MyPassword", // Proxy mode custom account: pass the entire "username@password" string as the password.PoolSize: 200,MinIdleConns: 50,DialTimeout: 2 * time.Second,ReadTimeout: 2 * time.Second,WriteTimeout: 2 * time.Second,})pong, err := rdb.Ping(ctx).Result()if err != nil {panic(err)}fmt.Println("Redis connected:", pong)err = rdb.Set(ctx, "key", "value", 0).Err()if err != nil {panic(err)}val, err := rdb.Get(ctx, "key").Result()if err != nil {panic(err)}fmt.Println("get key:", val)rdb.Close()}
package mainimport ("context""crypto/tls""crypto/x509""fmt""os""time""github.com/redis/go-redis/v9")func main() {ctx := context.Background()// Load the CA certificate.caCert, err := os.ReadFile("/path/to/ca.pem")if err != nil {panic(err)}caCertPool := x509.NewCertPool()caCertPool.AppendCertsFromPEM(caCert)// Configure TLS.tlsConfig := &tls.Config{RootCAs: caCertPool,InsecureSkipVerify: false, // Verify the server certificate.}rdb := redis.NewClient(&redis.Options{Addr: "192.xx.xx.2:6379",Password: "crs-xxxx:password",DB: 0,PoolSize: 200,MinIdleConns: 50,DialTimeout: 2 * time.Second,ReadTimeout: 2 * time.Second,WriteTimeout: 2 * time.Second,TLSConfig: tlsConfig,})pong, err := rdb.Ping(ctx).Result()if err != nil {panic(err)}fmt.Println("SSL Redis connected:", pong)err = rdb.Set(ctx, "ssl_test", "encrypted_connection", 0).Err()if err != nil {panic(err)}val, err := rdb.Get(ctx, "ssl_test").Result()if err != nil {panic(err)}fmt.Println("ssl_test:", val)rdb.Close()}
Parameter | Description | Recommended Value |
Addr | Instance connection address. | - |
Password | Authentication password | Default account format: Instance ID:Password |
PoolSize | Connection pool size (the number of connections per node in Cluster mode) | 200 |
MinIdleConns | Minimum number of free connections. | 50 |
DialTimeout | Connection timeout | 2 seconds |
ReadTimeout | Read timeout | 2 seconds |
WriteTimeout | Write timeout | 2 seconds |
PoolTimeout | Connection pool acquisition timeout | 3 seconds |
MaxRedirects | Maximum number of redirects (Cluster mode) | 3 |
PoolSize is set appropriately.PoolTimeout is set appropriately to avoid prolonged blocking.フィードバック