基于golang的redis使用
下載package
go get github.com/garyburd/redigo/redis redis 在使用中、會涉及到set、Mset、expire、lpush、Hset等
代碼篇:package mainimport (
"fmt"
"github.com/garyburd/redigo/redis"
"log")func main() {
c,err :=redis.Dial("tcp","localhost:6379")
if err != nil {
log.Fatal("conn redis failed,",err)
}
fmt.Println("redis conn success")
defer c.Close()
// set
c.Do("Set","abc",100)
r,_ := redis.Int(c.Do(" ","abc"))
fmt.Println(r)
// Mset
c.Do("Mset","abd",100,"efg",300)
rs,_ :=redis.Ints(c.Do("MGet","abd","efg"))
for _,v := range rs {
fmt.Println(v)
}
// 1. 設置過期時間 expire
_,err = c.Do("expire","abcd",10)
if err != nil {
fmt.Println(err)
return
}
// List隊列操作
c.Do("lpush","book_list","abc","abc","ceg",300)
rpush,rpusherr := redis.String(c.Do("lpop","book_list"))
if rpusherr != nil {
fmt.Println("get abc failed,", rpusherr)
return
}
fmt.Println(rpush)
// 1. Hash表
c.Do("Hset","books","abc",100)
rHash,rHasherr := redis.Int(c.Do("HGet","books","abc"))
if rHasherr != nil {
fmt.Println("get abc failed,", err)
return
}
fmt.Println(rHash)
}
|