feat: 完善后端脚手架基础能力
This commit is contained in:
@@ -21,6 +21,11 @@ var redisClient *RedisClient
|
||||
|
||||
// InitRedis 初始化Redis客户端
|
||||
func InitRedis(cfg *config.Config, logger *zap.Logger) error {
|
||||
if !cfg.Redis.Enabled {
|
||||
redisClient = nil
|
||||
logger.Info("Redis 已禁用")
|
||||
return nil
|
||||
}
|
||||
// 创建Redis客户端配置
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Redis.Host, cfg.Redis.Port),
|
||||
@@ -55,6 +60,18 @@ func InitRedis(cfg *config.Config, logger *zap.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ping 检查 Redis 是否可用。
|
||||
func (r *RedisClient) Ping(ctx context.Context) error {
|
||||
if r == nil || r.client == nil {
|
||||
return nil
|
||||
}
|
||||
return r.client.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
func RedisEnabled() bool {
|
||||
return redisClient != nil
|
||||
}
|
||||
|
||||
// GetRedisClient 获取Redis客户端实例
|
||||
func GetRedisClient() *RedisClient {
|
||||
return redisClient
|
||||
|
||||
Reference in New Issue
Block a user