升级增加client

This commit is contained in:
白茶清欢 2024-06-19 21:21:34 +08:00
parent 7e866b3f1f
commit cec7fa76e5
2 changed files with 40 additions and 3 deletions

View File

@ -9,6 +9,7 @@ package abstract
import (
"context"
"git.zhangdeman.cn/zhangdeman/redis/define"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
@ -22,7 +23,7 @@ import (
type IRedisClient interface {
GetRealClient(instanceFlag string) *redis.ClusterClient // 获取客户端连接
GetRealClientWithError(instanceFlag string) (*redis.ClusterClient, error) // 获取带error的客户端连接
AddClient(instanceFlag string, instanceConfig *redis.ClusterOptions) error // 添加新的客户端连接
AddClient(instanceFlag string, instanceConfig *define.ClusterOptions) error // 添加新的客户端连接
RemoveClient(instanceFlag string) // 移除一个客户端连接
SetLogger(loggerInstance *zap.Logger) // 设置日志实例, 全局生效, 而非针对某一个实例
Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult // 执行任意命令

View File

@ -141,8 +141,44 @@ func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.Cl
return instance, nil
}
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *redisClient.ClusterOptions) error {
instance := redisClient.NewClusterClient(instanceConfig)
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.ClusterOptions) error {
instance := redisClient.NewClusterClient(&redisClient.ClusterOptions{
Addrs: instanceConfig.Addrs,
ClientName: instanceConfig.ClientName,
NewClient: nil,
MaxRedirects: instanceConfig.MaxRedirects,
ReadOnly: instanceConfig.ReadOnly,
RouteByLatency: instanceConfig.RouteByLatency,
RouteRandomly: instanceConfig.RouteRandomly,
ClusterSlots: nil,
Dialer: nil,
OnConnect: func(ctx context.Context, cn *redisClient.Conn) error {
return nil
},
Protocol: 0,
Username: instanceConfig.Username,
Password: instanceConfig.Password,
CredentialsProvider: nil,
CredentialsProviderContext: nil,
MaxRetries: instanceConfig.MaxRetries,
MinRetryBackoff: time.Duration(instanceConfig.MinRetryBackoff) * time.Millisecond,
MaxRetryBackoff: time.Duration(instanceConfig.MaxRetryBackoff) * time.Millisecond,
DialTimeout: time.Duration(instanceConfig.DialTimeout) * time.Millisecond,
ReadTimeout: time.Duration(instanceConfig.ReadTimeout) * time.Millisecond,
WriteTimeout: time.Duration(instanceConfig.WriteTimeout) * time.Millisecond,
ContextTimeoutEnabled: instanceConfig.ContextTimeoutEnabled,
PoolFIFO: instanceConfig.PoolFIFO,
PoolSize: instanceConfig.PoolSize,
PoolTimeout: time.Duration(instanceConfig.PoolTimeout) * time.Millisecond,
MinIdleConns: instanceConfig.MinIdleConn,
MaxIdleConns: instanceConfig.MaxIdleConn,
MaxActiveConns: instanceConfig.MaxActiveConn,
ConnMaxIdleTime: time.Duration(instanceConfig.ConnMaxIdleTime) * time.Second,
ConnMaxLifetime: time.Duration(instanceConfig.ConnMaxIdleTime) * time.Second,
TLSConfig: nil,
DisableIndentity: instanceConfig.DisableIdentity,
IdentitySuffix: instanceConfig.IdentitySuffix,
})
o.lock.Lock()
defer o.lock.Unlock()
o.instanceTable[instanceFlag] = instance