lxutils/lxDb/redis.go
liuchangshun c28cf29933 123
2023-11-22 09:59:12 +08:00

48 lines
866 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: true,
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)
}
}