mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-05 16:17:00 +08:00
33 lines
750 B
Go
33 lines
750 B
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
"x_admin/config"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var Redis = initRedis()
|
|
|
|
// initRedis 初始化redis客户端
|
|
func initRedis() *redis.Client {
|
|
opt, err := redis.ParseURL(config.RedisConfig.Url)
|
|
if err != nil {
|
|
log.Fatal("initRedis redis.ParseURL err: ", err)
|
|
}
|
|
// opt.PoolSize = config.Config.RedisPoolSize
|
|
opt.MaxIdleConns = config.RedisConfig.MaxIdleConns
|
|
opt.ConnMaxLifetime = time.Duration(config.RedisConfig.ConnMaxLifetime) * time.Second
|
|
|
|
client := redis.NewClient(opt)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
_, err = client.Ping(ctx).Result()
|
|
if err != nil {
|
|
log.Fatal("initRedis client.Ping err: ", err)
|
|
}
|
|
return client
|
|
}
|