go with redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
"fmt"

"golangtest/redis"
)

func main() {
client := redis.GetClient()

val, err := client.HGetAll("todoitem:bbrrgade4b3gfpfecvh0").Result()
fmt.Printf("%v, %v\n", val, err)

client.Set("mytestkey", 100, 0)
fmt.Println(client.Get("mytestkey").Result())

client.LPush("mytestlist", 1, 2, 3, 4, 5, 100)
fmt.Println(client.LPop("mytestlist").Result()) // 100, nil

llen, _ := client.LLen("mytestlist").Result()
fmt.Println(llen) // 5
fmt.Println(client.LRange("mytestlist", 0, llen).Result()) // [5 4 3 2 1] <nil>
fmt.Println(client.LRange("mytestlist", 0, -1).Result()) // [5 4 3 2 1] <nil>

user := map[string]interface{}{
"name": "Jhon",
"age": 26,
"work": "programmer",
}
client.HMSet("mytestuser:1", user)
fmt.Println(client.HGet("mytestuser:1", "work").Result()) // programmer, nil
fmt.Println(client.HGetAll("mytestuser:1").Result()) // map[name:Jhon age:26 work:programmer] <nil>

// SET
client.SAdd("mytestset", 7, 6, 5, 4, 8, 9, 100)
// Get the number of members in a set
fmt.Println(client.SCard("mytestset").Result()) // 7

// Get all the members in a set
fmt.Println(client.SMembers("mytestset").Result()) // [4 5 6 7 8 9 100] <nil>

// 查看redis中所有的key
result, _ := client.Keys("*").Result()
fmt.Println("Redis value: ", result)

// pipe
p := client.Pipeline()
p.HMSet("mytestuser:2", map[string]interface{}{
"name": "Bob",
"age": 26,
"work": "PM",
})

p.LPush("mytestuserlist", "Bob")
p.Set("mytestuser:bob:age", 26, 0)
p.Set("mytestusernumber", 0, 0)
p.Incr("mytestusernumber")
p.Exec()
fmt.Println("mytestuser:2 -> ")
fmt.Println(client.HGetAll("mytestuser:2").Result()) // map[name:Bob age:26 work:PM] <nil>
fmt.Println("mytestuserlist -> ")
fmt.Println(client.LRange("mytestuserlist", 0, -1).Result()) // mytestuserlist ->[Bob] <nil>
fmt.Println("mytestusernumber -> ")
fmt.Println(client.Get("mytestusernumber").Result()) // 1 <nil>
fmt.Println("mytestuser:bob:age -> ")
fmt.Println(client.Get("mytestuser:bob:age").Result()) // 26 <nil>
}