-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
52 lines (45 loc) · 1.03 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
package gokord
import (
"context"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
)
var (
// Credentials of redis
Credentials RedisCredentials
// Ctx background
Ctx = context.Background()
)
// RedisBase is an interface helping use of redis to store/cache data
type RedisBase interface {
GenKey(key string) string // GenKey generates the key to use
}
// RedisUser is the default implementation of RedisBase for a Discord User
type RedisUser struct {
RedisBase
DiscordID string
GuildID string
}
var (
ErrNilClient = errors.New("redis.NewClient is nil")
)
func (p *RedisUser) GenKey(key string) string {
return fmt.Sprintf("%s:%s:%s", p.GuildID, p.DiscordID, key)
}
// Get the redis.Client with the given RedisCredentials
func (rc *RedisCredentials) Get() (*redis.Client, error) {
client := redis.NewClient(&redis.Options{
Addr: rc.Address,
Password: rc.Password,
DB: rc.DB,
})
if client == nil {
return nil, ErrNilClient
}
err := client.Ping(Ctx).Err()
if err != nil {
return nil, err
}
return client, err
}