golang-yitisheng-server/server/config/redis.go

43 lines
752 B
Go

// Package config Redis配置
package config
import (
"context"
"fmt"
"log"
"time"
"github.com/redis/go-redis/v9"
)
// RDB 全局Redis客户端
var RDB *redis.Client
// InitRedis 初始化Redis连接
func InitRedis() {
RDB = redis.NewClient(&redis.Options{
Addr: "81.70.191.16:56379",
Password: "Rd@5Wk8#Nv3Yt6$Bm",
DB: 1,
PoolSize: 10, // 连接池大小
})
// 测试连接
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := RDB.Ping(ctx).Result()
if err != nil {
log.Fatal("Redis连接失败:", err)
}
fmt.Println("Redis连接成功")
}
// CloseRedis 关闭Redis连接
func CloseRedis() {
if RDB != nil {
RDB.Close()
fmt.Println("Redis连接已关闭")
}
}