You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
61 lines
1.2 KiB
1 month ago
|
package redis
|
||
|
|
||
|
import (
|
||
|
"epur-pay/model"
|
||
|
"epur-pay/pkg/logger"
|
||
|
"fmt"
|
||
|
"github.com/gomodule/redigo/redis"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var RPool *redis.Pool
|
||
|
|
||
|
func New(r *model.Redis) {
|
||
|
RPool = newPool(r)
|
||
|
}
|
||
|
|
||
|
func newPool(r *model.Redis) *redis.Pool {
|
||
|
|
||
|
return &redis.Pool{
|
||
|
MaxIdle: r.MaxIdle,
|
||
|
MaxActive: r.MaxActive,
|
||
|
Wait: true,
|
||
|
IdleTimeout: r.IdleTimeout * time.Second,
|
||
|
Dial: func() (redis.Conn, error) {
|
||
|
return Dial(r)
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Dial(r *model.Redis) (redis.Conn, error) {
|
||
|
|
||
|
for {
|
||
|
c, err := dial(r)
|
||
|
if err != nil {
|
||
|
logger.ErrorLogger.Errorln(err.Error())
|
||
|
}
|
||
|
return c, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func dial(r *model.Redis) (redis.Conn, error) {
|
||
|
c, err := redis.Dial(
|
||
|
"tcp",
|
||
|
fmt.Sprintf("%s:%s", r.Host, r.Port),
|
||
|
redis.DialDatabase(r.Name),
|
||
|
redis.DialPassword(r.PassWord),
|
||
|
redis.DialReadTimeout(time.Second*r.ReadTimeout),
|
||
|
redis.DialWriteTimeout(time.Second*r.WriteTimeout),
|
||
|
redis.DialConnectTimeout(time.Second*r.ConnectTimeout))
|
||
|
if err != nil {
|
||
|
logger.ErrorLogger.Errorln("connect redis error!", err.Error())
|
||
|
return nil, err
|
||
|
}
|
||
|
_, err = redis.String(c.Do("PING"))
|
||
|
if err != nil {
|
||
|
logger.ErrorLogger.Errorln("connect redis error!", err.Error())
|
||
|
return nil, err
|
||
|
}
|
||
|
return c, err
|
||
|
}
|