66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
// Package redis ...
|
|
//
|
|
// Description : redis 客户端
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2021-02-27 4:49 下午
|
|
package redis
|
|
|
|
import (
|
|
"git.zhangdeman.cn/zhangdeman/redis/abstract"
|
|
"github.com/pkg/errors"
|
|
redisClient "github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
"sync"
|
|
)
|
|
|
|
var Client abstract.IRedisClient
|
|
|
|
func init() {
|
|
Client = &OwnClient{
|
|
lock: &sync.RWMutex{},
|
|
instanceTable: make(map[string]*redisClient.ClusterClient),
|
|
}
|
|
}
|
|
|
|
type OwnClient struct {
|
|
lock *sync.RWMutex
|
|
instanceTable map[string]*redisClient.ClusterClient
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func (o *OwnClient) GetRealClient(instanceFlag string) *redisClient.ClusterClient {
|
|
o.lock.RLock()
|
|
defer o.lock.RUnlock()
|
|
return o.instanceTable[instanceFlag]
|
|
}
|
|
|
|
func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.ClusterClient, error) {
|
|
o.lock.RLock()
|
|
defer o.lock.RUnlock()
|
|
instance, exist := o.instanceTable[instanceFlag]
|
|
if !exist {
|
|
return nil, errors.New(instanceFlag + " : redis instance is not found")
|
|
}
|
|
return instance, nil
|
|
}
|
|
|
|
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *redisClient.ClusterOptions) error {
|
|
instance := redisClient.NewClusterClient(instanceConfig)
|
|
o.lock.Lock()
|
|
defer o.lock.Unlock()
|
|
o.instanceTable[instanceFlag] = instance
|
|
return nil
|
|
}
|
|
|
|
func (o *OwnClient) RemoveClient(instanceFlag string) {
|
|
o.lock.Lock()
|
|
defer o.lock.Unlock()
|
|
delete(o.instanceTable, instanceFlag)
|
|
}
|
|
|
|
func (o *OwnClient) SetLogger(loggerInstance *zap.Logger) {
|
|
o.logger = loggerInstance
|
|
}
|