-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
43 lines (37 loc) · 805 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
43
package redis
import (
"fmt"
r "github.com/go-redis/redis"
"log"
"strconv"
)
type Redis struct {
URL, Password, Host, Port, DB string
Params *r.Options
Client *r.Client
}
func convertStringToInt(s string) int {
result, err := strconv.Atoi(s)
if err != nil {
log.Fatal("Not parse int")
}
return result
}
func (redis *Redis) Options() *r.Options {
return &r.Options{
Addr: fmt.Sprintf("%s:%s", redis.Host, redis.Port),
Password: redis.Password,
DB: convertStringToInt(redis.DB),
}
}
func (redis *Redis) Alive() bool {
_, err := redis.Client.Ping().Result()
if err != nil {
log.Fatal("Error connect to Redis")
return false
}
return true
}
func (redis *Redis) Connect() *r.Client {
return r.NewClient(redis.Params)
}