31 lines
852 B
Go
31 lines
852 B
Go
package corelib
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/redis/go-redis/v9"
|
|
"picgo/configs"
|
|
)
|
|
|
|
var (
|
|
RdbClient *redis.Client
|
|
CaptchaKey = "picgo:captcha:" // 验证码存储key
|
|
UserNameKey = "picgo:user:username:"
|
|
)
|
|
|
|
func NewRedis() {
|
|
RdbClient = redis.NewClient(&redis.Options{
|
|
Addr: configs.Settings.Redis.Addr, // Redis服务器地址
|
|
Password: configs.Settings.Redis.Password, // 密码,如果没有密码可以为空
|
|
DB: configs.Settings.Redis.DB, // 使用默认的数据库
|
|
PoolSize: configs.Settings.Redis.PoolSize, // 连接池大小
|
|
MinIdleConns: configs.Settings.Redis.MinidleConns, // 最小空闲连接数
|
|
})
|
|
|
|
// 测试连接
|
|
_, err := RdbClient.Ping(context.Background()).Result()
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Failed to connect to Redis: %v", err))
|
|
}
|
|
}
|