升级增加client
This commit is contained in:
parent
7e866b3f1f
commit
cec7fa76e5
@ -9,6 +9,7 @@ package abstract
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/redis/define"
|
"git.zhangdeman.cn/zhangdeman/redis/define"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@ -22,7 +23,7 @@ import (
|
|||||||
type IRedisClient interface {
|
type IRedisClient interface {
|
||||||
GetRealClient(instanceFlag string) *redis.ClusterClient // 获取客户端连接
|
GetRealClient(instanceFlag string) *redis.ClusterClient // 获取客户端连接
|
||||||
GetRealClientWithError(instanceFlag string) (*redis.ClusterClient, error) // 获取带error的客户端连接
|
GetRealClientWithError(instanceFlag string) (*redis.ClusterClient, error) // 获取带error的客户端连接
|
||||||
AddClient(instanceFlag string, instanceConfig *redis.ClusterOptions) error // 添加新的客户端连接
|
AddClient(instanceFlag string, instanceConfig *define.ClusterOptions) error // 添加新的客户端连接
|
||||||
RemoveClient(instanceFlag string) // 移除一个客户端连接
|
RemoveClient(instanceFlag string) // 移除一个客户端连接
|
||||||
SetLogger(loggerInstance *zap.Logger) // 设置日志实例, 全局生效, 而非针对某一个实例
|
SetLogger(loggerInstance *zap.Logger) // 设置日志实例, 全局生效, 而非针对某一个实例
|
||||||
Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult // 执行任意命令
|
Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult // 执行任意命令
|
||||||
|
40
client.go
40
client.go
@ -141,8 +141,44 @@ func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.Cl
|
|||||||
return instance, nil
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *redisClient.ClusterOptions) error {
|
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.ClusterOptions) error {
|
||||||
instance := redisClient.NewClusterClient(instanceConfig)
|
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()
|
o.lock.Lock()
|
||||||
defer o.lock.Unlock()
|
defer o.lock.Unlock()
|
||||||
o.instanceTable[instanceFlag] = instance
|
o.instanceTable[instanceFlag] = instance
|
||||||
|
Loading…
Reference in New Issue
Block a user