diff --git a/abstract/IRedisClient.go b/abstract/IRedisClient.go index 4da5f19..3dce1b4 100644 --- a/abstract/IRedisClient.go +++ b/abstract/IRedisClient.go @@ -21,9 +21,9 @@ import ( // // Date : 15:09 2024/6/18 type IRedisClient interface { - GetRealClient(instanceFlag string) *redis.ClusterClient // 获取客户端连接 - GetRealClientWithError(instanceFlag string) (*redis.ClusterClient, error) // 获取带error的客户端连接 - AddClient(instanceFlag string, instanceConfig *define.ClusterOptions) error // 添加新的客户端连接 + GetRealClient(instanceFlag string) *redis.Client // 获取客户端连接 + GetRealClientWithError(instanceFlag string) (*redis.Client, error) // 获取带error的客户端连接 + AddClient(instanceFlag string, instanceConfig *define.Options) error // 添加新的客户端连接 RemoveClient(instanceFlag string) // 移除一个客户端连接 SetLogger(loggerInstance *zap.Logger) // 设置日志实例, 全局生效, 而非针对某一个实例 Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult // 执行任意命令 diff --git a/client.go b/client.go index df5f0bb..a91150e 100644 --- a/client.go +++ b/client.go @@ -28,14 +28,14 @@ var ( func init() { Client = &OwnClient{ lock: &sync.RWMutex{}, - instanceTable: make(map[string]*redisClient.ClusterClient), + instanceTable: make(map[string]*redisClient.Client), whiteCommandTable: make(map[string]bool), } } type OwnClient struct { lock *sync.RWMutex - instanceTable map[string]*redisClient.ClusterClient + instanceTable map[string]*redisClient.Client whiteCommandTable map[string]bool logger *zap.Logger } @@ -62,7 +62,7 @@ func (o *OwnClient) isAllowCommand(command string) bool { // Date : 11:05 2024/6/19 func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult { var ( - instance *redisClient.ClusterClient + instance *redisClient.Client ) cmdParamList := []any{ @@ -127,13 +127,13 @@ func (o *OwnClient) SetCommandWhiteList(commandList []string) { } } -func (o *OwnClient) GetRealClient(instanceFlag string) *redisClient.ClusterClient { +func (o *OwnClient) GetRealClient(instanceFlag string) *redisClient.Client { o.lock.RLock() defer o.lock.RUnlock() return o.instanceTable[instanceFlag] } -func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.ClusterClient, error) { +func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.Client, error) { o.lock.RLock() defer o.lock.RUnlock() instance, exist := o.instanceTable[instanceFlag] @@ -143,17 +143,11 @@ func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.Cl return instance, nil } -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, +func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.Options) error { + instance := redisClient.NewClient(&redisClient.Options{ + Addr: instanceConfig.Addr, + ClientName: instanceConfig.ClientName, + Dialer: nil, OnConnect: func(ctx context.Context, cn *redisClient.Conn) error { return nil }, diff --git a/define/config.go b/define/config.go index 95e60fe..956c93d 100644 --- a/define/config.go +++ b/define/config.go @@ -7,23 +7,12 @@ // Date : 2024-06-18 16:12 package define -type ClusterOptions struct { +type Options struct { + Network string `json:"network" yaml:"network" ini:"network" toml:"network"` // 网络连接方式 // A seed list of host:port addresses of cluster nodes. - Addrs []string `json:"addrs" yaml:"addrs" ini:"addrs" toml:"addrs"` // 集群IP列表 + Addr string `json:"addr" yaml:"addr" ini:"addr" toml:"addr"` // 集群IP列表 // ClientName will execute the `CLIENT SETNAME ClientName` command for each conn. ClientName string `json:"client_name" yaml:"client_name" ini:"client_name" toml:"client_name"` // 集群名称 - // The maximum number of retries before giving up. Command is retried - // on network errors and MOVED/ASK redirects. - // Default is 3 retries. - MaxRedirects int `json:"max_redirects" yaml:"max_redirects" ini:"max_redirects" toml:"max_redirects"` // 最大重试次数 - // Enables read-only commands on slave nodes. - ReadOnly bool `json:"read_only" yaml:"read_only" ini:"read_only" toml:"read_only"` // 只读 - // Allows routing read-only commands to the closest master or slave node. - // It automatically enables ReadOnly. - RouteByLatency bool `json:"route_by_latency" yaml:"route_by_latency" ini:"route_by_latency" toml:"route_by_latency"` - // Allows routing read-only commands to the random master or slave node. - // It automatically enables ReadOnly. - RouteRandomly bool `json:"route_randomly" yaml:"route_randomly" ini:"route_randomly" toml:"route_randomly"` Protocol int Username string `json:"username" yaml:"username" ini:"username" toml:"username"`