28 lines
635 B
Go
28 lines
635 B
Go
package svc
|
|
|
|
import (
|
|
"chat/internal/config"
|
|
"context"
|
|
"fmt"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
func initRedis(ctx context.Context, c config.RedisConf) (*redis.Client, error) {
|
|
// 初始化 Redis 客戶端
|
|
redisClient := redis.NewClient(&redis.Options{
|
|
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
|
|
Password: c.Password,
|
|
DB: c.DB,
|
|
})
|
|
|
|
// 測試 Redis 連線
|
|
if err := redisClient.Ping(ctx).Err(); err != nil {
|
|
logx.Errorf("Failed to connect to Redis: %v", err)
|
|
return nil, err
|
|
}
|
|
logx.Infof("Connected to Redis at %s:%d", c.Host, c.Port)
|
|
|
|
return redisClient, nil
|
|
}
|