-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redis.go
42 lines (34 loc) · 785 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
42
package infrastructures
import (
"os"
redis "github.com/go-redis/redis/v7"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// IRedis interface
type IRedis interface {
Client() *redis.Client
}
var redisClient *redis.Client
// Redis struct
type Redis struct{}
// Client return new redis client from given address and password
func (r *Redis) Client() *redis.Client {
if redisClient == nil {
client := redis.NewClient(&redis.Options{
Addr: viper.GetString("redis.address"),
Password: "", // no password set
DB: 0, // use default DB
})
_, err := client.Ping().Result()
if err != nil {
log.WithFields(log.Fields{
"code": 5500,
"error": err,
}).Error(err)
os.Exit(1)
}
redisClient = client
}
return redisClient
}