-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
78 lines (66 loc) · 1.64 KB
/
redis.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
package common
import (
"encoding/json"
"log"
"github.com/go-redis/redis"
"github.com/pkg/errors"
)
var (
KEY_ONLINE_PLAYERS = "bancho/online"
)
type RedisAPI struct {
client *redis.Client
}
func NewRedis(addr string, pass string, db int) (rds *RedisAPI) {
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: pass,
DB: db,
})
rds = &RedisAPI{client}
return
}
func (rds *RedisAPI) AddClient(client *Client) (err error) {
data, err := client.Serialize()
if err != nil {
err = errors.Wrap(err, "RedisAPI.AddClient: error serializing client to json")
return
}
_, err = rds.client.HSet(KEY_ONLINE_PLAYERS, client.Uuid, data).Result()
return
}
func (rds *RedisAPI) GetClientByToken(token string) (client Client, err error) {
data, err := rds.client.HGet(KEY_ONLINE_PLAYERS, token).Result()
if err == redis.Nil {
err = errors.Errorf("RedisAPI.GetClientByToken: client with token '%s' doesn't exist", token)
return
} else if err != nil {
err = errors.Wrap(err, "RedisAPI.GetClientByToken: error getting client")
return
}
err = json.Unmarshal([]byte(data), &client)
if err != nil {
err = errors.Wrap(err, "RedisAPI.GetClientByToken: unmarshalling json")
}
return
}
func (rds *RedisAPI) GetOnlineClients() (clients []Client, err error) {
data, err := rds.client.HGetAll(KEY_ONLINE_PLAYERS).Result()
if err != nil {
return
}
clients = make([]Client, len(data))
i := 0
for _, value := range data {
var client Client
err = json.Unmarshal([]byte(value), &client)
if err != nil {
log.Println("Error decoding client from Redis:", err)
i++
continue
}
clients[i] = client
i++
}
return
}