Files
lone-services/pkg/redis/redis.go
T
gjs 647abf73f6
CI / changes (push) Successful in 38s
CI / ad (push) Successful in 32s
CI / admin (push) Successful in 27s
CI / bff (push) Successful in 23s
CI / chore (push) Successful in 24s
CI / equipment (push) Successful in 26s
CI / express (push) Successful in 24s
CI / factory (push) Successful in 22s
CI / payment (push) Successful in 24s
CI / product (push) Successful in 24s
CI / record (push) Successful in 23s
CI / sale (push) Successful in 26s
CI / task (push) Successful in 25s
CI / user (push) Successful in 26s
CI / wecom (push) Successful in 26s
order
2026-09-16 16:39:14 +08:00

60 lines
964 B
Go

package redis
import (
"context"
"fmt"
"time"
goredis "github.com/redis/go-redis/v9"
)
const Nil = goredis.Nil
const RedisNil = "redis: nil"
var Client *goredis.Client
type Config struct {
Host string
Port int
Password string
DB int
}
func Init(c Config) error {
client, err := New(c)
if err != nil {
return err
}
Client = client
return nil
}
func New(c Config) (*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
}
func GetRedisKey(key string) string {
return key
}