Total Lines | 44 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package infrastructures |
||
2 | |||
3 | import ( |
||
4 | "github.com/memnix/memnix-rest/config" |
||
5 | "github.com/redis/go-redis/v9" |
||
6 | "golang.org/x/net/context" |
||
7 | ) |
||
8 | |||
9 | var redisClient *redis.Client |
||
10 | |||
11 | // ConnectRedis Connects to redis |
||
12 | func ConnectRedis() error { |
||
13 | redisClient = NewRedisClient() |
||
14 | |||
15 | _, err := redisClient.Ping(context.Background()).Result() |
||
16 | if err != nil { |
||
17 | return err |
||
18 | } |
||
19 | |||
20 | return nil |
||
21 | } |
||
22 | |||
23 | // CloseRedis Closes redis connection |
||
24 | func CloseRedis() error { |
||
25 | return redisClient.Close() |
||
26 | } |
||
27 | |||
28 | // GetRedisClient Returns redis client |
||
29 | func GetRedisClient() *redis.Client { |
||
30 | return redisClient |
||
31 | } |
||
32 | |||
33 | // NewRedisClient Returns new redis client |
||
34 | func NewRedisClient() *redis.Client { |
||
35 | redisHost := config.RedisHost |
||
36 | |||
37 | client := redis.NewClient(&redis.Options{ |
||
38 | Addr: redisHost, |
||
39 | MinIdleConns: config.RedisMinIdleConns, |
||
40 | PoolSize: config.RedisPoolSize, |
||
41 | PoolTimeout: config.RedisPoolTimeout, |
||
42 | }) |
||
43 | |||
44 | return client |
||
45 | } |
||
46 |