云数据库 GEMINIDB-GeminiDB Redis客户端重试指南:Go-redis

时间:2024-10-29 15:29:47

Go-redis

package main
 
import (
       "context"
       "fmt"
       "time"
 
       "github.com/redis/go-redis/v9"
)
 
var ctx = context.Background()
 
func main() {
 
    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
        MaxRetries: 3, // set max retry times
        MinRetryBackoff: time.Duration(1) * time.Second, // set retry interval
        MaxRetryBackoff: time.Duration(2) * time.Second, // set retry interval
    })
 
    // Execute command
    err := client.Set(ctx, "key", "value", 0).Err()
    if err != nil {
        panic(err)
    }
 
    // Test
    pong, err := client.Ping(ctx).Result()
    if err != nil {
        fmt.Println("Failed:", err)
        return
    }
    fmt.Println("Success:", pong)
}
support.huaweicloud.com/redisug-nosql/redis_bestpractice_2_008.html