61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
// Package util provide some utility functions
|
|
package util
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type RedisOptions struct {
|
|
redisOptions *redis.Options
|
|
timeout time.Duration
|
|
}
|
|
|
|
type RedisOption func(*RedisOptions) error
|
|
|
|
// WithPassword define func of configure redis password options
|
|
func WithPassword(password string) RedisOption {
|
|
return func(o *RedisOptions) error {
|
|
if password == "" {
|
|
return errors.New("password is empty")
|
|
}
|
|
o.redisOptions.Password = password
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithTimeout define func of configure redis timeout options
|
|
func WithTimeout(timeout time.Duration) RedisOption {
|
|
return func(o *RedisOptions) error {
|
|
if timeout < 0 {
|
|
return errors.New("timeout can not be negative")
|
|
}
|
|
o.timeout = timeout
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithDB define func of configure redis db options
|
|
func WithDB(db int) RedisOption {
|
|
return func(o *RedisOptions) error {
|
|
if db < 0 {
|
|
return errors.New("db can not be negative")
|
|
}
|
|
o.redisOptions.DB = db
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithPoolSize define func of configure pool size options
|
|
func WithPoolSize(poolSize int) RedisOption {
|
|
return func(o *RedisOptions) error {
|
|
if poolSize <= 0 {
|
|
return errors.New("pool size must be greater than 0")
|
|
}
|
|
o.redisOptions.PoolSize = poolSize
|
|
return nil
|
|
}
|
|
}
|