2021-01-16 03:00:25 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2024-07-04 15:13:19 +03:00
|
|
|
"time"
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
"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{}
|
|
|
|
|
|
|
|
func Config(c *redis.Options) store.Option {
|
|
|
|
return store.SetOption(configKey{}, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clusterConfigKey struct{}
|
|
|
|
|
|
|
|
func ClusterConfig(c *redis.ClusterOptions) store.Option {
|
|
|
|
return store.SetOption(clusterConfigKey{}, c)
|
|
|
|
}
|
2024-07-04 15:13:19 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultMeterStatsInterval holds default stats interval
|
|
|
|
DefaultMeterStatsInterval = 5 * time.Second
|
|
|
|
// DefaultMeterMetricPrefix holds default metric prefix
|
|
|
|
DefaultMeterMetricPrefix = "micro_store_"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Options struct holds wrapper options
|
|
|
|
type Options struct {
|
|
|
|
Logger logger.Logger
|
|
|
|
Meter meter.Meter
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
MeterMetricPrefix string
|
|
|
|
MeterStatsInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option func signature
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// NewOptions create new Options struct from provided option slice
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
MeterStatsInterval: DefaultMeterStatsInterval,
|
|
|
|
MeterMetricPrefix: DefaultMeterMetricPrefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
options.Meter = options.Meter.Clone(
|
|
|
|
meter.MetricPrefix(options.MeterMetricPrefix),
|
|
|
|
)
|
|
|
|
|
|
|
|
options.Logger = options.Logger.Clone(logger.WithCallerSkipCount(1))
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetricInterval specifies stats interval for *sql.DB
|
|
|
|
func MetricInterval(td time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.MeterStatsInterval = td
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetricPrefix specifies prefix for each metric
|
|
|
|
func MetricPrefix(pref string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.MeterMetricPrefix = pref
|
|
|
|
}
|
|
|
|
}
|