2021-01-16 03:00:25 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2024-10-05 16:11:46 +03:00
|
|
|
goredis "github.com/redis/go-redis/v9"
|
2024-07-04 15:13:19 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
2021-10-27 23:29:16 +03:00
|
|
|
"go.unistack.org/micro/v3/store"
|
2024-07-04 15:13:19 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2021-01-16 03:00:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type configKey struct{}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func Config(c *goredis.Options) store.Option {
|
2021-01-16 03:00:25 +03:00
|
|
|
return store.SetOption(configKey{}, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clusterConfigKey struct{}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func ClusterConfig(c *goredis.ClusterOptions) store.Option {
|
2021-01-16 03:00:25 +03:00
|
|
|
return store.SetOption(clusterConfigKey{}, c)
|
|
|
|
}
|
2024-07-04 15:13:19 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
type universalConfigKey struct{}
|
|
|
|
|
|
|
|
func UniversalConfig(c *goredis.UniversalOptions) store.Option {
|
|
|
|
return store.SetOption(universalConfigKey{}, c)
|
|
|
|
}
|
2024-09-21 13:59:54 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
var (
|
2024-09-21 13:59:54 +03:00
|
|
|
labelHost = "redis_host"
|
|
|
|
labelName = "redis_name"
|
2024-07-04 15:13:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options struct holds wrapper options
|
|
|
|
type Options struct {
|
2024-10-05 16:11:46 +03:00
|
|
|
Logger logger.Logger
|
|
|
|
Meter meter.Meter
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
RedisHost string
|
|
|
|
RedisName string
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option func signature
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// NewOptions create new Options struct from provided option slice
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2024-10-05 16:11:46 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
options.Meter = options.Meter.Clone(
|
2024-09-21 13:59:54 +03:00
|
|
|
meter.Labels(
|
|
|
|
labelHost, options.RedisHost,
|
|
|
|
labelName, options.RedisName),
|
2024-07-04 15:13:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
options.Logger = options.Logger.Clone(logger.WithCallerSkipCount(1))
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|