-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
71 lines (63 loc) · 1.75 KB
/
cache.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
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"os"
"time"
)
var client *redis.Client
func SetupRedis(){
client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT")),
Password: os.Getenv("REDIS_PASSWORD"), // no password set
DB: 0, // use default DB
})
}
func StoreData(ctx context.Context, username string, uuid string, response string) error {
client.Set(ctx, fmt.Sprintf("player:%s", username), response, time.Hour * 24)
client.Set(ctx, fmt.Sprintf("uuid:%s", uuid), response, time.Hour * 24)
return nil
}
func HasDataFromUsername(ctx context.Context, username string) (bool, error) {
value, err := client.Exists(ctx, fmt.Sprintf("player:%s", username)).Result()
if err == redis.Nil {
return false, nil
} else if err != nil {
return false, nil
}
if value == 0 {
return false, nil
}
return true, nil
}
func HasDataFromUUID(ctx context.Context, uuid string) (bool, error) {
value, err := client.Exists(ctx, fmt.Sprintf("uuid:%s", uuid)).Result()
if err == redis.Nil {
return false, nil
} else if err != nil {
return false, nil
}
if value == 0 {
return false, nil
}
return true, nil
}
func GetDataFromUsername(ctx context.Context, username string) (*string, error) {
value, err := client.Get(ctx, fmt.Sprintf("player:%s", username)).Result()
if err == redis.Nil {
return nil, fmt.Errorf("does not exist")
} else if err != nil {
return nil, err
}
return &value, nil
}
func GetDataFromUUID(ctx context.Context, uuid string) (*string, error) {
value, err := client.Get(ctx, fmt.Sprintf("uuid:%s", uuid)).Result()
if err == redis.Nil {
return nil, fmt.Errorf("does not exist")
} else if err != nil {
return nil, err
}
return &value, nil
}