import "github.com/go-redis/redis"
func GetRedisClient() *Client { redisdb := NewClient(&Options{ Addr: "127.0.0.1:6379", Password: "", // no password set DB: 0, // use default DB }) pong, err := redisdb.Ping().Result() if err != nil { fmt.Println(pong, err) } return redisdb }
經過 cient.Ping() 來檢查是否成功鏈接到了 redis 服務器html
func StringDemo() { fmt.Println("-----------------------welcome to StringDemo-----------------------") redisClient:=GetRedisClient() if redisClient ==nil{ fmt.Errorf("StringDemo redisClient is nil") return } name := "張三" key :="name:zhangsan" redisClient.Set(key , name,1 * time.Second) val := redisClient.Get(key) if val == nil { fmt.Errorf("StringDemo get error") } fmt.Println("name", val) }
func ListDemo(){ fmt.Println("-----------------------welcome to ListDemo-----------------------") redisClient:=GetRedisClient() if redisClient == nil { fmt.Errorf("ListDemo redisClient is nil") return } articleKey := "article" result,err:=redisClient.RPush(articleKey, "a","b","c").Result() // if err!=nil { fmt.Println(err) return } fmt.Println("result:",result) result,err = redisClient.LPush(articleKey, "d").Result() // if err!=nil { fmt.Println(err) return } fmt.Println("result:",result) length, err := redisClient.LLen(articleKey).Result() if err != nil { fmt.Println("ListDemo LLen is nil") } fmt.Println("length: ", length) // 長度 mapOut,err1:=redisClient.LRange(articleKey,0,100).Result() if err1!=nil { fmt.Println(err1) return } for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } }
func HashDemo() { fmt.Println("-----------------------welcome to HashDemo-----------------------") redisClient := GetRedisClient() if redisClient == nil { fmt.Errorf("HashDemo redisClient is nil") return } article := Article{18, "測試文章內容22222", "測試文章內容22222測試文章內容22222測試文章內容22222", 10, 0} articleKey := "article:18" redisClient.HMSet(articleKey, ToStringDictionary(&article)) mapOut := redisClient.HGetAll(articleKey).Val() for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } fmt.Print("\n") redisClient.HSet(articleKey, "Content", "測試文章內容") mapOut = redisClient.HGetAll(articleKey).Val() for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } fmt.Print("\n") view, err := redisClient.HIncrBy(articleKey, "Views", 1).Result() if err != nil { fmt.Printf("\n HIncrBy error=%s ", err) } else { fmt.Printf("\n HIncrBy Views=%d ", view) } fmt.Print("\n") mapOut = redisClient.HGetAll(articleKey).Val() for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } fmt.Print("\n") }
func GetRedisClientPool() *Client{ redisdb := NewClient(&Options{ Addr: "127.0.0.1:6379", Password: "", DB: 0, PoolSize: 5,}) pong, err := redisdb.Ping().Result() if err != nil { fmt.Println(pong, err) } return redisdb }
// 鏈接池測試 func connectPoolTest() { fmt.Println("-----------------------welcome to connect Pool Test-----------------------") client :=GetRedisClientPool() wg := sync.WaitGroup{} wg.Add(10) for i := 0; i < 10; i++ { go func() { defer wg.Done() for j := 0; j < 1000; j++ { client.Set(fmt.Sprintf("name%d", j), fmt.Sprintf("xys%d", j), 0).Err() client.Get(fmt.Sprintf("name%d", j)).Result() } fmt.Printf("PoolStats, TotalConns: %d, IdleConns: %d\n", client.PoolStats().TotalConns, client.PoolStats().IdleConns); }() } wg.Wait() }
package main import ( "fmt" . "github.com/go-redis/redis" . "redisDemo/models" "time" "sync" ) func main() { fmt.Println("-----------------------welcome to redisdemo-----------------------") //StringDemo() //ListDemo() //HashDemo() connectPoolTest() } func StringDemo() { fmt.Println("-----------------------welcome to StringDemo-----------------------") redisClient:=GetRedisClient() if redisClient ==nil{ fmt.Errorf("StringDemo redisClient is nil") return } name := "張三" key :="name:zhangsan" redisClient.Set(key , name,1 * time.Second) val := redisClient.Get(key) if val == nil { fmt.Errorf("StringDemo get error") } fmt.Println("name", val) } func ListDemo(){ fmt.Println("-----------------------welcome to ListDemo-----------------------") redisClient:=GetRedisClient() if redisClient == nil { fmt.Errorf("ListDemo redisClient is nil") return } articleKey := "article" result,err:=redisClient.RPush(articleKey, "a","b","c").Result() //在名稱爲 key 的list尾添加一個值爲value的元素 if err!=nil { fmt.Println(err) return } fmt.Println("result:",result) result,err = redisClient.LPush(articleKey, "d").Result() //在名稱爲 key 的list頭添加一個值爲value的元素 if err!=nil { fmt.Println(err) return } fmt.Println("result:",result) length, err := redisClient.LLen(articleKey).Result() if err != nil { fmt.Println("ListDemo LLen is nil") } fmt.Println("length: ", length) // 長度 mapOut,err1:=redisClient.LRange(articleKey,0,100).Result() if err1!=nil { fmt.Println(err1) return } for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } } func HashDemo() { fmt.Println("-----------------------welcome to HashDemo-----------------------") redisClient := GetRedisClient() if redisClient == nil { fmt.Errorf("HashDemo redisClient is nil") return } article := Article{18, "測試文章內容22222", "測試文章內容22222測試文章內容22222測試文章內容22222", 10, 0} articleKey := "article:18" redisClient.HMSet(articleKey, ToStringDictionary(&article)) mapOut := redisClient.HGetAll(articleKey).Val() for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } fmt.Print("\n") redisClient.HSet(articleKey, "Content", "測試文章內容") mapOut = redisClient.HGetAll(articleKey).Val() for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } fmt.Print("\n") view, err := redisClient.HIncrBy(articleKey, "Views", 1).Result() if err != nil { fmt.Printf("\n HIncrBy error=%s ", err) } else { fmt.Printf("\n HIncrBy Views=%d ", view) } fmt.Print("\n") mapOut = redisClient.HGetAll(articleKey).Val() for inx, item := range mapOut { fmt.Printf("\n %s:%s", inx, item) } fmt.Print("\n") } func GetRedisClient() *Client { redisdb := NewClient(&Options{ Addr: "127.0.0.1:6379", Password: "", // no password set DB: 0, // use default DB }) pong, err := redisdb.Ping().Result() if err != nil { fmt.Println(pong, err) } return redisdb } func GetRedisClientPool() *Client{ redisdb := NewClient(&Options{ Addr: "127.0.0.1:6379", Password: "", DB: 0, PoolSize: 5,}) pong, err := redisdb.Ping().Result() if err != nil { fmt.Println(pong, err) } return redisdb } // 鏈接池測試 func connectPoolTest() { fmt.Println("-----------------------welcome to connect Pool Test-----------------------") client :=GetRedisClientPool() wg := sync.WaitGroup{} wg.Add(10) for i := 0; i < 10; i++ { go func() { defer wg.Done() for j := 0; j < 1000; j++ { client.Set(fmt.Sprintf("name%d", j), fmt.Sprintf("xys%d", j), 0).Err() client.Get(fmt.Sprintf("name%d", j)).Result() } fmt.Printf("PoolStats, TotalConns: %d, IdleConns: %d\n", client.PoolStats().TotalConns, client.PoolStats().IdleConns); }() } wg.Wait() }
1. go語言使用Redis 仍是很是簡單的,以上已經把Redis 的基本的用法講完了。你們能夠本身動手寫代碼試試。git
2. 完整代碼:點擊下載github