forked from kushaldas/bugspad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_op.go
235 lines (207 loc) · 4.48 KB
/
redis_op.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
_ "github.com/go-sql-driver/mysql"
"strconv"
)
type Bug map[string]interface{}
// TODO: Update this codebase to keep all search related indexs on redis.
// Generic function to update a redis HASH
func redis_hset(name, key, value string) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Println(err)
return
}
defer conn.Close()
_, err = conn.Do("HSET", name, key, value)
if err != nil {
// handle error
fmt.Print(err)
return
}
}
// Generic function to get a redis HASH
func redis_hget(name, key string) []byte {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Println(err)
return nil
}
defer conn.Close()
val, err := conn.Do("HGET", name, key)
if err != nil || val == nil {
// handle error
fmt.Print(err)
return nil
}
return val.([]uint8)
}
func update_redis_bug_status(bug_id string, status string) {
redis_hset("b_status:"+status, bug_id, "1")
}
func delete_redis_bug_status(bug_id string, status string) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Println(err)
return
}
defer conn.Close()
conn.Do("HDEL", "b_status:"+status, bug_id)
}
func get_redis_bug(bug_id string) Bug {
m := make(Bug)
data := redis_hget("bugs", bug_id)
if data == nil {
fmt.Println("sorry no data")
return nil
}
err := json.Unmarshal(data, &m)
if err != nil {
fmt.Println(err)
return nil
}
return m
}
/*
Loads all user details from the database into redis. This needs to called before
the server starts up.
*/
func load_users() {
db, err := sql.Open("mysql", conn_str)
defer db.Close()
rows, err := db.Query("SELECT email, password, type from users")
if err == nil {
var email, password, utype string
c := make(chan int)
for rows.Next() {
err = rows.Scan(&email, &password, &utype)
go update_redis(email, password, utype, c)
}
}
defer rows.Close()
fmt.Println("Users loaded.")
}
func update_redis(email string, password string, utype string, channel chan int) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
channel <- 1
return
}
defer conn.Close()
_, err = conn.Do("HSET", "users", email, password)
_, err = conn.Do("HSET", "userstype", email, utype)
if err != nil {
// handle error
fmt.Print(err)
channel <- 1
return
}
channel <- 1
}
func add_latest_created(bug_id string) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return
}
defer conn.Close()
_, err = conn.Do("LPUSH", "latest_created", bug_id)
_, err = conn.Do("LTRIM", "latest_created", 0, 9)
}
/*
This function returns a slice of latest created bugs (last 10)
*/
func get_latest_created_list() interface{} {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return nil
}
val, err := conn.Do("LRANGE", "latest_created", 0, 9)
if err != nil {
// handle error
fmt.Println(err)
return nil
}
return val
}
func add_latest_updated(bug_id string) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return
}
defer conn.Close()
_, err = conn.Do("LPUSH", "latest_updated", bug_id)
_, err = conn.Do("LTRIM", "latest_updated", 0, 9)
}
func get_latest_updated_list() interface{} {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return nil
}
val, err := conn.Do("LRANGE", "latest_updated", 0, 9)
if err != nil {
// handle error
fmt.Println(err)
return nil
}
return val
}
func set_redis_bug(id int64, status, summary string) {
m := make(Bug)
m["id"] = id
m["status"] = status
m["summary"] = summary
data, _ := json.Marshal(m)
sdata := string(data)
sid := strconv.FormatInt(id, 10)
redis_hset("bugs", sid, sdata)
}
func add_redis_release(name string) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return
}
defer conn.Close()
_, err = conn.Do("LPUSH", "releases", name)
}
func get_redis_release_list() interface{} {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return nil
}
val, err := conn.Do("LRANGE", "releases", 0, -1)
if err != nil {
// handle error
fmt.Println(err)
return nil
}
return val
}
func clear_redis_releases() {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
}
conn.Do("DEL", "releases")
}