feat: 新增项目公共工具包,包含日志、错误、数据库、缓存、公共proto

This commit is contained in:
fish
2026-03-28 18:14:28 +08:00
parent 27da6939f4
commit 2f55987222
6 changed files with 211 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package redis
import (
"context"
"shared/logger"
"github.com/go-redis/redis/v8"
)
type Config struct {
Addr string
Password string
DB int
}
func NewRedisClient(cfg Config) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: cfg.Addr,
Password: cfg.Password,
DB: cfg.DB,
})
ctx := context.Background()
_, err := client.Ping(ctx).Result()
if err != nil {
logger.Error().Err(err).Msg("Failed to connect to Redis")
return nil
}
logger.Info().Msg("Connected to Redis successfully")
return client
}