infrastructures/redis.go   A
last analyzed

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A infrastructures.GetRedisClient 0 2 1
A infrastructures.NewRedisClient 0 11 1
A infrastructures.CloseRedis 0 2 1
A infrastructures.ConnectRedis 0 9 2
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