add action,login log
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user