Files
lone-services/pkg/logRedis/redis.go
T
gjs f49d155373
CI / changes (push) Successful in 4s
CI / docker-bff (push) Failing after 2s
CI / docker-product (push) Failing after 2s
CI / docker-admin (push) Failing after 1s
CI / docker-user (push) Failing after 1s
add action,login log
2026-08-20 17:35:04 +08:00

55 lines
924 B
Go

package logRedis
import (
"context"
"fmt"
"time"
goredis "github.com/redis/go-redis/v9"
)
const Nil = goredis.Nil
var LogRedisClient *goredis.Client
type LogRedisConfig struct {
Host string
Port int
Password string
DB int
}
func Init(c LogRedisConfig) error {
client, err := New(c)
if err != nil {
return err
}
LogRedisClient = client
return nil
}
func New(c LogRedisConfig) (*goredis.Client, error) {
if c.Host == "" {
return nil, fmt.Errorf("redis: Host is empty")
}
if c.Port == 0 {
c.Port = 6379
}
client := goredis.NewClient(&goredis.Options{
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
Password: c.Password,
DB: c.DB,
})
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
_ = client.Close()
return nil, fmt.Errorf("redis ping: %w", err)
}
return client, nil
}