您现在的位置是:网站首页> Go语言

go-redis cluster 使用demo

摘要

package main 

import (

    "fmt"

    "time"

    "github.com/go-redis/redis"

func main() {

    client := redis.NewClusterClient(&redis.ClusterOptions{

        Addrs:    []string{"redis-cluster.test.com:port"}, //set redis cluster url

        Password: "mypassword",                    //set password

    })     

    pong, err := client.Ping().Result()

    fmt.Println(pong, err)

    fmt.Println("pool state init state:", client.PoolStats())

    for i := 0; i < 1000; i++ {

        k := fmt.Sprintf("key:%d", i)

        v := k

        val, err := client.Set(k, v, 60*time.Second).Result()

        if err != nil {

            panic(err)

        } 

        val, err = client.Get(k).Result()

        if err != nil {

            panic(err)

        }

        fmt.Println("key:", val)

    }

    fmt.Println("pool state final state:", client.PoolStats()) //获取客户端连接池相关信息 

}


Top