1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
adapter.go 3.43 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-10-27 13:42 . redis
package redis
import (
"context"
"fmt"
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/server"
"gitee.com/h79/goutils/plugins"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
"time"
)
var _ Redis = (*Adapter)(nil)
type Adapter struct {
client *redis.Client
sentinel *redis.SentinelClient
cluster *redis.ClusterClient
name string
}
func NewAdapter(name string, cfg *server.RedisConfig, sentinelCfg *server.RedisConfig, clusterCfg *server.RedisConfig) (*Adapter, error) {
cli, err := newClient(cfg)
if err != nil {
return nil, err
}
sentinel, err := newSentinelClient(sentinelCfg)
if err != nil {
zap.L().Warn("Redis: newSentinelClient", zap.Error(err))
}
cluster, err := newClusterClient(clusterCfg)
if err != nil {
zap.L().Warn("Redis: newClusterClient", zap.Error(err))
}
return &Adapter{client: cli, sentinel: sentinel, cluster: cluster, name: name}, nil
}
func newClient(cfg *server.RedisConfig) (*redis.Client, error) {
if len(cfg.Host) == 0 || len(cfg.Host[0]) <= 0 {
return nil, result.RErrParam
}
cli := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: cfg.Host[0],
Username: cfg.User,
Password: cfg.Pwd, // no password set
DB: cfg.DB, // use default DB
DialTimeout: cfg.DialTimeout * time.Second,
ReadTimeout: cfg.ReadTimeout * time.Second,
WriteTimeout: cfg.WriteTimeout * time.Second,
IdleTimeout: cfg.IdleTimeout * time.Minute,
})
ctx := context.Background()
if _, err := cli.Ping(ctx).Result(); err != nil {
alarm(fmt.Sprintf("redis ping(%s)", cfg.Host), err)
}
return cli, nil
}
func newSentinelClient(cfg *server.RedisConfig) (*redis.SentinelClient, error) {
if len(cfg.Host) == 0 || len(cfg.Host[0]) <= 0 {
return nil, result.RErrParam
}
cli := redis.NewSentinelClient(&redis.Options{
Network: "tcp",
Addr: cfg.Host[0],
Username: cfg.User,
Password: cfg.Pwd, // no password set
DB: cfg.DB, // use default DB
DialTimeout: cfg.DialTimeout * time.Second,
ReadTimeout: cfg.ReadTimeout * time.Second,
WriteTimeout: cfg.WriteTimeout * time.Second,
IdleTimeout: cfg.IdleTimeout * time.Minute,
})
ctx := context.Background()
if _, err := cli.Ping(ctx).Result(); err != nil {
alarm(fmt.Sprintf("redis ping(%s)", cfg.Host), err)
}
return cli, nil
}
func newClusterClient(cfg *server.RedisConfig) (*redis.ClusterClient, error) {
if len(cfg.Host) == 0 || len(cfg.Host[0]) <= 0 {
return nil, result.RErrParam
}
cli := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: cfg.Host,
Username: cfg.User,
Password: cfg.Pwd, // no password set
DialTimeout: cfg.DialTimeout * time.Second,
ReadTimeout: cfg.ReadTimeout * time.Second,
WriteTimeout: cfg.WriteTimeout * time.Second,
IdleTimeout: cfg.IdleTimeout * time.Minute,
})
ctx := context.Background()
if _, err := cli.Ping(ctx).Result(); err != nil {
alarm(fmt.Sprintf("redis ping(%s)", cfg.Host), err)
}
return cli, nil
}
func (a *Adapter) Rds() *redis.Client {
return a.client
}
func (a *Adapter) Sentinel() *redis.SentinelClient {
return a.sentinel
}
func (a *Adapter) Cluster() *redis.ClusterClient {
return a.cluster
}
func (a *Adapter) Name() string {
return a.name
}
func alarm(detail string, err error) {
d := debug.New(result.ErrRdsPingInternal).
WithDetail(detail).
WithLevel(debug.DFatalLevel).
WithError(err)
_ = plugins.DoWithError(plugins.KAlarm, d)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.4.23

搜索帮助