MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / InitRedisClient

Function InitRedisClient

common/redis.go:24–54  ·  view source on GitHub ↗

InitRedisClient This function is called after init()

()

Source from the content-addressed store, hash-verified

22
23// InitRedisClient This function is called after init()
24func 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
56func ParseRedisOption() *redis.Options {
57 opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))

Callers 1

InitResourcesFunction · 0.92

Calls 4

SysLogFunction · 0.85
FatalLogFunction · 0.85
GetEnvOrDefaultFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected