InitRedisClient This function is called after init()
()
| 22 | |
| 23 | // InitRedisClient This function is called after init() |
| 24 | func InitRedisClient() (err error) { |
| 25 | if os.Getenv("REDIS_CONN_STRING") == "" { |
| 26 | RedisEnabled = false |
| 27 | SysLog("REDIS_CONN_STRING not set, Redis is not enabled") |
| 28 | return nil |
| 29 | } |
| 30 | if os.Getenv("SYNC_FREQUENCY") == "" { |
| 31 | SysLog("SYNC_FREQUENCY not set, use default value 60") |
| 32 | SyncFrequency = 60 |
| 33 | } |
| 34 | SysLog("Redis is enabled") |
| 35 | opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING")) |
| 36 | if err != nil { |
| 37 | FatalLog("failed to parse Redis connection string: " + err.Error()) |
| 38 | } |
| 39 | opt.PoolSize = GetEnvOrDefault("REDIS_POOL_SIZE", 10) |
| 40 | RDB = redis.NewClient(opt) |
| 41 | |
| 42 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 43 | defer cancel() |
| 44 | |
| 45 | _, err = RDB.Ping(ctx).Result() |
| 46 | if err != nil { |
| 47 | FatalLog("Redis ping test failed: " + err.Error()) |
| 48 | } |
| 49 | if DebugEnabled { |
| 50 | SysLog(fmt.Sprintf("Redis connected to %s", opt.Addr)) |
| 51 | SysLog(fmt.Sprintf("Redis database: %d", opt.DB)) |
| 52 | } |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | func ParseRedisOption() *redis.Options { |
| 57 | opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING")) |
no test coverage detected