37 lines
790 B
Go
37 lines
790 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gitee.ltd/lxh/wechat-robot/internal/config"
|
|
"github.com/gofiber/fiber/v2/log"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var Client *redis.Client
|
|
|
|
// InitRedisClient
|
|
// @description: 初始化redis客户端
|
|
func InitRedisClient() {
|
|
var err error
|
|
defer func() {
|
|
if err != nil {
|
|
log.Errorf("Redis连接初始化失败: %s", err)
|
|
}
|
|
}()
|
|
|
|
// 初始化连接
|
|
conn := redis.NewClient(&redis.Options{
|
|
Addr: fmt.Sprintf("%s:%d", config.Scd.Redis.Host, config.Scd.Redis.Port),
|
|
Password: config.Scd.Redis.Password,
|
|
DB: config.Scd.Redis.DB,
|
|
})
|
|
if err = conn.Ping(context.Background()).Err(); err != nil {
|
|
log.Errorf("Redis连接初始化失败: %s", err)
|
|
return
|
|
}
|
|
log.Debug("Redis连接初始化成功")
|
|
Client = conn
|
|
return
|
|
}
|