在Go语言中基础的Redis操作

9/29/2018 GoRedis后端

在Go语言中基础的Redis操作需要先安装 redigo go get "github.com/garyburd/redigo/redis"Go语言Redis客户端的简单示例连接池 POOL为了和redis建立连接,需要创建一个redis.Pool的对象 func newPool() redis

# 在Go语言中基础的Redis操作

需要先安装 redigo

go get "github.com/garyburd/redigo/redis"
1

Go语言Redis客户端的简单示例

# 连接池 POOL

为了和redis建立连接,需要创建一个redis.Pool的对象

func newPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:   10,
		MaxActive: 12000,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", ":6379")
			if err != nil {
				panic(err)
			}
			return c, err
		},
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 测试连接 PING

如果想测试是否连接成功,可以使用 PING 命令

func ping(c redis.Conn) error {
	pong, err := c.Do("PING")
	if err != nil {
		return err
	}

	s, err := redis.String(pong, err)
	if err != nil {
		return err
	}
	fmt.Printf("PING Response = %s\n", s)
	return nil
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# SET

func set(c redis.Conn) error {
	_, err := c.Do("SET", "Favorite Movie", "Repo Man")
	if err != nil {
		return err
	}
	_, err = c.Do("SET", "Release Year", 1984)
	if err != nil {
		return err
	}
	return nil
}
1
2
3
4
5
6
7
8
9
10
11

# GET

func get(c redis.Conn) error {
	key := "Favorite Movie"
	s, err := redis.String(c.Do("GET", key))
	if err != nil {
		return err
	}
	fmt.Printf("%s = %s\n", key, s)

	key = "Release Year"
	i, err := redis.Int(c.Do("GET", key))
	if err != nil {
		return err
	}
	fmt.Printf("%s = %d\n", key, i)

	key = "Nonexistent Key"
	s, err = redis.String(c.Do("GET", key))
	if err == redis.ErrNil {
		fmt.Printf("%s does not exist\n", key)
	} else if err != nil {
		return err
	} else {
		fmt.Printf("%s = %s\n", key, s)
	}
	return nil
}
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

# SET STRUCT

func setStruct(c redis.Conn) error {
	const objectPrefix string = "user:"
	user := User{
		Username:  "coderminer.com",
		MobileID:  "12345678941",
		Email:     "kevin@163.com",
		FirstName: "coderminer.com",
		LastName:  "coderminer.com",
	}

	json, err := json.Marshal(user)
	if err != nil {
		return err
	}

	_, err = c.Do("SET", objectPrefix+user.Username, json)
	if err != nil {
		return err
	}
	return nil
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# GET STRUCT

func getStruct(c redis.Conn) error {
	const objectPrefix string = "user:"
	username := "coderminer.com"
	s, err := redis.String(c.Do("GET", objectPrefix+username))
	if err == redis.ErrNil {
		fmt.Println("User does not exist")
	} else if err != nil {
		return err
	}
	user := User{}
	err = json.Unmarshal([]byte(s), &user)
	fmt.Printf("%+v\n", user)
	return nil
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 最终的代码

package main

import (
	"encoding/json"
	"fmt"

	"github.com/garyburd/redigo/redis"
)

type User struct {
	Username  string
	MobileID  string
	Email     string
	FirstName string
	LastName  string
}

func newPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:   10,
		MaxActive: 12000,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", ":6379")
			if err != nil {
				panic(err)
			}
			return c, err
		},
	}
}

func ping(c redis.Conn) error {
	pong, err := c.Do("PING")
	if err != nil {
		return err
	}

	s, err := redis.String(pong, err)
	if err != nil {
		return err
	}
	fmt.Printf("PING Response = %s\n", s)
	return nil
}

func set(c redis.Conn) error {
	_, err := c.Do("SET", "Favorite Movie", "Repo Man")
	if err != nil {
		return err
	}
	_, err = c.Do("SET", "Release Year", 1984)
	if err != nil {
		return err
	}
	return nil
}

func get(c redis.Conn) error {
	key := "Favorite Movie"
	s, err := redis.String(c.Do("GET", key))
	if err != nil {
		return err
	}
	fmt.Printf("%s = %s\n", key, s)

	key = "Release Year"
	i, err := redis.Int(c.Do("GET", key))
	if err != nil {
		return err
	}
	fmt.Printf("%s = %d\n", key, i)

	key = "Nonexistent Key"
	s, err = redis.String(c.Do("GET", key))
	if err == redis.ErrNil {
		fmt.Printf("%s does not exist\n", key)
	} else if err != nil {
		return err
	} else {
		fmt.Printf("%s = %s\n", key, s)
	}
	return nil
}

func setStruct(c redis.Conn) error {
	const objectPrefix string = "user:"
	user := User{
		Username:  "coderminer.com",
		MobileID:  "12345678941",
		Email:     "kevin@163.com",
		FirstName: "coderminer.com",
		LastName:  "coderminer.com",
	}

	json, err := json.Marshal(user)
	if err != nil {
		return err
	}

	_, err = c.Do("SET", objectPrefix+user.Username, json)
	if err != nil {
		return err
	}
	return nil
}

func getStruct(c redis.Conn) error {
	const objectPrefix string = "user:"
	username := "coderminer.com"
	s, err := redis.String(c.Do("GET", objectPrefix+username))
	if err == redis.ErrNil {
		fmt.Println("User does not exist")
	} else if err != nil {
		return err
	}
	user := User{}
	err = json.Unmarshal([]byte(s), &user)
	fmt.Printf("%+v\n", user)
	return nil
}

func main() {
	pool := newPool()
	conn := pool.Get()
	defer conn.Close()

	err := ping(conn)
	if err != nil {
		fmt.Println(err)
	}

	err = set(conn)
	if err != nil {
		fmt.Println(err)
	}

	err = get(conn)
	if err != nil {
		fmt.Println(err)
	}

	err = setStruct(conn)
	if err != nil{
		fmt.Println(err)
	}

	err = getStruct(conn)
	if err != nil{
		fmt.Println(err)
	}
}

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

更多精彩内容 (opens new window)