安装依赖包
go get -u github.com/go-redis/redis/v9
代码
import (
"context"
"github.com/redis/go-redis/v9"
"log"
"testing"
)
func initSentinel() *redis.Client {
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"192.168.98.138:26380", "192.168.98.138:26381", "192.168.98.138:26382"},
Password: "szz123",
})
ctx := context.Background()
_, err := rdb.Ping(ctx).Result()
if err != nil {
log.Fatalf("Failed to connect to Redis: %v", err)
}
return rdb
}
func TestSet(t *testing.T) {
var client = initSentinel()
ctx := context.Background()
client.Set(ctx, "name", "王五", 0)
rs := client.Get(ctx, "name")
t.Log(rs.Val())
}