2021-05-12 09:03:38 +00:00
|
|
|
package main
|
|
|
|
|
|
2021-05-12 09:14:35 +00:00
|
|
|
import (
|
2021-05-15 05:33:50 +00:00
|
|
|
"encoding/json"
|
2021-05-12 09:14:35 +00:00
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
|
|
|
|
"myschools.me/suguo/tutorial-redis/conf"
|
|
|
|
|
)
|
2021-05-12 09:03:38 +00:00
|
|
|
|
|
|
|
|
func main() {
|
2021-05-12 09:14:35 +00:00
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: conf.Host,
|
|
|
|
|
Password: conf.Password,
|
|
|
|
|
DB: conf.Database,
|
|
|
|
|
})
|
|
|
|
|
//延迟到程序结束关闭链接
|
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
|
|
//ping
|
|
|
|
|
pong, err := client.Ping().Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("ping error", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("ping result:", pong)
|
|
|
|
|
|
|
|
|
|
//string------------------------------------------------------------------------
|
|
|
|
|
key := "go2key"
|
|
|
|
|
//过期时间1小时
|
|
|
|
|
err = client.Set(key, "我是值", time.Hour).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("set err", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取
|
|
|
|
|
value, err := client.Get(key).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Get err", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("key:%v 值:%v \n", key, value)
|
2021-05-15 05:33:50 +00:00
|
|
|
|
|
|
|
|
//json---------------------------------
|
|
|
|
|
//存储结构
|
|
|
|
|
doctor := struct {
|
|
|
|
|
Id int
|
|
|
|
|
Name string
|
|
|
|
|
Age int
|
|
|
|
|
Sex int
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
|
}{1, "钟南山", 83, 1, time.Now()}
|
|
|
|
|
doctorJson, _ := json.Marshal(doctor)
|
|
|
|
|
client.Set("doctor2", doctorJson, time.Hour)
|
|
|
|
|
|
|
|
|
|
//读取结构
|
|
|
|
|
doctorResult, _ := client.Get("doctor2").Result()
|
|
|
|
|
var doctor2 struct {
|
|
|
|
|
Id int
|
|
|
|
|
Name string
|
|
|
|
|
Age int
|
|
|
|
|
Sex int
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
|
}
|
|
|
|
|
//反序列化
|
|
|
|
|
json.Unmarshal([]byte(doctorResult), &doctor2)
|
|
|
|
|
fmt.Println("doctor2", doctor2)
|
|
|
|
|
|
|
|
|
|
//list----------------------------------------------------
|
|
|
|
|
//通道列表 list
|
|
|
|
|
listKey := "go2list"
|
|
|
|
|
client.RPush(listKey, "str1", 10, "str2", 15, "str3", 20).Err()
|
|
|
|
|
|
|
|
|
|
//lpop 取出并移除左边第一个元素
|
|
|
|
|
first, _ := client.LPop(listKey).Result()
|
|
|
|
|
fmt.Printf("列表第一个元素 key:%v value:%v \n", first[0], first[1])
|
|
|
|
|
|
|
|
|
|
//Blpop 取出并移除左边第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
|
|
|
|
|
first2, _ := client.BLPop(time.Second*60, listKey).Result()
|
|
|
|
|
fmt.Printf("列表第一个元素 key:%v value:%v \n", first2[0], first2[1])
|
|
|
|
|
|
|
|
|
|
//数据长度
|
|
|
|
|
listLen, _ := client.LLen(listKey).Result()
|
|
|
|
|
fmt.Println("list length", listLen)
|
|
|
|
|
|
|
|
|
|
//获取列表
|
|
|
|
|
listGet, _ := client.LRange(listKey, 1, 2).Result()
|
|
|
|
|
fmt.Println("索引1-2的2个元素元素", listGet)
|
|
|
|
|
|
|
|
|
|
//hash-------------------------------------------
|
|
|
|
|
hashKey := "userkey_1"
|
|
|
|
|
//set hash 适合存储结构
|
|
|
|
|
client.HSet(hashKey, "name", "叶子")
|
|
|
|
|
client.HSet(hashKey, "age", 18)
|
|
|
|
|
|
|
|
|
|
//get hash
|
|
|
|
|
hashGet, _ := client.HGet(hashKey, "name").Result()
|
|
|
|
|
fmt.Println("HGet name", hashGet)
|
|
|
|
|
|
|
|
|
|
//获取所有hash 返回map
|
|
|
|
|
hashGetAll, _ := client.HGetAll(hashKey).Result()
|
|
|
|
|
fmt.Println("HGetAll", hashGetAll)
|
2021-05-12 09:03:38 +00:00
|
|
|
}
|