-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis.go
41 lines (34 loc) · 851 Bytes
/
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
import (
"context"
"github.com/go-redis/redis/v8"
"time"
)
var rdb *redis.Client
func Setup(cfg config.RedisConfig) {
rdb = redis.NewClient(&redis.Options{
Addr: cfg.Host,
Password: cfg.Password,
DB: cfg.Db,
WriteTimeout: cfg.WriteTimeout * time.Second,
})
}
func Ping(ctx context.Context) error {
return rdb.Ping(ctx).Err()
}
func Get(ctx context.Context, key string) (string, error) {
return rdb.Get(ctx, key).Result()
}
func Set(ctx context.Context, key string, value []byte, duration time.Duration) error {
return rdb.Set(ctx, key, value, duration).Err()
}
func Delete(ctx context.Context, key string) error {
return rdb.Del(ctx, key).Err()
}
func Exist(ctx context.Context, key string) bool {
res := rdb.Exists(ctx, key)
if res != nil && res.Val() > 0 {
return true
} else {
return false
}
}