-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis.go
41 lines (33 loc) · 780 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
package redis
import (
"context"
"github.com/go-redis/redis/v8"
log "github.com/sirupsen/logrus"
"sync"
"time"
)
var (
instance *redis.Client
once sync.Once
)
func NewRedisDB(redisOptions *redis.Options) *redis.Client {
once.Do(func() {
instance = Connect(redisOptions)
})
return instance
}
func Connect(redisOptions *redis.Options) *redis.Client {
log.Infoln("Connecting to redis database...")
ctx := context.Background()
db := redis.NewClient(redisOptions)
if err := db.Ping(ctx).Err(); err != nil {
log.Errorln(err)
time.Sleep(2 * time.Second)
db := redis.NewClient(redisOptions)
if err := db.Ping(ctx).Err(); err != nil {
log.Panicf("Redis connection error %+v\n", err)
}
}
log.Infoln("Redis successfully connected...")
return db
}