邹方成 e2782a69d3 feat: 添加对对碰游戏功能与Redis支持
refactor: 重构抽奖逻辑以支持可验证凭据
feat(redis): 集成Redis客户端并添加配置支持
fix: 修复订单取消时的优惠券和库存处理逻辑
docs: 添加对对碰游戏前端对接指南和示例JSON
test: 添加对对碰游戏模拟测试和验证逻辑
2025-12-21 17:31:32 +08:00

57 lines
1.3 KiB
Go

package redis
import (
"context"
"sync"
"time"
"bindbox-game/configs"
"bindbox-game/internal/pkg/logger"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
)
var (
once sync.Once
client *redis.Client
)
// Init Initialize Redis client
func Init(ctx context.Context, logger logger.CustomLogger) error {
var err error
once.Do(func() {
cfg := configs.Get().Redis
client = redis.NewClient(&redis.Options{
Addr: cfg.Addr,
Password: cfg.Pass,
DB: cfg.DB,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
PoolSize: 20,
})
// Ping to verify connection
pingCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err = client.Ping(pingCtx).Err(); err != nil {
logger.Error("Failed to connect to Redis", zap.String("addr", cfg.Addr), zap.Error(err))
return // err set above
}
logger.Info("Connected to Redis", zap.String("addr", cfg.Addr))
})
return err
}
// GetClient Returns the singleton Redis client
func GetClient() *redis.Client {
if client == nil {
// Should have been initialized. If not, panic or try init?
// For safety in this strict environment, let's assume Init was called in main.
// Panic might be safer to detect misuse early.
panic("Redis client not initialized")
}
return client
}