Files
workspace/code/allapp/allapp-go/config/redis.go
T
2026-02-26 17:31:08 +08:00

30 lines
530 B
Go

package config
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
func initRedis() (*redis.Client, error) {
cfg := GetConf().Redis
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Password: cfg.Password,
DB: cfg.Db,
})
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := rdb.Ping(ctx).Err(); err != nil {
_ = rdb.Close()
return nil, fmt.Errorf("redis ping failed: %w", err)
}
return rdb, nil
}