MCPcopy Create free account
hub / github.com/DoNewsCode/core / provideRedisFactory

Function provideRedisFactory

otredis/dependency.go:97–178  ·  view source on GitHub ↗

provideRedisFactory creates Factory and redis.UniversalClient. It is a valid dependency for package core.

(option *providersOption)

Source from the content-addressed store, hash-verified

95// provideRedisFactory creates Factory and redis.UniversalClient. It is a valid
96// dependency for package core.
97func 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{

Callers 3

TestNewRedisFactoryFunction · 0.85
ProvidersFunction · 0.85

Calls 8

NewFactoryFunction · 0.92
CloseMethod · 0.80
newCollectorFunction · 0.70
UnmarshalMethod · 0.65
SprintfMethod · 0.65
DebugMethod · 0.65
WithMethod · 0.45

Tested by 2

TestNewRedisFactoryFunction · 0.68