Passed
Push — main ( f6597a...58b626 )
by Yume
01:31 queued 12s
created

infrastructures.CloseRedis   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package infrastructures
2
3
import (
4
	"context"
5
6
	"github.com/memnix/memnix-rest/config"
7
	"github.com/redis/go-redis/v9"
8
)
9
10
var redisClient *redis.Client
11
12
// ConnectRedis Connects to redis
13
func ConnectRedis() error {
14
	redisClient = NewRedisClient()
15
16
	_, err := redisClient.Ping(context.Background()).Result()
17
	if err != nil {
18
		return err
19
	}
20
21
	return nil
22
}
23
24
// CloseRedis Closes redis connection
25
func CloseRedis() error {
26
	return redisClient.Close()
27
}
28
29
// GetRedisClient Returns redis client
30
func GetRedisClient() *redis.Client {
31
	return redisClient
32
}
33
34
// NewRedisClient Returns new redis client
35
func NewRedisClient() *redis.Client {
36
	var redisHost string
37
	if config.IsDevelopment() {
38
		redisHost = config.EnvHelper.GetEnv("DEBUG_REDIS_URL")
39
	} else {
40
		redisHost = config.EnvHelper.GetEnv("REDIS_URL")
41
	}
42
43
	client := redis.NewClient(&redis.Options{
44
		Addr:         redisHost,
45
		MinIdleConns: config.RedisMinIdleConns,
46
		PoolSize:     config.RedisPoolSize,
47
		PoolTimeout:  config.RedisPoolTimeout,
48
	})
49
50
	return client
51
}
52