Skip to content
Snippets Groups Projects
Commit 8a38bdd5 authored by Johan de Klerk's avatar Johan de Klerk
Browse files

Connect to redis client

parent 9e3825e9
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math"
"os"
"strings"
"time"
......@@ -16,13 +17,53 @@ import (
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
var (
ctx = context.Background()
redisClient *ClientWithHelpers
)
type ClientWithHelpers struct {
Client *redis.Client
Available bool
}
func GetRedisClient(isDebug bool) *ClientWithHelpers {
if redisClient != nil && redisClient.IsConnected() {
return redisClient
}
redisClient = connectToRedis(isDebug)
return redisClient
}
func connectToRedis(isDebug bool) *ClientWithHelpers {
if os.Getenv("REDIS_HOST") != "false" {
host := os.Getenv("REDIS_HOST")
port := os.Getenv("REDIS_PORT")
if isDebug {
env := os.Getenv("ENVIRONMENT")
switch env {
case "dev":
port = "6380"
case "stage":
port = "6381"
case "sandbox", "qa":
port = "6382"
case "prod":
port = "6383"
}
}
return NewClient(host + ":" + port)
}
return &ClientWithHelpers{
Client: nil,
Available: false,
}
}
func NewClient(addr string) *ClientWithHelpers {
return &ClientWithHelpers{
Client: redis.NewClient(&redis.Options{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment