mirror of
https://github.com/eolinker/apinto
synced 2025-09-27 05:06:11 +08:00
33 lines
710 B
Go
33 lines
710 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/eolinker/eosc"
|
|
"github.com/go-redis/redis/v8"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Addrs []string `json:"addrs" label:"redis 节点列表"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
func (c *Config) connect() (*redis.ClusterClient, error) {
|
|
if len(c.Addrs) == 0 {
|
|
return nil, fmt.Errorf("addrs:%w", eosc.ErrorRequire)
|
|
}
|
|
nc := redis.NewClusterClient(&redis.ClusterOptions{
|
|
Addrs: c.Addrs,
|
|
Username: c.Username,
|
|
Password: c.Password,
|
|
})
|
|
timeout, _ := context.WithTimeout(context.Background(), time.Second)
|
|
if err := nc.Ping(timeout).Err(); err != nil {
|
|
nc.Close()
|
|
return nil, err
|
|
}
|
|
return nc, nil
|
|
}
|