1. 摘要
基于GO的REDIOS調(diào)用框架有開(kāi)源庫(kù)redigo讥珍。本文主要講解redigo的框架和調(diào)用樣例抖苦。
2. 使用實(shí)踐
2.1 前置條件
(1)GO環(huán)境已經(jīng)搭建
(2)REDIS已經(jīng)搭建
沒(méi)有搭建的毁菱,參考《【實(shí)踐】REDIS緩存數(shù)據(jù)庫(kù)從安裝到入門(mén)》。
(3)下載redigo庫(kù)
go get github.com/gomodule/redigo/redis
2.2 測(cè)試實(shí)踐
2.2.1 建立工程
在GO的源目錄下建立rediogoDemo工程锌历,包含main.go文件贮庞。
2.2.2 測(cè)試讀寫(xiě)和有效時(shí)間
func testSetGet() {
c, err := redis.Dial("tcp", "52.82.14.217:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("SET", "name", "duncanwang", "EX", "5")
if err != nil {
fmt.Println("redis set failed:", err)
}
username, err := redis.String(c.Do("GET", "name"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
time.Sleep(8 * time.Second)
username, err = redis.String(c.Do("GET", "name"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get name: %v \n", username)
}
}
2.2.3 批量寫(xiě)入讀取
func testMSetGet() {
c, err := redis.Dial("tcp", "52.82.14.217:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("MSET", "name", "duncanwang","sex","male")
if err != nil {
fmt.Println("redis set failed:", err)
}
is_key_exit, err := redis.Bool(c.Do("EXISTS", "name"))
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Printf("Is name key exists ? : %v \n", is_key_exit)
}
reply, err := redis.Values(c.Do("MGET", "name", "sex"))
if err != nil {
fmt.Printf("patch get name & sex error \n。")
} else {
var name string
var sex string
_, err := redis.Scan(reply, &name, &sex);
if err != nil {
fmt.Printf("Scan error \n究西。")
} else {
fmt.Printf("The name is %v, sex is %v \n", name, sex)
}
}
}
2.2.4 讀寫(xiě)json到redis轉(zhuǎn)換
func testJsonConvert() {
c, err := redis.Dial("tcp", "52.82.14.217:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
key := "profile"
imap := map[string]string{"name": "duncanwang", "sex": "male","mobile":"13671927788"}
value, _ := json.Marshal(imap)
n, err := c.Do("SETNX", key, value)
if err != nil {
fmt.Println(err)
}
if n == int64(1) {
fmt.Println("set Json key success窗慎。")
}
var imapGet map[string]string
valueGet, err := redis.Bytes(c.Do("GET", key))
if err != nil {
fmt.Println(err)
}
errShal := json.Unmarshal(valueGet, &imapGet)
if errShal != nil {
fmt.Println(err)
}
fmt.Println(imapGet["name"])
fmt.Println(imapGet["sex"])
fmt.Println(imapGet["mobile"])
}
2.2.5 主函數(shù)和引用
package main
import (
"fmt"
"time"
"encoding/json"
"github.com/gomodule/redigo/redis"
)
func main() {
/*測(cè)試讀寫(xiě)和有效時(shí)間*/
testSetGet();
/*批量寫(xiě)入讀取*/
testMSetGet();
/*讀寫(xiě)json到redis*/
testJsonConvert();
}
// ... 加入之前的函數(shù)
運(yùn)行結(jié)果如下,達(dá)到了預(yù)期目的。
D:\jusanban\doc\50-編碼實(shí)現(xiàn)\GO\src\rediogoDemo> go build
PS D:\jusanban\doc\50-編碼實(shí)現(xiàn)\GO\src\rediogoDemo> ./rediogoDemo.exe
Get mykey: duncanwang
redis get failed: redigo: nil returned
Is name key exists ? : true
The name is duncanwang, sex is male
duncanwang
male
13671927788
如果需要完整工程源碼可加入輝哥的知識(shí)星球獲取遮斥。
https://t.zsxq.com/EiyNbqB
REDIGO的完整幫助文檔參考:
https://godoc.org/github.com/gomodule/redigo/redis
3. 框架整體介紹
4. 參考
(1)Go實(shí)戰(zhàn)--golang中使用redis(redigo和go-redis/redis)
https://blog.csdn.net/wangshubo1989/article/details/75050024
(2)開(kāi)源庫(kù)redigo和文檔[歐陽(yáng)采用]
github地址:
https://github.com/gomodule/redigo
文檔地址:
https://godoc.org/github.com/gomodule/redigo/redis
go語(yǔ)言使用redis(redigo)
http://www.reibang.com/p/62f0b9ce7584
(3)開(kāi)源庫(kù)go-redis/redis的使用
github地址:
https://github.com/go-redis/redis
文檔地址:
https://godoc.org/github.com/go-redis/redis
(4)Redis 教程
https://www.runoob.com/redis/redis-tutorial.html
(5)redis命令列表
https://redis.io/commands