HASH操作
1. hgetall
func main() {
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
} else {
fmt.Println("Connect to redis ok.")
}
// 函数退出时关闭连接
defer conn.Close()
personMap := make(map[int]Person)
conn.Send("MULTI")
for i := 0; i < 50; i++ {
key := fmt.Sprintf("test_%d", i)
conn.Send("HGETALL", key)
}
values, err := redis.Values(conn.Do("EXEC"))
for _, value := range values {
p_map, _ := redis.StringMap(value, nil)
for key, v := range p_map {
var person Person
json.Unmarshal([]byte(v), &person)
fmt.Println(key, person)
id, _ := strconv.Atoi(key)
personMap[id] = person
}
}
}
ZSET 操作
1. ZREVRANK
func main() {
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
} else {
fmt.Println("Connect to redis ok.")
}
// 函数退出时关闭连接
defer conn.Close()
ids := []int{1, 2, 3, 4}
key := "test"
conn.Send("MULTI")
for _, id := range ids {
conn.Send("ZREVRANK", key, id)
}
values, err := redis.Values(conn.Do("EXEC"))
if err != nil {
return
}
for i, id := range ids {
value := values[i*2]
if value == nil {
continue
}
rank := value.(int64) + 1
fmt.Println(id, rank)
}
}
网友评论