2021-09-26 02:35:02 +00:00
|
|
|
package gredis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2021-10-15 03:43:41 +00:00
|
|
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
|
|
"github.com/gomodule/redigo/redis"
|
2021-09-26 02:35:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetRedisConn(m *model.RedisModel) *redis.Pool {
|
|
|
|
redisConn := &redis.Pool{
|
|
|
|
MaxIdle: m.MaxIdle,
|
|
|
|
MaxActive: m.MaxActive,
|
|
|
|
IdleTimeout: m.IdleTimeout * time.Second,
|
|
|
|
Dial: func() (redis.Conn, error) {
|
|
|
|
c, err := redis.Dial("tcp", m.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if m.Password != "" {
|
|
|
|
if _, err := c.Do("AUTH", m.Password); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
},
|
|
|
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
|
|
_, err := c.Do("PING")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return redisConn
|
|
|
|
}
|