Skip to content
Snippets Groups Projects
Select Git revision
  • 746ba073abfbb3221958a028bf6a31e0ba7c4b9c
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

redis.go

Blame
  • redis.go 7.61 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,