代码拉取完成,页面将自动刷新
package redis
import (
"context"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/server"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
)
var _ Client = (*Connector)(nil)
type Connector struct {
selectName string
clientMap map[string]*Adapter
}
func NewRedis(conf []server.Redis) (Client, error) {
zap.L().Info("Redis", zap.Any("Config", conf))
clients := &Connector{clientMap: map[string]*Adapter{}}
for i := range conf {
adapter, err := NewAdapter(conf[i].Name, &conf[i].Master, &conf[i].Sentinel, &conf[i].Cluster)
if err != nil {
continue
}
if conf[i].Logger.LogLevel > 0 {
redis.SetLogger(&Logger{
RedisLogger: conf[i].Logger,
})
}
clients.clientMap[conf[i].Name] = adapter
}
return clients, nil
}
func (c *Connector) Get(name string) (Redis, error) {
if len(name) == 0 {
name = c.selectName
}
cli, exists := c.clientMap[name]
if exists == true {
return cli, nil
}
return nil, result.Error(result.ErrNotFound, "Not found")
}
func (c *Connector) Close(name string) error {
if len(name) == 0 {
name = c.selectName
}
cli, exists := c.clientMap[name]
if exists == true {
return cli.client.Close()
}
return nil
}
func (c *Connector) CloseAll() {
for _, cli := range c.clientMap {
_ = cli.client.Close()
}
c.clientMap = nil
}
func (c *Connector) Select(name string) {
c.selectName = name
}
func (c *Connector) SelectDB(name string, index int) error {
cli, err := c.Get(name)
if err != nil {
return err
}
_, err = cli.Rds().Do(context.Background(), "select", index).Result()
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。