Golang 官方推薦使用的 Redis 客戶端 Redigo
01介紹
在之前的文章介紹過 Golang 操作 Redis 的三方庫 go-redis,本文主要介紹另外一個 Golang 操作 Redis 的三方庫 redigo,它是 Golang 官方推薦使用的 Redis 客戶端。
go-redis 三方庫為我們封裝了很多函數(shù)來執(zhí)行 Redis 命令,而 redigo 三方庫只有一個 Do 函數(shù)執(zhí)行 Redis 命令,更接近使用 redis-cli 操作 Redis,這一點是我個人比較喜歡的,只要熟悉 Redis,就可以輕松使用 redigo 作為客戶端操作 Redis,而不需要再去記三方庫封裝的函數(shù)。
使用 go get 命令安裝 redigo:
- go get github.com/gomodule/redigo/redis
redigo 庫中的 Conn 接口是操作 Redis 的主要接口。
- type Conn interface {
- // Close closes the connection.
- Close() error
- // Err returns a non-nil value when the connection is not usable.
- Err() error
- // Do sends a command to the server and returns the received reply.
- Do(commandName string, args ...interface{}) (reply interface{}, err error)
- // Send writes the command to the client's output buffer.
- Send(commandName string, args ...interface{}) error
- // Flush flushes the output buffer to the Redis server.
- Flush() error
- // Receive receives a single reply from the Redis server
- Receive() (reply interface{}, err error)
- }
閱讀 redigo 庫的源碼,我們可以發(fā)現(xiàn) Conn 接口包含 6 個方法,本文我們主要使用 Do 和 Close。
02創(chuàng)建連接
redigo 庫提供了多個函數(shù)創(chuàng)建連接,本文我們使用 Dial 函數(shù)創(chuàng)建連接,此外,還可以使用 DialURL 函數(shù)和 NewConn 函數(shù)創(chuàng)建連接,限于篇幅,本文不準備逐一介紹。
示例代碼:
- // 連接 redis-server
- // 創(chuàng)建連接
- c, err := redis.Dial("tcp", ":6379")
- if err != nil {
- fmt.Printf("redis.Dial() error:%v", err)
- return
- }
- // 關閉連接
- defer c.Close()
閱讀上面這段代碼,我們使用 Dial 函數(shù)創(chuàng)建連接,需要注意的是,我們不要忘記關閉連接。
使用該方式主要是為了讀者朋友們容易理解,建議在生產環(huán)境中使用連接池,避免每次執(zhí)行命令都需要先創(chuàng)建連接,影響性能。
03string 操作
redigo 執(zhí)行 Redis 命令的通用方法是使用 Conn 接口的 Do 函數(shù),Do 函數(shù)可以發(fā)送命令給 Redis 服務器,并返回 Redis 服務器的回復。
- Do(commandName string, args ...interface{}) (reply interface{}, err error)
示例代碼:
- func stringSet(conn redis.Conn) {
- replySet, err := conn.Do("SET", "key1", "value1")
- if err != nil {
- fmt.Println("SET error: ", err)
- }
- fmt.Println(replySet)
- }
閱讀上面這段代碼,我們使用 Do 函數(shù)執(zhí)行 Redis 的 set 命令,限于篇幅,更多關于 string 類型的 redis 命令示例代碼,請閱讀推送到 GitHub 的本文相關代碼。
04復合類型操作
除了操作字符串,我們還會操作復合類型,redigo 庫中的 Args 類型提供了操作復合類型的方法 AddFlat。
- func (args Args) AddFlat(v interface{}) Args
寫操作:
- type User struct {
- ID int64 `redis:"id"`
- Name string `redis:"name"`
- }
- // struct
- func structAdd(conn redis.Conn) {
- u1 := User{
- ID: 1,
- Name: "name1",
- }
- replyStruct, err := conn.Do("HMSET", redis.Args{}.Add("hkey1").AddFlat(&u1)...)
- if err != nil {
- fmt.Println("struct err: ", err)
- }
- fmt.Println(replyStruct)
- }
閱讀上面這段代碼,我們使用 AddFlat 方法將結構體寫入 Redis,需要注意的是結構體字段是可導出的字段名稱,并且使用了字段標簽 redis。
讀操作:
- func structValues(conn redis.Conn) {
- v, err := redis.Values(conn.Do("HGETALL", "hkey1"))
- if err != nil {
- fmt.Println("redis.Values() err: ", err)
- }
- // redis.ScanStruct()
- u2 := new(User)
- if err := redis.ScanStruct(v, u2); err != nil {
- fmt.Println("redis.ScanStruct() err: ", err)
- }
- fmt.Printf("%+v\n", u2)
- }
閱讀上面這段代碼,我們使用兩個助手函數(shù) Values 和 ScanStruct,將 Redis 服務器的回復解析到 struct,redigo 還提供了助手函數(shù) ScanSlice 將 Redis 服務器的回復解析到 slice。
限于篇幅,關于 map 和 slice 的示例代碼,請閱讀推送到 GitHub 上的本文相關代碼。
05總結
本文我們介紹了 Golang 官方推薦的操作 Redis 的三方庫 redigo,它僅需使用 Do 函數(shù)執(zhí)行 Redis 所有命令,它還提供了很多助手函數(shù)幫助我們更加方便操作 Redis。
本文轉載自微信公眾號「Golang語言開發(fā)棧」,可以通過以下二維碼關注。轉載本文請聯(lián)系Golang語言開發(fā)棧公眾號。