init
This commit is contained in:
34
utils/redis.go
Normal file
34
utils/redis.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Redis 封装结构体
|
||||
type Redis struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
// 初始化Redis连接
|
||||
func InitRedis() (*Redis, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建 Redis 客户端
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379", // Redis 服务器地址
|
||||
Password: "", // 密码,没有则为空
|
||||
DB: 0, // 默认数据库
|
||||
})
|
||||
|
||||
// 测试连接
|
||||
if _, err := rdb.Ping(ctx).Result(); err != nil {
|
||||
return nil, fmt.Errorf("redis连接失败: %w", err)
|
||||
}
|
||||
|
||||
log.Println("✅ Redis连接成功")
|
||||
return &Redis{rdb: rdb}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user