基本使用list hash json
This commit is contained in:
parent
f8b68b8895
commit
23775ed2fe
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
|
@ -41,4 +42,64 @@ func main() {
|
|||
return
|
||||
}
|
||||
fmt.Printf("key:%v 值:%v \n", key, value)
|
||||
|
||||
//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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue