2021-04-21 10:22:09 +08:00
|
|
|
package starter
|
|
|
|
|
|
|
|
|
|
import (
|
2022-07-10 12:14:06 +08:00
|
|
|
"context"
|
2021-04-21 10:22:09 +08:00
|
|
|
"fmt"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/config"
|
2023-09-02 17:24:18 +08:00
|
|
|
"mayfly-go/pkg/logx"
|
2022-12-26 20:53:07 +08:00
|
|
|
"mayfly-go/pkg/rediscli"
|
2021-04-21 10:22:09 +08:00
|
|
|
|
2023-03-17 09:46:41 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2021-04-21 10:22:09 +08:00
|
|
|
)
|
|
|
|
|
|
2022-12-26 20:53:07 +08:00
|
|
|
func initRedis() {
|
|
|
|
|
rediscli.SetCli(connRedis())
|
2022-01-13 17:06:04 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-26 20:53:07 +08:00
|
|
|
func connRedis() *redis.Client {
|
2021-04-21 10:22:09 +08:00
|
|
|
// 设置redis客户端
|
2021-07-28 18:03:19 +08:00
|
|
|
redisConf := config.Conf.Redis
|
2023-09-02 17:24:18 +08:00
|
|
|
if redisConf.Host == "" {
|
|
|
|
|
// logx.Panic("未找到redis配置信息")
|
2022-01-13 17:06:04 +08:00
|
|
|
return nil
|
2021-04-21 10:22:09 +08:00
|
|
|
}
|
2023-09-02 17:24:18 +08:00
|
|
|
logx.Infof("连接redis [%s:%d]", redisConf.Host, redisConf.Port)
|
2021-04-21 10:22:09 +08:00
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: fmt.Sprintf("%s:%d", redisConf.Host, redisConf.Port),
|
|
|
|
|
Password: redisConf.Password, // no password set
|
|
|
|
|
DB: redisConf.Db, // use default DB
|
|
|
|
|
})
|
|
|
|
|
// 测试连接
|
2022-07-10 12:14:06 +08:00
|
|
|
_, e := rdb.Ping(context.TODO()).Result()
|
2021-04-21 10:22:09 +08:00
|
|
|
if e != nil {
|
2023-09-02 17:24:18 +08:00
|
|
|
logx.Panicf("连接redis失败! [%s:%d][%s]", redisConf.Host, redisConf.Port, e.Error())
|
2021-04-21 10:22:09 +08:00
|
|
|
}
|
|
|
|
|
return rdb
|
|
|
|
|
}
|