不会飞的章鱼

熟能生巧,勤能补拙;念念不忘,必有回响。

Golang操作Redis数据库小结

起因

十月中旬要做出一个和Redis相关的服务,在此记录Golang操作Redis的笔记。

Golang操作Redis

(内容还在更新中…)

使用redisgo包

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main

import (
"fmt"

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

func main() {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()

//写入值
_, err = c.Do("SET", "username", "nick")
if err != nil {
fmt.Println("redis set failed:", err)
}

//读取key对应的value值
username, err := redis.String(c.Do("GET", "username"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get username %v \n", username)
}

//从右往左,存一个名字为mylist的列表
_, err = c.Do("lpush", "mylist", "ofo", "mobike", "foo")
if err != nil {
fmt.Println("redis lpush failed", err.Error())
}

//从左往右,给一个名字为mylist的列表存值
_, err = c.Do("rpush", "mylist", "bluegogo", "xiaolan", "xiaoming")
if err != nil {
fmt.Println("redis rpush failed", err.Error())
}

//获取这个列表的长度
num, err := c.Do("llen", "mylist")
if err != nil {
fmt.Println("mylist get len err", err.Error())
} else {
fmt.Println("mylist's len is ", num)
}

//获取mylist列表下表从0到10的值
values, err := redis.Values(c.Do("lrange", "mylist", 0, 10))
if err != nil {
fmt.Println("lrange err", err.Error())
}
fmt.Printf("mylist is:")
for _, v := range values {
fmt.Printf(" %s ", v.([]byte))
}
fmt.Println()

_, err = c.Do("ltrim", "mylist", 0, 4)
if err != nil {
fmt.Println("ltrim error", err.Error())
} else {
values, err = redis.Values(c.Do("lrange", "mylist", 0, 4))
if err != nil {
fmt.Println("ltrim failed:", err.Error())
}
fmt.Printf("ltrim mylist is:")
for _, v := range values {
fmt.Printf("%s ", v.([]byte))
}
fmt.Printf("\n")
}

val, err := c.Do("lindex", "mylist", 2)
if err != nil {
fmt.Println("lindex error:", err.Error())
} else {
fmt.Printf("lindex get result :%s\n", val)
}

_, err = c.Do("rpoplpush", "mylist", "mybike")
if err != nil {
fmt.Println("rpoplpush failed:", err.Error())
} else {
values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))
if err != nil {
fmt.Println("lrange failed:", err.Error())
}
for _, v := range values {
fmt.Printf("rpoplpush %s\n", v.([]byte))
}

values, err = redis.Values(c.Do("lrange", "mybike", 0, 10))
if err != nil {
fmt.Println("lrange failed:", err.Error())
}
for _, v := range values {
//fmt.Println(string(v.([]byte)))
fmt.Printf("rpoplpush %s\n", v.([]byte))
}
}

_, err = c.Do("lset", "mylist", 2, "mysql")
if err != nil {
fmt.Println("lset error:", err.Error())
}
val, err = c.Do("lindex", "mylist", 2)
if err != nil {
fmt.Println("lset error:", err.Error())
} else {
fmt.Printf("lset get result:%s\n", val)
}

_, err = c.Do("lrem", "mylist", 1, "mysql")
if err != nil {
fmt.Println("lrem error", err.Error())
} else {
values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))
if err != nil {
fmt.Println("ltrim failed:", err.Error())
}
for _, v := range values {
fmt.Printf("lrem mylist: %s", v.([]byte))
}
fmt.Printf("\n")
}

_, err = c.Do("lpop", "mylist")
if err != nil {
fmt.Println("lpop failed:", err.Error())
} else {
values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))
if err != nil {
fmt.Println("lpop failed:", err.Error())
}
fmt.Printf("lpop mylist :")
for _, v := range values {
fmt.Printf("lpop mylist %s", v.([]byte))
}
fmt.Printf("\n")
}

_, err = c.Do("rpop", "mylist")
if err != nil {
fmt.Println("rpop failed", err.Error())
} else {
values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))
if err != nil {
fmt.Println("rpop failed:", err.Error())
}
fmt.Printf("rpop mylist :")
for _, v := range values {
//fmt.Println(string(v.([]byte)))
fmt.Printf("lpop mylist %s", v.([]byte))
}
fmt.Printf("\n")
}

res, err := c.Do("blpop", "mylist", 10)
if err != nil {
fmt.Println("blpop error")
} else {
fmt.Printf("blpop from mylist get:%s\n", res)
}

res, err = c.Do("blpop", "tmpbike", 10)
if err != nil {
fmt.Println("blpop time out")
} else {
fmt.Println("blpop from tmpbike get:", res)
}

res, err = c.Do("brpop", "tmpbike", 10)
if err != nil {
fmt.Println("brpop error")
} else {
fmt.Printf("brpop from tmpbike get :%s\n", res)
}

res, err = c.Do("brpop", "mybike", 10)
if err != nil {
fmt.Println("brpop time out")
} else {
fmt.Printf("brpop from mybike get:%s ", res)

}

}

参考资料

------ 本文结束------
如果本篇文章对你有帮助,可以给作者加个鸡腿~(*^__^*),感谢鼓励与支持!