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()) //獲取客戶端鏈接池相關信息

}

 總結:go-redis的鏈接池無需單獨管理,在初始化時會自動根據cpu個數分配,本demo中跑了個循環,因此設置的key會分別打到不一樣的node上去,鏈接池會自動增長node

相關文章
相關標籤/搜索