Select Git revision
-
Jano Hendriks authoredJano Hendriks authored
redis.go 8.00 KiB
package redis
import (
"context"
"encoding/json"
"fmt"
"math"
"os"
"strings"
"time"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"github.com/go-redis/redis_rate/v9"
"github.com/go-redis/redis/v8"
)
var (
ctx = context.Background()
redisClient *ClientWithHelpers
)
type ClientWithHelpers struct {
Client *redis.Client
Available bool
}
type SetLockKeyOptions struct {
Value *string
MaxRetries *int
RetryInterval *time.Duration
FallbackResult *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,