library-go/store/valkey/option.go

50 lines
1.0 KiB
Go

package valkey
import (
"context"
"github.com/valkey-io/valkey-go"
"github.com/zeromicro/go-zero/core/errorx"
"time"
)
// Option defines the method to customize a Redis.
type Option func(r *VK)
// Cluster customizes the given Redis as a cluster.
func Cluster() Option {
return func(r *VK) {
r.Type = ClusterType
}
}
// SetSlowThreshold sets the slow threshold.
func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
}
// WithPass customizes the given Redis with given password.
func WithPass(pass string) Option {
return func(r *VK) {
r.Pass = pass
}
}
// WithTLS customizes the given Redis with TLS enabled.
func WithTLS() Option {
return func(r *VK) {
r.tls = true
}
}
// WithHook customizes the given Redis with given durationHook, only for private use now,
// maybe expose later.
func WithHook(hook Hook) Option {
return func(r *VK) {
r.hooks = append(r.hooks, hook)
}
}
func acceptable(err error) bool {
return err == nil || errorx.In(err, valkey.Nil, context.Canceled)
}