48 lines
867 B
Go
48 lines
867 B
Go
package lxDb
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gomodule/redigo/redis"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// RedisPool Redis连接池
|
|
var RedisPool *redis.Pool
|
|
|
|
type RedisConfig struct {
|
|
URL string
|
|
MaxIdle int
|
|
MaxActive int
|
|
Password string
|
|
}
|
|
|
|
func InitRedis(conf RedisConfig) {
|
|
if conf.URL == "" {
|
|
RedisPool = nil
|
|
fmt.Println("未配置Redis连接")
|
|
return
|
|
}
|
|
|
|
RedisPool = &redis.Pool{
|
|
MaxIdle: conf.MaxIdle,
|
|
MaxActive: conf.MaxActive,
|
|
IdleTimeout: 240 * time.Second,
|
|
Wait: false,
|
|
Dial: func() (redis.Conn, error) {
|
|
c, err := redis.Dial("tcp", conf.URL, redis.DialPassword(conf.Password))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
},
|
|
}
|
|
c := RedisPool.Get()
|
|
defer c.Close()
|
|
_, err := redis.String(c.Do("PING")) // Redis Ping: PONG
|
|
if err != nil {
|
|
fmt.Println("error while connecting redis:", err)
|
|
os.Exit(-1)
|
|
}
|
|
}
|