github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/redis/client.go (about)

     1  package redis
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/go-redis/redis"
     8  	"github.com/sereiner/library/types"
     9  )
    10  
    11  //ClientConf redis客户端配置
    12  type ClientConf struct {
    13  	MasterName  string   `json:"master"`
    14  	Address     []string `json:"addrs"`
    15  	Password    string   `json:"password"`
    16  	Db          int      `json:"db"`
    17  	DialTimeout int      `json:"dial_timeout"`
    18  	RTimeout    int      `json:"read_timeout"`
    19  	WTimeout    int      `json:"write_timeout"`
    20  	PoolSize    int      `json:"pool_size"`
    21  }
    22  
    23  //Client redis client
    24  type Client struct {
    25  	redis.UniversalClient
    26  }
    27  
    28  //ClientOption 配置选项
    29  type ClientOption func(*ClientConf)
    30  
    31  //WithAddress 设置哨兵服务器
    32  func WithAddress(address []string) ClientOption {
    33  	return func(o *ClientConf) {
    34  		o.Address = address
    35  	}
    36  }
    37  
    38  //WithPassword 设置服务器登录密码
    39  func WithPassword(password string) ClientOption {
    40  	return func(o *ClientConf) {
    41  		o.Password = password
    42  	}
    43  }
    44  
    45  //WithDB 设置数据库
    46  func WithDB(db int) ClientOption {
    47  	return func(o *ClientConf) {
    48  		o.Db = db
    49  	}
    50  }
    51  
    52  //WithDialTimeout 设置连接超时时长
    53  func WithDialTimeout(timeout int) ClientOption {
    54  	return func(o *ClientConf) {
    55  		o.DialTimeout = timeout
    56  	}
    57  }
    58  
    59  //WithRTimeout 设置读写超时时长
    60  func WithRTimeout(timeout int) ClientOption {
    61  	return func(o *ClientConf) {
    62  		o.RTimeout = timeout
    63  	}
    64  }
    65  
    66  //WithWTimeout 设置读写超时时长
    67  func WithWTimeout(timeout int) ClientOption {
    68  	return func(o *ClientConf) {
    69  		o.WTimeout = timeout
    70  	}
    71  }
    72  
    73  //NewClient 构建客户端
    74  func NewClient(master string, option ...ClientOption) (r *Client, err error) {
    75  	conf := &ClientConf{}
    76  	for _, opt := range option {
    77  		opt(conf)
    78  	}
    79  	conf.MasterName = master
    80  	return NewClientByConf(conf)
    81  
    82  }
    83  
    84  //NewClientByJSON 根据json构建failover客户端
    85  func NewClientByJSON(j string) (r *Client, err error) {
    86  	conf := &ClientConf{}
    87  	err = json.Unmarshal([]byte(j), &conf)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return NewClientByConf(conf)
    92  }
    93  
    94  //NewClientByConf 根据配置对象构建客户端
    95  func NewClientByConf(conf *ClientConf) (client *Client, err error) {
    96  	conf.DialTimeout = types.DecodeInt(conf.DialTimeout, 0, 3, conf.DialTimeout)
    97  	conf.RTimeout = types.DecodeInt(conf.RTimeout, 0, 3, conf.RTimeout)
    98  	conf.WTimeout = types.DecodeInt(conf.WTimeout, 0, 3, conf.WTimeout)
    99  	conf.PoolSize = types.DecodeInt(conf.PoolSize, 0, 3, conf.PoolSize)
   100  	client = &Client{}
   101  	opts := &redis.UniversalOptions{
   102  		MasterName:   conf.MasterName,
   103  		Addrs:        conf.Address,
   104  		Password:     conf.Password,
   105  		DB:           conf.Db,
   106  		DialTimeout:  time.Duration(conf.DialTimeout) * time.Second,
   107  		ReadTimeout:  time.Duration(conf.RTimeout) * time.Second,
   108  		WriteTimeout: time.Duration(conf.WTimeout) * time.Second,
   109  		PoolSize:     conf.PoolSize,
   110  	}
   111  	client.UniversalClient = redis.NewUniversalClient(opts)
   112  	_, err = client.UniversalClient.Ping().Result()
   113  	return
   114  }