|
@@ -169,6 +169,7 @@ func (r *GoRedis) init(addr, code string, dbs []int, poolSize, minIdleConns, idl
|
|
|
r.DB = dbs[0]
|
|
|
r.Ctx = context.Background()
|
|
|
r.CMap = map[int]map[int]*redis.Client{}
|
|
|
+ r.CMap[0] = map[int]*redis.Client{}
|
|
|
if len(dbs) > 1 {
|
|
|
r.HashDb = len(dbs) - 1
|
|
|
r.DBS = []int{}
|
|
@@ -202,10 +203,9 @@ func (r *GoRedis) GetDB(key string) (int, int) {
|
|
|
case 1:
|
|
|
return 0, hashCode(key) % len(r.DBS)
|
|
|
case 2:
|
|
|
- return hashCode(key) % len(r.Nodes), r.DB
|
|
|
+ return hashCode(key[len(key)/2:]) % len(r.Nodes), r.DB
|
|
|
case 3:
|
|
|
- hk := hashCode(key)
|
|
|
- return hk % len(r.Nodes), hk % len(r.DBS)
|
|
|
+ return hashCode(key[len(key)/2:]) % len(r.Nodes), hashCode(key) % len(r.DBS)
|
|
|
}
|
|
|
// if r.HashDb {
|
|
|
// //return int(key[len(key)-1]) % len(r.DBS)
|
|
@@ -225,19 +225,29 @@ func hashCode(key string) int {
|
|
|
return v
|
|
|
}
|
|
|
|
|
|
+func (r *GoRedis) Client(key string) *redis.Client {
|
|
|
+ _i, _k := r.GetDB(key)
|
|
|
+ m := r.CMap[_i]
|
|
|
+ if m != nil {
|
|
|
+ c := m[_k]
|
|
|
+ if c != nil {
|
|
|
+ return c
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return &redis.Client{}
|
|
|
+}
|
|
|
+
|
|
|
//-----具体方法
|
|
|
//简单的Put方法
|
|
|
func (r *GoRedis) Put(key string, val interface{}) (string, error) {
|
|
|
- i, k := r.GetDB(key)
|
|
|
- stutsCmd := r.CMap[i][k].Set(r.Ctx, key, val, 0)
|
|
|
+ stutsCmd := r.Client(key).Set(r.Ctx, key, val, 0)
|
|
|
str, err := stutsCmd.Result()
|
|
|
return str, err
|
|
|
}
|
|
|
|
|
|
//存key,加过期时间
|
|
|
func (r *GoRedis) Set(key string, val interface{}, timeout int) (string, error) {
|
|
|
- i, k := r.GetDB(key)
|
|
|
- stutsCmd := r.CMap[i][k].Set(r.Ctx, key, val, D(timeout))
|
|
|
+ stutsCmd := r.Client(key).Set(r.Ctx, key, val, D(timeout))
|
|
|
//cmd := r.CMap[r.GetDB(key)].Do(r.Ctx, "setex", key, timeout, val)
|
|
|
str, err := stutsCmd.Result()
|
|
|
return str, err
|
|
@@ -245,8 +255,7 @@ func (r *GoRedis) Set(key string, val interface{}, timeout int) (string, error)
|
|
|
|
|
|
//简单的Get方法,无结果返回空串,err: redis: nil
|
|
|
func (r *GoRedis) Get(key string) (string, error) {
|
|
|
- i, k := r.GetDB(key)
|
|
|
- stutsCmd := r.CMap[i][k].Get(r.Ctx, key)
|
|
|
+ stutsCmd := r.Client(key).Get(r.Ctx, key)
|
|
|
str, err := stutsCmd.Result()
|
|
|
// log.Println("1", str, "-", err)
|
|
|
if err != nil {
|
|
@@ -297,8 +306,7 @@ func (r *GoRedis) BulkPut(timeout int, obj interface{}) {
|
|
|
if ok && len(tmp) == 2 {
|
|
|
key, ok1 := tmp[0].(string)
|
|
|
if ok1 {
|
|
|
- _i, _k := r.GetDB(key)
|
|
|
- r.CMap[_i][_k].Set(r.Ctx, key, tmp[1], timeEx)
|
|
|
+ r.Client(key).Set(r.Ctx, key, tmp[1], timeEx)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -306,8 +314,7 @@ func (r *GoRedis) BulkPut(timeout int, obj interface{}) {
|
|
|
case map[string]interface{}:
|
|
|
if objs, ok := obj.(map[string]interface{}); ok {
|
|
|
for k, v := range objs {
|
|
|
- _i, _k := r.GetDB(k)
|
|
|
- r.CMap[_i][_k].Set(r.Ctx, k, v, timeEx)
|
|
|
+ r.Client(k).Set(r.Ctx, k, v, timeEx)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -358,15 +365,13 @@ func (r *GoRedis) BulkPut(timeout int, obj interface{}) {
|
|
|
|
|
|
//设置超时时间,单位秒
|
|
|
func (r *GoRedis) SetExpire(key string, expire int) error {
|
|
|
- i, k := r.GetDB(key)
|
|
|
- boolCmd := r.CMap[i][k].Expire(r.Ctx, key, D(expire))
|
|
|
+ boolCmd := r.Client(key).Expire(r.Ctx, key, D(expire))
|
|
|
return boolCmd.Err()
|
|
|
}
|
|
|
|
|
|
//判断一个key是否存在
|
|
|
func (r *GoRedis) Exists(key string) bool {
|
|
|
- _i, _k := r.GetDB(key)
|
|
|
- intCmd := r.CMap[_i][_k].Exists(r.Ctx, key)
|
|
|
+ intCmd := r.Client(key).Exists(r.Ctx, key)
|
|
|
i, err := intCmd.Result()
|
|
|
if err != nil {
|
|
|
log.Println("redisutil-exists", key, err)
|
|
@@ -376,8 +381,7 @@ func (r *GoRedis) Exists(key string) bool {
|
|
|
|
|
|
//直接返回字节流
|
|
|
func (r *GoRedis) GetBytes(key string) (ret *[]byte, err error) {
|
|
|
- i, k := r.GetDB(key)
|
|
|
- cmd := r.CMap[i][k].Do(r.Ctx, "GET", key)
|
|
|
+ cmd := r.Client(key).Do(r.Ctx, "GET", key)
|
|
|
res, err := cmd.Result()
|
|
|
log.Println(res)
|
|
|
if err != nil {
|
|
@@ -400,8 +404,7 @@ func (r *GoRedis) Del(key ...string) (b bool) {
|
|
|
i := 0
|
|
|
if r.HashDb > 0 {
|
|
|
for _, k := range key {
|
|
|
- _i, _k := r.GetDB(k)
|
|
|
- cmd := r.CMap[_i][_k].Del(r.Ctx, k)
|
|
|
+ cmd := r.Client(k).Del(r.Ctx, k)
|
|
|
i1, _ := cmd.Result()
|
|
|
i += int(i1)
|
|
|
}
|
|
@@ -423,16 +426,14 @@ func (r *GoRedis) DelByPattern(key string) {
|
|
|
|
|
|
//自增计数器
|
|
|
func (r *GoRedis) Incr(key string) (int64, error) {
|
|
|
- _i, _k := r.GetDB(key)
|
|
|
- intCmd := r.CMap[_i][_k].Incr(r.Ctx, key)
|
|
|
+ intCmd := r.Client(key).Incr(r.Ctx, key)
|
|
|
i, err := intCmd.Result()
|
|
|
return i, err
|
|
|
}
|
|
|
|
|
|
//自减
|
|
|
func (r *GoRedis) Decrby(key string, val int) (int64, error) {
|
|
|
- _i, _k := r.GetDB(key)
|
|
|
- intCmd := r.CMap[_i][_k].DecrBy(r.Ctx, key, int64(val))
|
|
|
+ intCmd := r.Client(key).DecrBy(r.Ctx, key, int64(val))
|
|
|
i, err := intCmd.Result()
|
|
|
return i, err
|
|
|
}
|
|
@@ -476,8 +477,8 @@ func (r *GoRedis) Mget(key []string) []interface{} {
|
|
|
|
|
|
//取出并删除Key
|
|
|
func (r *GoRedis) Pop(key string) (result interface{}) {
|
|
|
- _i, _k := r.GetDB(key)
|
|
|
- strCmd := r.CMap[_i][_k].Get(r.Ctx, key)
|
|
|
+ c := r.Client(key)
|
|
|
+ strCmd := c.Get(r.Ctx, key)
|
|
|
b, err := strCmd.Bytes()
|
|
|
if err != nil {
|
|
|
log.Println("Poperr bytes", err)
|
|
@@ -488,7 +489,7 @@ func (r *GoRedis) Pop(key string) (result interface{}) {
|
|
|
log.Println("Poperr json ", err)
|
|
|
return
|
|
|
} else {
|
|
|
- go r.CMap[_i][_k].Del(r.Ctx, key)
|
|
|
+ go c.Del(r.Ctx, key)
|
|
|
}
|
|
|
|
|
|
return
|
|
@@ -496,8 +497,7 @@ func (r *GoRedis) Pop(key string) (result interface{}) {
|
|
|
|
|
|
//list操作
|
|
|
func (r *GoRedis) LPOP(list string) (result interface{}) {
|
|
|
- _i, _k := r.GetDB(list)
|
|
|
- strCmd := r.CMap[_i][_k].LPop(r.Ctx, list)
|
|
|
+ strCmd := r.Client(list).LPop(r.Ctx, list)
|
|
|
b, err := strCmd.Bytes()
|
|
|
if err != nil {
|
|
|
log.Println("LPOP bytes", err)
|
|
@@ -513,8 +513,7 @@ func (r *GoRedis) LPOP(list string) (result interface{}) {
|
|
|
|
|
|
//将一个或多个值插入到列表的尾部
|
|
|
func (r *GoRedis) RPUSH(list string, val ...interface{}) bool {
|
|
|
- _i, _k := r.GetDB(list)
|
|
|
- intCmd := r.CMap[_i][_k].RPush(r.Ctx, list, val...)
|
|
|
+ intCmd := r.Client(list).RPush(r.Ctx, list, val...)
|
|
|
i, err := intCmd.Result()
|
|
|
if err != nil {
|
|
|
log.Println("RPUSH bytes", err)
|
|
@@ -525,8 +524,7 @@ func (r *GoRedis) RPUSH(list string, val ...interface{}) bool {
|
|
|
|
|
|
//获取列表长度
|
|
|
func (r *GoRedis) LLEN(list string) int64 {
|
|
|
- _i, _k := r.GetDB(list)
|
|
|
- intCmd := r.CMap[_i][_k].LLen(r.Ctx, list)
|
|
|
+ intCmd := r.Client(list).LLen(r.Ctx, list)
|
|
|
i, err := intCmd.Result()
|
|
|
if err != nil {
|
|
|
log.Println("RPUSH bytes", err)
|