使用 redigo 库操作 redis
安装
go get github.com/gomodule/redigo/redis
连接
c, err := redis.Dial("tcp",
"172.17.84.205:6379",
redis.DialKeepAlive(1*time.Second),
redis.DialPassword("123456"),
redis.DialConnectTimeout(5*time.Second),
redis.DialReadTimeout(1*time.Second),
redis.DialWriteTimeout(1*time.Second))
if err != nil {fmt.Println("error:", err)
return
}
defer c.Close()
set/get key-value
_, err = c.Do("set", "testkey1", "Hello from redis")
r, err := redis.String(c.Do("get", "testkey1"))
如果要设置过期时间
_, err = c.Do("set", "testkey1", "Hello from redis", "EX", "5")
其中 “EX”, “5” 是设置的过期时间 5 秒
mset/mget 设置获取多个键值
_, err = c.Do("mset", "name", "Michael", "sex", "M", "age", 23, "postcode", 2343253)
stringValues, err := redis.Strings(c.Do("mget", "name", "sex"))
intValues, err := redis.Ints(c.Do("mget", "age", "postcode"))
for _, v := range stringValues {fmt.Println(v)
}
for _, i := range intValues {fmt.Println(i)
}
HSET key field value
将哈希表 key 中的域 field 的值设为 value
_, err = c.Do("hset", "books", "name", "golang", "author", "Moon", "pages", "4000")
if err != nil {fmt.Println("err:", err)
}
v, err := redis.String(c.Do("hget", "books", "name"))
v, err := redis.String(c.Do("hget", "books", "author"))
HMSET/HGETALL,HMGET
在使用前先说明几个函数
- type Args []interface{}
从结构化的值构造命令参数
- func (args Args) Add(value …interface{}) Args
添加值到 Args
- func (args Args) AddFlat(v interface{}) Args
添加 Map, Slice, struct 等的值到 Args
- func ScanStruct(src []interface{}, dest interface{}) error
将 name 和 value 从 src 转变成 dest
示例代码如下
package main
import (
"fmt"
"os"
"time"
redis "github.com/gomodule/redigo/redis"
)
type Book struct {
BookName string
Author string
PageCount string
Press string
}
func main() {
c, err := redis.Dial("tcp",
"172.17.84.205:6379",
redis.DialKeepAlive(1*time.Second),
redis.DialPassword("123456"),
redis.DialConnectTimeout(5*time.Second),
redis.DialReadTimeout(1*time.Second),
redis.DialWriteTimeout(1*time.Second))
errCheck(err)
defer c.Close()
//Struct
top1 := Book{
BookName: "Crazy golang",
Author: "Moon",
PageCount: "600",
Press: "GoodBook",
}
if _, err = c.Do("HMSET", redis.Args{}.Add("Top1").AddFlat(&top1)...); err != nil {fmt.Println(err)
return
}
//Map
top2 := map[string]string{
"BookName": "Mast C++",
"Author": "Diablo",
"PageCount": "2000",
"Press": "BLZ",
}
if _, err = c.Do("HMSET", redis.Args{}.Add("Top2").AddFlat(top2)...); err != nil {fmt.Println(err)
return
}
top3 := []string{"BookName", "Deep learning",
"Author", "Barl",
"PageCount", "2600",
"Publish House", "BLZ"}
_, err = c.Do("HMSET", redis.Args{}.Add("Top3").AddFlat(top3)...)
errCheck(err)
topx := Book{}
for _, item := range []string{"Top1", "Top2", "Top3"} {value, err := redis.Values(c.Do("HGETALL", item))
errCheck(err)
err = redis.ScanStruct(value, &topx)
errCheck(err)
fmt.Printf("%s[%+v]\n", item, topx)
}
stringsValue, err := redis.Strings(c.Do("HMGET", "Top1", "BookName", "Author"))
errCheck(err)
fmt.Printf("hmget:%+v\n", stringsValue)
}
func errCheck(err error) {
if err != nil {fmt.Println(err)
os.Exit(-1)
}
}