provideRedisFactory creates Factory and redis.UniversalClient. It is a valid dependency for package core.
(option *providersOption)
| 95 | // provideRedisFactory creates Factory and redis.UniversalClient. It is a valid |
| 96 | // dependency for package core. |
| 97 | func provideRedisFactory(option *providersOption) func(p factoryIn) (factoryOut, func()) { |
| 98 | if option.interceptor == nil { |
| 99 | option.interceptor = func(name string, opts *redis.UniversalOptions) {} |
| 100 | } |
| 101 | return func(p factoryIn) (factoryOut, func()) { |
| 102 | factory := di.NewFactory(func(name string) (di.Pair, error) { |
| 103 | var ( |
| 104 | base RedisUniversalOptions |
| 105 | full redis.UniversalOptions |
| 106 | ) |
| 107 | if err := p.Conf.Unmarshal(fmt.Sprintf("redis.%s", name), &base); err != nil { |
| 108 | return di.Pair{}, fmt.Errorf("redis configuration %s not valid: %w", name, err) |
| 109 | } |
| 110 | if len(base.Addrs) == 0 { |
| 111 | base = RedisUniversalOptions{ |
| 112 | Addrs: []string{"127.0.0.1:6379"}, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | full = redis.UniversalOptions{ |
| 117 | Addrs: base.Addrs, |
| 118 | DB: base.DB, |
| 119 | Username: base.Username, |
| 120 | Password: base.Password, |
| 121 | SentinelPassword: base.SentinelPassword, |
| 122 | MaxRetries: base.MaxRetries, |
| 123 | MinRetryBackoff: base.MinRetryBackoff.Duration, |
| 124 | MaxRetryBackoff: base.MaxRetryBackoff.Duration, |
| 125 | DialTimeout: base.DialTimeout.Duration, |
| 126 | ReadTimeout: base.ReadTimeout.Duration, |
| 127 | WriteTimeout: base.WriteTimeout.Duration, |
| 128 | PoolSize: base.PoolSize, |
| 129 | MinIdleConns: base.MinIdleConns, |
| 130 | MaxConnAge: base.MaxConnAge.Duration, |
| 131 | PoolTimeout: base.PoolTimeout.Duration, |
| 132 | IdleTimeout: base.IdleTimeout.Duration, |
| 133 | IdleCheckFrequency: base.IdleCheckFrequency.Duration, |
| 134 | TLSConfig: nil, |
| 135 | MaxRedirects: base.MaxRetries, |
| 136 | ReadOnly: base.ReadOnly, |
| 137 | RouteByLatency: base.RouteByLatency, |
| 138 | RouteRandomly: base.RouteRandomly, |
| 139 | MasterName: base.MasterName, |
| 140 | } |
| 141 | option.interceptor(name, &full) |
| 142 | redis.SetLogger(&RedisLogAdapter{level.Debug(log.With(p.Logger, "tag", "redis"))}) |
| 143 | |
| 144 | client := redis.NewUniversalClient(&full) |
| 145 | if p.Tracer != nil { |
| 146 | client.AddHook( |
| 147 | hook{ |
| 148 | addrs: full.Addrs, |
| 149 | database: full.DB, |
| 150 | tracer: p.Tracer, |
| 151 | }, |
| 152 | ) |
| 153 | } |
| 154 | return di.Pair{ |