github.com/jtzjtz/kit@v1.0.2/conn/redis_pool/redis_pool.go (about)

     1  package redis_pool
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/gomodule/redigo/redis"
     6  )
     7  
     8  func NewRedisPool(o *Options) (*redis.Pool, error) {
     9  
    10  	if err := o.validate(); err != nil {
    11  		return nil, err
    12  	}
    13  
    14  	pool := &redis.Pool{
    15  		MaxActive:   o.MaxCap,
    16  		MaxIdle:     o.InitCap,
    17  		IdleTimeout: o.IdleTimeout,
    18  		Wait:        o.IsWait,
    19  		Dial: func() (redis.Conn, error) {
    20  			conn, err := redis.Dial(
    21  				"tcp",
    22  				o.Host,
    23  				redis.DialDatabase(o.Database),
    24  				redis.DialPassword(o.PassWord),
    25  				redis.DialConnectTimeout(o.DialConnectTimeout),
    26  				redis.DialReadTimeout(o.DialReadTimeout),
    27  				redis.DialWriteTimeout(o.DialWriteTimeout),
    28  			)
    29  			if err != nil {
    30  				fmt.Println(err)
    31  				return nil, err
    32  			}
    33  			return conn, nil
    34  		},
    35  	}
    36  	return pool, nil
    37  }