modelRT/util/redis_options.go

84 lines
1.8 KiB
Go

// Package util provide some utility functions
package util
import (
"errors"
"time"
"modelRT/constants"
"github.com/redis/go-redis/v9"
)
type clientConfig struct {
*redis.Options
}
type Option func(*clientConfig) error
// WithPassword define func of configure redis password options
func WithPassword(password string, env string) Option {
return func(c *clientConfig) error {
if env == constants.ProductionDeployMode && password == "" {
return errors.New("password is empty")
}
c.Password = password
return nil
}
}
// WithConnectTimeout define func of configure redis connect timeout options
func WithConnectTimeout(timeout time.Duration) Option {
return func(c *clientConfig) error {
if timeout < 0 {
return errors.New("timeout can not be negative")
}
c.DialTimeout = timeout
return nil
}
}
// WithReadTimeout define func of configure redis read timeout options
func WithReadTimeout(timeout time.Duration) Option {
return func(c *clientConfig) error {
if timeout < 0 {
return errors.New("timeout can not be negative")
}
c.ReadTimeout = timeout
return nil
}
}
// WithWriteTimeout define func of configure redis write timeout options
func WithWriteTimeout(timeout time.Duration) Option {
return func(c *clientConfig) error {
if timeout < 0 {
return errors.New("timeout can not be negative")
}
c.WriteTimeout = timeout
return nil
}
}
// WithDB define func of configure redis db options
func WithDB(db int) Option {
return func(c *clientConfig) error {
if db < 0 {
return errors.New("db can not be negative")
}
c.DB = db
return nil
}
}
// WithPoolSize define func of configure pool size options
func WithPoolSize(poolSize int) Option {
return func(c *clientConfig) error {
if poolSize <= 0 {
return errors.New("pool size must be greater than 0")
}
c.PoolSize = poolSize
return nil
}
}