您好,登錄后才能下訂單哦!
這篇文章主要介紹了Go操作redis與redigo的方法的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Go操作redis與redigo的方法文章都會有所收獲,下面我們一起來看看吧。
golang操作redis的客戶端包有多個比如redigo、go-redis,github上Star最多的莫屬redigo。
go get github.com/garyburd/redigo/redis import "github.com/garyburd/redigo/redis"
Conn接口是與Redis協作的主要接口,可以使用Dial,DialWithTimeout或者NewConn函數來創建連接,當任務完成時,應用程序必須調用Close函數來完成操作。
package main import ( "github.com/garyburd/redigo/redis" "fmt" ) func main() { conn,err := redis.Dial("tcp","10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() }
package main import ( "github.com/garyburd/redigo/redis" "fmt" ) func main() { conn,err := redis.Dial("tcp","10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() _, err = conn.Do("SET", "name", "wd") if err != nil { fmt.Println("redis set error:", err) } name, err := redis.String(conn.Do("GET", "name")) if err != nil { fmt.Println("redis get error:", err) } else { fmt.Printf("Got name: %s \n", name) } }
_, err = conn.Do("expire", "name", 10) //10秒過期 if err != nil { fmt.Println("set expire error: ", err) return }
_, err = conn.Do("MSET", "name", "wd","age",22) if err != nil { fmt.Println("redis mset error:", err) } res, err := redis.Strings(conn.Do("MGET", "name","age")) if err != nil { fmt.Println("redis get error:", err) } else { res_type := reflect.TypeOf(res) fmt.Printf("res type : %s \n", res_type) fmt.Printf("MGET name: %s \n", res) fmt.Println(len(res)) } //結果: //res type : []string //MGET name: [wd 22] //2
package main import ( "github.com/garyburd/redigo/redis" "fmt" "reflect" ) func main() { conn,err := redis.Dial("tcp","10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() _, err = conn.Do("LPUSH", "list1", "ele1","ele2","ele3") if err != nil { fmt.Println("redis mset error:", err) } res, err := redis.String(conn.Do("LPOP", "list1")) if err != nil { fmt.Println("redis POP error:", err) } else { res_type := reflect.TypeOf(res) fmt.Printf("res type : %s \n", res_type) fmt.Printf("res : %s \n", res) } } //res type : string //res : ele3
package main import ( "github.com/garyburd/redigo/redis" "fmt" "reflect" ) func main() { conn,err := redis.Dial("tcp","10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() _, err = conn.Do("HSET", "student","name", "wd","age",22) if err != nil { fmt.Println("redis mset error:", err) } res, err := redis.Int64(conn.Do("HGET", "student","age")) if err != nil { fmt.Println("redis HGET error:", err) } else { res_type := reflect.TypeOf(res) fmt.Printf("res type : %s \n", res_type) fmt.Printf("res : %d \n", res) } } //res type : int64 //res : 22
管道操作可以理解為并發操作,并通過Send(),Flush(),Receive()三個方法實現。客戶端可以使用send()方法一次性向服務器發送一個或多個命令,命令發送完畢時,使用flush()方法將緩沖區的命令輸入一次性發送到服務器,客戶端再使用Receive()方法依次按照先進先出的順序讀取所有命令操作結果。
Send(commandName string, args ...interface{}) error Flush() error Receive() (reply interface{}, err error)
Send:發送命令至緩沖區
Flush:清空緩沖區,將命令一次性發送至服務器
Recevie:依次讀取服務器響應結果,當讀取的命令未響應時,該操作會阻塞。
package main import ( "github.com/garyburd/redigo/redis" "fmt" ) func main() { conn,err := redis.Dial("tcp","10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() conn.Send("HSET", "student","name", "wd","age","22") conn.Send("HSET", "student","Score","100") conn.Send("HGET", "student","age") conn.Flush() res1, err := conn.Receive() fmt.Printf("Receive res1:%v \n", res1) res2, err := conn.Receive() fmt.Printf("Receive res2:%v\n",res2) res3, err := conn.Receive() fmt.Printf("Receive res3:%s\n",res3) } //Receive res1:0 //Receive res2:0 //Receive res3:22
package main import ( "github.com/garyburd/redigo/redis" "fmt" "time" ) func Subs() { //訂閱者 conn, err := redis.Dial("tcp", "10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :", err) return } defer conn.Close() psc := redis.PubSubConn{conn} psc.Subscribe("channel1") //訂閱channel1頻道 for { switch v := psc.Receive().(type) { case redis.Message: fmt.Printf("%s: message: %s\n", v.Channel, v.Data) case redis.Subscription: fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: fmt.Println(v) return } } } func Push(message string) { //發布者 conn, _ := redis.Dial("tcp", "10.1.210.69:6379") _,err1 := conn.Do("PUBLISH", "channel1", message) if err1 != nil { fmt.Println("pub err: ", err1) return } } func main() { go Subs() go Push("this is wd") time.Sleep(time.Second*3) } //channel1: subscribe 1 //channel1: message: this is wd
MULTI, EXEC,DISCARD和WATCH是構成Redis事務的基礎,當然我們使用go語言對redis進行事務操作的時候本質也是使用這些命令。
MULTI:開啟事務
EXEC:執行事務
DISCARD:取消事務
WATCH:監視事務中的鍵變化,一旦有改變則取消事務。
示例:
package main import ( "github.com/garyburd/redigo/redis" "fmt" ) func main() { conn,err := redis.Dial("tcp","10.1.210.69:6379") if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() conn.Send("MULTI") conn.Send("INCR", "foo") conn.Send("INCR", "bar") r, err := conn.Do("EXEC") fmt.Println(r) } //[1, 1]
conn,err := redis.Dial( "tcp", "10.0.3.100:6379", redis.DialPassword("EfcHGSzKqg6cfzWq"), redis.DialDatabase(8)) if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close()
//寫入 //_, err = conn.Do("LPUSH", "list1", "ele1","ele2","ele3") _, err = conn.Do("reids寫入方法", "key名字", "內容1","內容2","內容3") if err != nil { fmt.Println("redis set error:", err) }
//讀取 redis.Strings:返回多個 redis.String:返回一個 redis.int:返回統計的數字 //獲取集合所有成員 //name, err := redis.Strings(conn.Do("smembers", "beautiful_user")) // 返回集合成員數 //name, err := redis.Int(conn.Do("scard", "beautiful_user")) name, err := redis.方法名(conn.Do("redis讀取方法", "key名字")) if err != nil { fmt.Println("redis get error:", err) } else { fmt.Printf("Got name: %s \n", name) }
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { conn,err := redis.Dial("tcp","10.0.3.100:6379",redis.DialPassword("EfcHGSzKqg6cfzWq"),redis.DialDatabase(8)) if err != nil { fmt.Println("connect redis error :",err) return } defer conn.Close() //寫入 _, err = conn.Do("LPUSH", "list1", "ele1","ele2","ele3") if err != nil { fmt.Println("redis set error:", err) } //讀取 name, err := redis.Strings(conn.Do("smembers", "beautiful_user")) if err != nil { fmt.Println("redis get error:", err) } else { fmt.Printf("Got name: %s \n", name) } }
關于“Go操作redis與redigo的方法”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Go操作redis與redigo的方法”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。