diff --git a/lego/sys/mgo/core.go b/lego/sys/mgo/core.go index 691a206ce..eb0fc742d 100644 --- a/lego/sys/mgo/core.go +++ b/lego/sys/mgo/core.go @@ -30,6 +30,8 @@ type ( UpdateOne(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) UpdateMany(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) UpdateManyByCtx(sqltable core.SqlTable, ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) + BulkWrite(sqltable core.SqlTable, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) + BulkWriteByCtx(sqltable core.SqlTable, ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) FindOneAndDelete(sqltable core.SqlTable, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) DeleteMany(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) diff --git a/lego/sys/mgo/mgo.go b/lego/sys/mgo/mgo.go index 734e69c61..6fb81cdf9 100644 --- a/lego/sys/mgo/mgo.go +++ b/lego/sys/mgo/mgo.go @@ -135,6 +135,16 @@ func (this *Mongodb) FindOneAndDelete(sqltable core.SqlTable, filter interface{} return this.Collection(sqltable).FindOneAndDelete(this.getContext(), filter, opts...) } +///批量写 +func (this *Mongodb) BulkWrite(sqltable core.SqlTable, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) { + return this.Collection(sqltable).BulkWrite(this.getContext(), models, opts...) +} + +///批量写 +func (this *Mongodb) BulkWriteByCtx(sqltable core.SqlTable, ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) { + return this.Collection(sqltable).BulkWrite(ctx, models, opts...) +} + func (this *Mongodb) DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { return this.Collection(sqltable).DeleteOne(this.getContext(), filter, opts...) } diff --git a/lego/sys/redis/cluster/core.go b/lego/sys/redis/cluster/core.go index 7d1ccdf91..a59688ae9 100644 --- a/lego/sys/redis/cluster/core.go +++ b/lego/sys/redis/cluster/core.go @@ -88,6 +88,6 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) { //锁 func (this *Redis) UnLock(key string) (err error) { - err = this.Delete(key) + _, err = this.Delete(key) return } diff --git a/lego/sys/redis/cluster/hash.go b/lego/sys/redis/cluster/hash.go index 875e1235c..4572af92b 100644 --- a/lego/sys/redis/cluster/hash.go +++ b/lego/sys/redis/cluster/hash.go @@ -1,8 +1,6 @@ package cluster import ( - "reflect" - "github.com/go-redis/redis/v8" ) @@ -10,13 +8,7 @@ import ( Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略 */ func (this *Redis) HDel(key string, fields ...string) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "HDEL") - agrs = append(agrs, key) - for _, v := range fields { - agrs = append(agrs, v) - } - err = this.client.Do(this.getContext(), agrs...).Err() + err = this.client.HDel(this.getContext(), key, fields...).Err() return } @@ -24,18 +16,15 @@ func (this *Redis) HDel(key string, fields ...string) (err error) { Redis Hexists 命令用于查看哈希表的指定字段是否存在 */ func (this *Redis) HExists(key string, field string) (result bool, err error) { - result, err = this.client.Do(this.getContext(), "HEXISTS", key, field).Bool() + this.client.HExists(this.getContext(), key, field) return } /* Redis Hget 命令用于返回哈希表中指定字段的值 */ -func (this *Redis) HGet(key string, field string, value interface{}) (err error) { - var resultvalue string - if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) { - err = this.Decode([]byte(resultvalue), value) - } +func (this *Redis) HGet(key string, field string, v interface{}) (err error) { + err = this.client.HGet(this.getContext(), key, field).Scan(v) return } @@ -43,19 +32,8 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error) Redis Hgetall 命令用于返回哈希表中,所有的字段和值。 在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍 */ -func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) - this.client.Process(this.getContext(), cmd) - var _result []string - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } +func (this *Redis) HGetAll(key string, v interface{}) (err error) { + err = this.client.HGetAll(this.getContext(), key).Scan(v) return } @@ -67,8 +45,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 本操作的值被限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) HIncrBy(key string, field string, value int) (err error) { - err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() +func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { + err = this.client.HIncrBy(this.getContext(), key, field, value).Err() return } @@ -76,8 +54,8 @@ func (this *Redis) HIncrBy(key string, field string, value int) (err error) { Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 */ -func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { - err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() +func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { + err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err() return } @@ -85,17 +63,21 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er Redis Hkeys 命令用于获取哈希表中的所有域(field) */ func (this *Redis) Hkeys(key string) (result []string, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() + var cmd *redis.StringSliceCmd + cmd = this.client.HKeys(this.getContext(), key) + result = cmd.Val() + err = cmd.Err() return } /* Redis Hlen 命令用于获取哈希表中字段的数量 */ -func (this *Redis) Hlen(key string) (result int, err error) { - result, err = this.client.Do(this.getContext(), "HLEN", key).Int() +func (this *Redis) Hlen(key string) (result int64, err error) { + var cmd *redis.IntCmd + cmd = this.client.HLen(this.getContext(), key) + result = cmd.Val() + err = cmd.Err() return } @@ -103,26 +85,8 @@ func (this *Redis) Hlen(key string) (result int, err error) { Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 如果指定的字段不存在于哈希表,那么返回一个 nil 值 */ -func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "HMGET") - agrs = append(agrs, key) - for _, v := range fields { - agrs = append(agrs, v) - } - cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) - this.client.Process(this.getContext(), cmd) - var _result []string - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd { + return this.client.HMGet(this.getContext(), key, fields...) } /* @@ -148,10 +112,7 @@ Redis Hset 命令用于为哈希表中的字段赋值 如果字段已经存在于哈希表中,旧值将被覆盖 */ func (this *Redis) HSet(key string, field string, value interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err() - } + err = this.client.HSet(this.getContext(), key, field, value).Err() return } @@ -162,9 +123,6 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令 */ func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err() - } + err = this.client.HSetNX(this.getContext(), key, field, value).Err() return } diff --git a/lego/sys/redis/cluster/key.go b/lego/sys/redis/cluster/key.go index 2db92cd50..311515e50 100644 --- a/lego/sys/redis/cluster/key.go +++ b/lego/sys/redis/cluster/key.go @@ -1,84 +1,69 @@ package cluster import ( - "github.com/go-redis/redis/v8" + "time" ) /* Key *******************************************************************************/ ///删除redis key -func (this *Redis) Delete(key string) (err error) { - err = this.client.Do(this.getContext(), "DEL", key).Err() - - return +func (this *Redis) Delete(key ...string) (int64, error) { + return this.client.Del(this.getContext(), key...).Result() } ///判断是否存在key -func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { - iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() - return +func (this *Redis) Exists(key ...string) (int64, error) { + return this.client.Exists(this.getContext(), key...).Result() } ///设置key的过期时间 单位以秒级 -func (this *Redis) ExpireKey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() - return +func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { + return this.client.Expire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 秒级时间戳 -func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() - return +func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { + return this.client.ExpireAt(this.getContext(), key, expire).Result() } ///设置key的过期时间 单位以毫秒级 -func (this *Redis) Pexpirekey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() - return +func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { + return this.client.PExpire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 单位以豪秒级 -func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() - return +func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { + return this.client.PExpireAt(this.getContext(), key, expire).Result() } ///移除Key的过期时间 -func (this *Redis) PersistKey(key string) (err error) { - err = this.client.Do(this.getContext(), "PERSIST", key).Err() - return +func (this *Redis) Persist(key string) (bool, error) { + return this.client.Persist(this.getContext(), key).Result() } ///获取key剩余过期时间 单位毫秒 -func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() - return +func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { + return this.client.PTTL(this.getContext(), key).Result() } ///获取key剩余过期时间 单位秒 -func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() - return +func (this *Redis) TTL(key string) (surplus time.Duration, err error) { + return this.client.TTL(this.getContext(), key).Result() } ///重命名Key -func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() - return +func (this *Redis) Rename(oldkey string, newkey string) (string, error) { + return this.client.Rename(this.getContext(), oldkey, newkey).Result() } ///重命名key 在新的 key 不存在时修改 key 的名称 -func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() - return +func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { + return this.client.RenameNX(this.getContext(), oldkey, newkey).Result() } ///判断是否存在key pattern:key* func (this *Redis) Keys(pattern string) (keys []string, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) - this.client.Process(this.getContext(), cmd) - keys, err = cmd.Result() - return + return this.client.Keys(this.getContext(), pattern).Result() } ///获取键类型 diff --git a/lego/sys/redis/cluster/list.go b/lego/sys/redis/cluster/list.go index 9e5a98c7a..9b9981167 100644 --- a/lego/sys/redis/cluster/list.go +++ b/lego/sys/redis/cluster/list.go @@ -1,23 +1,14 @@ package cluster import ( - "fmt" - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Lindex(key string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { + return this.client.LIndex(this.getContext(), key, index).Scan(v) } /* @@ -25,90 +16,55 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { - var ( - tagervalue []byte - resultvalue []byte - ) - if tagervalue, err = this.Encode(tager); err == nil { - if resultvalue, err = this.Encode(value); err == nil { - if isbefore { - err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err() - } else { - err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err() - } - } +func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + op := "BEFORE" + if !isbefore { + op = "AFTER" } - return + return this.client.LInsert(this.getContext(), key, op, pivot, value).Result() } /* Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Llen(key string) (result int, err error) { - result, err = this.client.Do(this.getContext(), "LLEN", key).Int() - return +func (this *Redis) Llen(key string) (int64, error) { + return this.client.LLen(this.getContext(), key).Result() } /* Redis Lpop 命令用于移除并返回列表的第一个元素 */ -func (this *Redis) LPop(key string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) LPop(key string, v interface{}) (err error) { + return this.client.LPop(this.getContext(), key).Scan(v) } /* Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 */ -func (this *Redis) LPush(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "LPUSH") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { + return this.client.LPush(this.getContext(), key, values...).Result() } /* Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 */ -func (this *Redis) LPushX(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "LPUSHX") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { + // agrs := make([]interface{}, 0) + // agrs = append(agrs, "LPUSHX") + // for _, v := range values { + // result, _ := this.Encode(v) + // agrs = append(agrs, result) + // } + // err = this.client.Do(this.getContext(), agrs...).Err() + return this.client.LPushX(this.getContext(), key, values...).Result() } /* Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { - var _result []string - cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end) - this.client.Process(this.getContext(), cmd) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd { + return this.client.LRange(this.getContext(), key, start, end) } /* @@ -118,24 +74,16 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素, count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 count = 0 : 移除表中所有与 VALUE 相等的值 */ -func (this *Redis) LRem(key string, count int, target interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(target); err == nil { - err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err() - } - return +func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { + return this.client.LRem(this.getContext(), key, count, v).Result() } /* -Redis Lset 通过索引来设置元素的值。 +Redis LSet 通过索引来设置元素的值。 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 */ -func (this *Redis) LSet(key string, index int, value interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err() - } - return +func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { + return this.client.LSet(this.getContext(), key, index, v).Result() } /* @@ -143,35 +91,22 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Ltrim(key string, start, stop int) (err error) { - err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() - return +func (this *Redis) LTrim(key string, start, stop int64) (string, error) { + return this.client.LTrim(this.getContext(), key, start, stop).Result() } /* Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 */ -func (this *Redis) Rpop(key string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) RPop(key string, v interface{}) (err error) { + return this.client.RPop(this.getContext(), key).Scan(v) } /* Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 */ -func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) { + return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v) } /* @@ -179,27 +114,13 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边) 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 */ -func (this *Redis) RPush(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "RPUSH") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { + return this.client.RPush(this.getContext(), key, values...).Result() } /* Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 */ -func (this *Redis) RPushX(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "RPUSHX") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { + return this.client.RPushX(this.getContext(), key, values...).Result() } diff --git a/lego/sys/redis/cluster/set.go b/lego/sys/redis/cluster/set.go index 74d5561f6..64a59cb3e 100644 --- a/lego/sys/redis/cluster/set.go +++ b/lego/sys/redis/cluster/set.go @@ -1,30 +1,23 @@ package cluster -import "reflect" +import ( + "github.com/go-redis/redis/v8" +) /* Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 当集合 key 不是集合类型时,返回一个错误。 */ -func (this *Redis) SAdd(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "SADD") - agrs = append(agrs, key) - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { + return this.client.SAdd(this.getContext(), key, values...).Result() } /* Redis Scard 命令返回集合中元素的数量 */ -func (this *Redis) SCard(key string) (result int64, err error) { - result, err = this.client.SCard(this.getContext(), key).Result() - return +func (this *Redis) SCard(key string) (int64, error) { + return this.client.SCard(this.getContext(), key).Result() } /* @@ -32,79 +25,43 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 实例: */ -func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SDiff(this.getContext(), keys...) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { + return this.client.SDiff(this.getContext(), keys...) } /* Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 */ -func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) { + return this.client.SDiffStore(this.getContext(), destination, keys...).Result() } /* Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 */ -func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SInter(this.getContext(), keys...) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { + return this.client.SInter(this.getContext(), keys...) } /* Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 */ -func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) { + return this.client.SInterStore(this.getContext(), destination, keys...).Result() } /* Redis Sismember 命令判断成员元素是否是集合的成员 */ -func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { - iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() - return +func (this *Redis) SIsMember(key string, value interface{}) (bool, error) { + return this.client.SIsMember(this.getContext(), key, value).Result() } /* Redis Smembers 号召返回集合中的所有成员。 */ -func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SMembers(this.getContext(), key) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SMembers(key string) *redis.StringSliceCmd { + return this.client.SMembers(this.getContext(), key) } /* @@ -114,18 +71,16 @@ SMOVE 是原子性操作。 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 当 source 或 destination 不是集合类型时,返回一个错误。 */ -func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { - result, err = this.client.SMove(this.getContext(), source, destination, member).Result() - return +func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) { + return this.client.SMove(this.getContext(), source, destination, member).Result() } /* Redis Spop命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 */ -func (this *Redis) Spop(key string) (result string, err error) { - result, err = this.client.SPop(this.getContext(), key).Result() - return +func (this *Redis) SPop(key string) (string, error) { + return this.client.SPop(this.getContext(), key).Result() } /* @@ -135,9 +90,8 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 */ -func (this *Redis) Srandmember(key string) (result string, err error) { - result, err = this.client.SRandMember(this.getContext(), key).Result() - return +func (this *Redis) SRandMember(key string) (string, error) { + return this.client.SRandMember(this.getContext(), key).Result() } /* @@ -145,41 +99,27 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在 当键不是集合类型,返回一个错误。 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 */ -func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.SRem(this.getContext(), key, members...).Result() - return +func (this *Redis) SRem(key string, members ...interface{}) (int64, error) { + return this.client.SRem(this.getContext(), key, members...).Result() } /* Redis Sunion 命令返回给定集合的并集。 */ -func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SUnion(this.getContext(), keys...) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { + return this.client.SUnion(this.getContext(), keys...) } /* Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 */ -func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) { + return this.client.SUnionStore(this.getContext(), destination, keys...).Result() } /* Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 */ -func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - keys, cursor, err = this.client.SScan(this.getContext(), key, _cursor, match, count).Result() - return +func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { + return this.client.SScan(this.getContext(), key, _cursor, match, count).Result() } diff --git a/lego/sys/redis/cluster/string.go b/lego/sys/redis/cluster/string.go index 17fc6f1a9..1dee13835 100644 --- a/lego/sys/redis/cluster/string.go +++ b/lego/sys/redis/cluster/string.go @@ -1,7 +1,6 @@ package cluster import ( - "fmt" "time" "github.com/go-redis/redis/v8" @@ -11,54 +10,39 @@ import ( /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 */ -func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { - var result []byte - if result, err = this.Encode(value); err == nil { - err = this.client.Set(this.getContext(), string(key), result, expiration).Err() - } - return +func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { + return this.client.Set(this.getContext(), string(key), v, expiration).Result() } /* 指定的 key 不存在时,为 key 设置指定的值 */ -func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { - // var _value []byte - // if result, err = this.Encode(value); err == nil { - // err = this.client.Do(this.getContext(), "SETNX", key, result).Err() - cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() - // } - return +func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return this.client.SetNX(this.getContext(), key, v, expiration).Result() } /* 同时设置一个或多个 key-value 对 */ -func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSet(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSET") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 */ -func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSetNX(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSETNX") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* @@ -67,9 +51,8 @@ Redis Incr 命令将 key 中储存的数字值增一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内。 */ -func (this *Redis) Incr(key string) (err error) { - err = this.client.Do(this.getContext(), "INCR", key).Err() - return +func (this *Redis) Incr(key string) (int64, error) { + return this.client.Incr(this.getContext(), key).Result() } /* @@ -78,18 +61,16 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) IncrBY(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() - return +func (this *Redis) IncrBY(key string, value int64) (int64, error) { + return this.client.IncrBy(this.getContext(), key, value).Result() } /* Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 */ -func (this *Redis) Incrbyfloat(key string, value float32) (err error) { - err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() - return +func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { + return this.client.IncrByFloat(this.getContext(), key, value).Result() } /* @@ -98,9 +79,8 @@ Redis Decr 命令将 key 中储存的数字值减一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) Decr(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECR", key, value).Err() - return +func (this *Redis) Decr(key string, value int) (int64, error) { + return this.client.Decr(this.getContext(), key).Result() } /* @@ -109,9 +89,8 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) DecrBy(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() - return +func (this *Redis) DecrBy(key string, value int64) (int64, error) { + return this.client.DecrBy(this.getContext(), key, value).Result() } /* @@ -119,62 +98,28 @@ Redis Append 命令用于为指定的 key 追加值。 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 */ -func (this *Redis) Append(key string, value interface{}) (err error) { - var result []byte - if result, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "APPEND", key, result).Err() - } +func (this *Redis) Append(key string, value string) (err error) { + this.client.Append(this.getContext(), key, value).Result() return } /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 */ -func (this *Redis) Get(key string, value interface{}) (err error) { - var result []byte - if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil { - err = this.Decode(result, value) - } - return +func (this *Redis) Get(key string, v interface{}) (err error) { + return this.client.Get(this.getContext(), key).Scan(v) } /* 设置指定 key 的值,并返回 key 的旧值 */ -func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { - var ( - data string - _value []byte - ) - if _value, err = this.Encode(value); err == nil { - if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), result) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - } - return +func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) { + return this.client.GetSet(this.getContext(), key, v).Scan(result) } /* 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil */ -func (this *Redis) MGet(keys ...string) (result []string, err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "MGET") - for _, v := range keys { - agrs = append(agrs, v) - } - cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() - return -} - -///判断是否存在key pattern:key* -func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { - cmd := redis.NewIntCmd(this.getContext(), "INCRBY", key, amount) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() - return +func (this *Redis) MGet(keys ...string) *redis.SliceCmd { + return this.client.MGet(this.getContext(), keys...) } diff --git a/lego/sys/redis/cluster/zset.go b/lego/sys/redis/cluster/zset.go index 206710e22..9d3de4aab 100644 --- a/lego/sys/redis/cluster/zset.go +++ b/lego/sys/redis/cluster/zset.go @@ -1,217 +1,145 @@ package cluster import ( - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 */ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { - this.client.ZAdd(this.getContext(), key, members...) - return +func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { + return this.client.ZAdd(this.getContext(), key, members...).Result() } /* Redis Zcard 用于计算集合中元素的数量。 */ -func (this *Redis) ZCard(key string) (result int64, err error) { - result, err = this.client.ZCard(this.getContext(), key).Result() - return +func (this *Redis) ZCard(key string) (int64, error) { + return this.client.ZCard(this.getContext(), key).Result() } /* Redis ZCount 用于计算集合中指定的范围内的数量 */ -func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZCount(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZCount(key string, min string, max string) (int64, error) { + return this.client.ZCount(this.getContext(), key, min, max).Result() } /* Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment */ -func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { - result, err = this.client.ZIncrBy(this.getContext(), key, increment, member).Result() - return +func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) { + return this.client.ZIncrBy(this.getContext(), key, increment, member).Result() } /* Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 */ -func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() - return +func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) { + return this.client.ZInterStore(this.getContext(), destination, store).Result() } /* Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 */ -func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZLexCount(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) { + return this.client.ZLexCount(this.getContext(), key, min, max).Result() } /* Redis ZRange 通过索引区间返回有序集合指定区间内的成员 */ -func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRange(this.getContext(), key, start, stop) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRange(this.getContext(), key, start, stop) } /* Redis ZRangeByLex 通过字典区间返回有序集合的成员 */ -func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRangeByLex(this.getContext(), key, opt) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByLex(this.getContext(), key, opt) } /* Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 */ -func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRangeByScore(this.getContext(), key, opt) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByScore(this.getContext(), key, opt) } /* Redis ZRank 返回有序集合中指定成员的索引 */ -func (this *Redis) ZRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRank(key string, member string) (int64, error) { + return this.client.ZRank(this.getContext(), key, member).Result() } /* Redis ZRem 移除有序集合中的一个或多个成员 */ -func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.ZRem(this.getContext(), key, members...).Result() - return +func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) { + return this.client.ZRem(this.getContext(), key, members...).Result() } /* Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 */ -func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() } /* Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 */ -func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { - result, err = this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() - return +func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) { + return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() } /* Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 */ -func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() } /* Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE */ -func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRevRange(this.getContext(), key, start, stop) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRevRange(this.getContext(), key, start, stop) } /* Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRevRangeByScore(this.getContext(), key, opt) } /* Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRevRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRevRank(key string, member string) (int64, error) { + return this.client.ZRevRank(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZScore(key string, member string) (result float64, err error) { - result, err = this.client.ZScore(this.getContext(), key, member).Result() - return +func (this *Redis) ZScore(key string, member string) (float64, error) { + return this.client.ZScore(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE */ -func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() - return +func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) { + return this.client.ZUnionStore(this.getContext(), dest, store).Result() } /* Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) */ -func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - keys, cursor, err = this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() - return +func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { + return this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() } diff --git a/lego/sys/redis/core.go b/lego/sys/redis/core.go index 90af5e2c7..74d555214 100644 --- a/lego/sys/redis/core.go +++ b/lego/sys/redis/core.go @@ -2,7 +2,6 @@ package redis import ( "context" - "reflect" "time" "github.com/go-redis/redis/v8" @@ -18,99 +17,382 @@ type ( TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) /*Key*/ - Delete(key string) (err error) - ExistsKey(key string) (iskeep bool, err error) - ExpireKey(key string, expire int) (err error) - ExpireatKey(key string, expire_unix int64) (err error) - Pexpirekey(key string, expire int) (err error) - PexpireatKey(key string, expire_unix int64) (err error) - PersistKey(key string) (err error) - PttlKey(key string) (leftexpire int64, err error) - TtlKey(key string) (leftexpire int64, err error) - RenameKye(oldkey string, newkey string) (err error) - RenamenxKey(oldkey string, newkey string) (err error) + ///删除redis key + Delete(key ...string) (int64, error) + ///判断是否存在key + Exists(key ...string) (int64, error) + ///设置key的过期时间 单位以秒级 + Expire(key string, expire time.Duration) (bool, error) + ///设置key的过期时间戳 秒级时间戳 + ExpireAt(key string, expire time.Time) (bool, error) + ///设置key的过期时间 单位以毫秒级 + PExpire(key string, expire time.Duration) (bool, error) + ///设置key的过期时间戳 单位以豪秒级 + PExpireAt(key string, expire time.Time) (bool, error) + ///移除Key的过期时间 + Persist(key string) (bool, error) + ///获取key剩余过期时间 单位毫秒 + PTTL(key string) (surplus time.Duration, err error) + ///获取key剩余过期时间 单位秒 + TTL(key string) (surplus time.Duration, err error) + ///重命名Key + Rename(oldkey string, newkey string) (string, error) + ///重命名key 在新的 key 不存在时修改 key 的名称 + RenameNX(oldkey string, newkey string) (bool, error) + ///判断是否存在key pattern:key* Keys(pattern string) (keys []string, err error) + ///获取键类型 Type(key string) (ty string, err error) /*String*/ - Set(key string, value interface{}, expiration time.Duration) (err error) - SetNX(key string, value interface{}) (result int64, err error) - MSet(keyvalues map[string]interface{}) (err error) + /* + 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 + */ + Set(key string, v interface{}, expiration time.Duration) (string, error) + /* + 指定的 key 不存在时,为 key 设置指定的值 + */ + SetNX(key string, v interface{}, expiration time.Duration) (bool, error) + /* + 同时设置一个或多个 key-value 对 + */ + MSet(keyvalues map[string]interface{}) error + /* + 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 + */ MSetNX(keyvalues map[string]interface{}) (err error) - Incr(key string) (err error) - IncrBY(key string, value int) (err error) - Incrbyfloat(key string, value float32) (err error) - Decr(key string, value int) (err error) - DecrBy(key string, value int) (err error) - Append(key string, value interface{}) (err error) - Get(key string, value interface{}) (err error) - GetSet(key string, value interface{}, result interface{}) (err error) - MGet(keys ...string) (result []string, err error) - INCRBY(key string, amount int64) (result int64, err error) + /* + Redis Incr 命令将 key 中储存的数字值增一。 + 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。 + 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 + 本操作的值限制在 64 位(bit)有符号数字表示之内。 + */ + Incr(key string) (int64, error) + /* + Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 + 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。 + 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 + 本操作的值限制在 64 位(bit)有符号数字表示之内 + */ + IncrBY(key string, value int64) (int64, error) + /* + Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 + 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 + */ + Incrbyfloat(key string, value float64) (float64, error) + /* + Redis Decr 命令将 key 中储存的数字值减一。 + 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作。 + 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 + 本操作的值限制在 64 位(bit)有符号数字表示之内 + */ + Decr(key string, value int) (int64, error) + /* + Redis Decrby 命令将 key 所储存的值减去指定的减量值。 + 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。 + 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 + 本操作的值限制在 64 位(bit)有符号数字表示之内 + */ + DecrBy(key string, value int64) (int64, error) + /* + Redis Append 命令用于为指定的 key 追加值。 + 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 + 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 + */ + Append(key string, value string) (err error) + /* + 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 + */ + Get(key string, v interface{}) (err error) + /* + 设置指定 key 的值,并返回 key 的旧值 + */ + GetSet(key string, v interface{}, result interface{}) (err error) + /* + 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil + */ + MGet(keys ...string) *redis.SliceCmd /*List*/ - Lindex(key string, value interface{}) (err error) - Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) - Llen(key string) (result int, err error) - LPop(key string, value interface{}) (err error) - LPush(key string, values ...interface{}) (err error) - LPushX(key string, values ...interface{}) (err error) - LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) - LRem(key string, count int, target interface{}) (err error) - LSet(key string, index int, value interface{}) (err error) - Ltrim(key string, start, stop int) (err error) - Rpop(key string, value interface{}) (err error) - RPopLPush(oldkey string, newkey string, value interface{}) (err error) - RPush(key string, values ...interface{}) (err error) - RPushX(key string, values ...interface{}) (err error) + /* + Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 + */ + Lindex(key string, index int64, v interface{}) (err error) + /* + Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。 + 当列表不存在时,被视为空列表,不执行任何操作。 + 如果 key 不是列表类型,返回一个错误 + */ + Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) + /* + Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 + */ + Llen(key string) (int64, error) + /* + Redis Lpop 命令用于移除并返回列表的第一个元素 + */ + LPop(key string, v interface{}) (err error) + /* + Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 + */ + LPush(key string, values ...interface{}) (int64, error) + /* + Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 + */ + LPushX(key string, values ...interface{}) (int64, error) + /* + Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, + 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 + */ + LRange(key string, start, end int64) *redis.StringSliceCmd + /* + Redis Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。 + COUNT 的值可以是以下几种: + count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。 + count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 + count = 0 : 移除表中所有与 VALUE 相等的值 + */ + LRem(key string, count int64, v interface{}) (int64, error) + /* + Redis Lset 通过索引来设置元素的值。 + 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 + */ + LSet(key string, index int64, v interface{}) (string, error) + /* + Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 + 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, + 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 + */ + LTrim(key string, start, stop int64) (string, error) + /* + Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 + */ + RPop(key string, v interface{}) (err error) + /* + Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 + */ + RPopLPush(oldkey string, newkey string, v interface{}) (err error) + /* + Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。 + 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 + 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 + */ + RPush(key string, values ...interface{}) (int64, error) + /* + Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 + */ + RPushX(key string, values ...interface{}) (int64, error) /*Hash*/ + ///Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略 HDel(key string, fields ...string) (err error) + ///Hexists 命令用于查看哈希表的指定字段是否存在 HExists(key string, field string) (result bool, err error) - HGet(key string, field string, value interface{}) (err error) - HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) - HIncrBy(key string, field string, value int) (err error) - HIncrByFloat(key string, field string, value float32) (err error) + ///Hget 命令用于返回哈希表中指定字段的值 + HGet(key string, field string, v interface{}) (err error) + ///Hgetall 命令用于返回哈希表中,所有的字段和值。 + ///在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍 + HGetAll(key string, v interface{}) (err error) + /* + Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 + 增量也可以为负数,相当于对指定字段进行减法操作。 + 如果哈希表的 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。 + 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。 + 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 + 本操作的值被限制在 64 位(bit)有符号数字表示之内 + */ + HIncrBy(key string, field string, value int64) (err error) + /* + Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 + 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 + */ + HIncrByFloat(key string, field string, value float64) (err error) + /* + Redis Hkeys 命令用于获取哈希表中的所有域(field) + */ Hkeys(key string) (result []string, err error) - Hlen(key string) (result int, err error) - HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) + /* + Redis Hlen 命令用于获取哈希表中字段的数量 + */ + Hlen(key string) (result int64, err error) + /* + Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 + 如果指定的字段不存在于哈希表,那么返回一个 nil 值 + */ + HMGet(key string, fields ...string) *redis.SliceCmd + /* + Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。 + 此命令会覆盖哈希表中已存在的字段。 + 如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作 + */ HMSet(key string, value map[string]interface{}) (err error) + /* + Redis Hset 命令用于为哈希表中的字段赋值 + 如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作 + 如果字段已经存在于哈希表中,旧值将被覆盖 + */ HSet(key string, field string, value interface{}) (err error) + /* + Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 + 如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作 + 如果字段已经存在于哈希表中,操作无效 + 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令 + */ HSetNX(key string, field string, value interface{}) (err error) /*Set*/ - SAdd(key string, values ...interface{}) (err error) - SCard(key string) (result int64, err error) - SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) - SDiffStore(destination string, keys ...string) (result int64, err error) - SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) - SInterStore(destination string, keys ...string) (result int64, err error) - Sismember(key string, value interface{}) (iskeep bool, err error) - SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) - SMove(source string, destination string, member interface{}) (result bool, err error) - Spop(key string) (result string, err error) - Srandmember(key string) (result string, err error) - SRem(key string, members ...interface{}) (result int64, err error) - SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) - Sunionstore(destination string, keys ...string) (result int64, err error) - Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) + /* + Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 + 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 + 当集合 key 不是集合类型时,返回一个错误。 + */ + SAdd(key string, values ...interface{}) (int64, error) + /* + Redis Scard 命令返回集合中元素的数量 + */ + SCard(key string) (int64, error) + /* + Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可以认为说第一个集合中独有的元素。不存在的集合 key 将视为空集。 + 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 + 实例: + */ + SDiff(keys ...string) *redis.StringSliceCmd + /* + Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 + */ + SDiffStore(destination string, keys ...string) (int64, error) + /* + Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 + */ + SInter(keys ...string) *redis.StringSliceCmd + /* + Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 + */ + SInterStore(destination string, keys ...string) (int64, error) + /* + Redis SIsMember 命令判断成员元素是否是集合的成员 + */ + SIsMember(key string, value interface{}) (bool, error) + /* + Redis Smembers 号召返回集合中的所有成员。 + */ + SMembers(key string) *redis.StringSliceCmd + /* + Redis Smove 命令将指定成员 member 元素从 source 集合移动到 destination 集合。 + SMOVE 是原子性操作。 + 如果 source 集合不存在或不包含指定的 member 元素,则 SMOVE 命令不执行任何操作,仅返回 0 。否则, member 元素从 source 集合中被移除,并添加到 destination 集合中去。 + 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 + 当 source 或 destination 不是集合类型时,返回一个错误。 + */ + SMove(source string, destination string, member interface{}) (bool, error) + /* + Redis SPop 命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 + 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 + */ + SPop(key string) (string, error) + /* + Redis Srandmember 命令用于返回集合中的一个随机元素。 + 从 Redis 2.6 版本开始, Srandmember 命令接受可选的 count 参数: + 如果 count 为正数,且小于集合基数,那么命令返回一个包含 count 个元素的数组,数组中的元素各不相同。如果 count 大于等于集合基数,那么返回整个集合。 + 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 + 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 + */ + SRandMember(key string) (string, error) + /* + Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在的元素元素会被忽略。 + 当键不是集合类型,返回一个错误。 + 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 + */ + SRem(key string, members ...interface{}) (int64, error) + /* + Redis Sunion 命令返回给定集合的并集。 + */ + SUnion(keys ...string) *redis.StringSliceCmd + /* + Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 + */ + SUnionStore(destination string, keys ...string) (int64, error) + /* + Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 + */ + SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) /*ZSet*/ - ZAdd(key string, members ...*redis.Z) (err error) - ZCard(key string) (result int64, err error) - ZCount(key string, min string, max string) (result int64, err error) - ZIncrBy(key string, increment float64, member string) (result float64, err error) - ZInterStore(destination string, store *redis.ZStore) (result int64, err error) - ZLexCount(key string, min string, max string) (result int64, err error) - ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) - ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) - ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) - ZRank(key string, member string) (result int64, err error) - ZRem(key string, members ...interface{}) (result int64, err error) - ZRemRangeByLex(key string, min string, max string) (result int64, err error) - ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) - ZRemRangeByScore(key string, min string, max string) (result int64, err error) - ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) - ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) - ZRevRank(key string, member string) (result int64, err error) - ZScore(key string, member string) (result float64, err error) - ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) - ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) + /* + Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 + */ + ZAdd(key string, members ...*redis.Z) (int64, error) + /* + Redis Zcard 用于计算集合中元素的数量。 + */ + ZCard(key string) (int64, error) + /* + Redis ZCount 用于计算集合中指定的范围内的数量 + */ + ZCount(key string, min string, max string) (int64, error) + /* + Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment + */ + ZIncrBy(key string, increment float64, member string) (float64, error) + /* + Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 + */ + ZInterStore(destination string, store *redis.ZStore) (int64, error) + /* + Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 + */ + ZLexCount(key string, min string, max string) (int64, error) + /* + Redis ZRange 通过索引区间返回有序集合指定区间内的成员 + */ + ZRange(key string, start int64, stop int64) *redis.StringSliceCmd + /* + Redis ZRangeByLex 通过字典区间返回有序集合的成员 + */ + ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd + /* + Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 + */ + ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd + /* + Redis ZRank 返回有序集合中指定成员的索引 + */ + ZRank(key string, member string) (int64, error) + /* + Redis ZRem 移除有序集合中的一个或多个成员 + */ + ZRem(key string, members ...interface{}) (int64, error) + /* + Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 + */ + ZRemRangeByLex(key string, min string, max string) (int64, error) + /* + Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 + */ + ZRemRangeByRank(key string, start int64, stop int64) (int64, error) + /* + Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 + */ + ZRemRangeByScore(key string, min string, max string) (int64, error) + /* + Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE + */ + ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd + /* + Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 + */ + ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd + /* + Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 + */ + ZRevRank(key string, member string) (int64, error) + /* + Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 + */ + ZScore(key string, member string) (float64, error) + /* + Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE + */ + ZUnionStore(dest string, store *redis.ZStore) (int64, error) + /* + Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) + */ + ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) } ISys interface { @@ -162,55 +444,52 @@ func Encode(value interface{}) (result []byte, err error) { func Decode(value []byte, result interface{}) (err error) { return defsys.Decode(value, result) } -func Delete(key string) (err error) { - return defsys.Delete(key) +func Delete(key ...string) (int64, error) { + return defsys.Delete(key...) } -func ExistsKey(key string) (iskeep bool, err error) { - return defsys.ExistsKey(key) - +func Exists(key ...string) (int64, error) { + return defsys.Exists(key...) } -func ExpireKey(key string, expire int) (err error) { - return defsys.ExpireKey(key, expire) +func Expire(key string, expire time.Duration) (bool, error) { + return defsys.Expire(key, expire) } -func ExpireatKey(key string, expire_unix int64) (err error) { - return defsys.ExpireatKey(key, expire_unix) +func ExpireAt(key string, expire time.Time) (bool, error) { + return defsys.ExpireAt(key, expire) } -func Pexpirekey(key string, expire int) (err error) { - return defsys.Pexpirekey(key, expire) +func PExpire(key string, expire time.Duration) (bool, error) { + return defsys.PExpire(key, expire) } -func PexpireatKey(key string, expire_unix int64) (err error) { - return defsys.PexpireatKey(key, expire_unix) +func PExpireAt(key string, expire time.Time) (bool, error) { + return defsys.PExpireAt(key, expire) } -func PersistKey(key string) (err error) { - return defsys.PersistKey(key) +func Persist(key string) (bool, error) { + return defsys.Persist(key) } -func PttlKey(key string) (leftexpire int64, err error) { - return defsys.PttlKey(key) +func PTTL(key string) (surplus time.Duration, err error) { + return defsys.PTTL(key) } -func TtlKey(key string) (leftexpire int64, err error) { - return defsys.TtlKey(key) +func TTL(key string) (surplus time.Duration, err error) { + return defsys.TTL(key) } -func RenameKye(oldkey string, newkey string) (err error) { - return defsys.RenameKye(oldkey, newkey) +func Rename(oldkey string, newkey string) (string, error) { + return defsys.Rename(oldkey, newkey) } -func RenamenxKey(oldkey string, newkey string) (err error) { - return defsys.RenamenxKey(oldkey, newkey) +func RenameNX(oldkey string, newkey string) (bool, error) { + return defsys.RenameNX(oldkey, newkey) } func Keys(pattern string) (keys []string, err error) { return defsys.Keys(pattern) } - -///获取键类型 func Type(key string) (ty string, err error) { return defsys.Type(key) } /*String*/ -func Set(key string, value interface{}, expiration time.Duration) (err error) { - return defsys.Set(key, value, expiration) +func Set(key string, v interface{}, expiration time.Duration) (string, error) { + return defsys.Set(key, v, expiration) } -func SetNX(key string, value interface{}) (result int64, err error) { - return defsys.SetNX(key, value) +func SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return defsys.SetNX(key, v, expiration) } func MSet(keyvalues map[string]interface{}) (err error) { return defsys.MSet(keyvalues) @@ -218,36 +497,33 @@ func MSet(keyvalues map[string]interface{}) (err error) { func MSetNX(keyvalues map[string]interface{}) (err error) { return defsys.MSetNX(keyvalues) } -func Incr(key string) (err error) { +func Incr(key string) (int64, error) { return defsys.Incr(key) } -func IncrBY(key string, value int) (err error) { +func IncrBY(key string, value int64) (int64, error) { return defsys.IncrBY(key, value) } -func Incrbyfloat(key string, value float32) (err error) { +func Incrbyfloat(key string, value float64) (float64, error) { return defsys.Incrbyfloat(key, value) } -func Decr(key string, value int) (err error) { +func Decr(key string, value int) (int64, error) { return defsys.Decr(key, value) } -func DecrBy(key string, value int) (err error) { +func DecrBy(key string, value int64) (int64, error) { return defsys.DecrBy(key, value) } -func Append(key string, value interface{}) (err error) { +func Append(key string, value string) (err error) { return defsys.Append(key, value) } -func Get(key string, value interface{}) (err error) { - return defsys.Get(key, value) +func Get(key string, v interface{}) (err error) { + return defsys.Get(key, v) } -func GetSet(key string, value interface{}, result interface{}) (err error) { - return defsys.GetSet(key, value, result) +func GetSet(key string, v interface{}, result interface{}) (err error) { + return defsys.GetSet(key, v, result) } -func MGet(keys ...string) (result []string, err error) { +func MGet(keys ...string) *redis.SliceCmd { return defsys.MGet(keys...) } -func INCRBY(key string, amount int64) (result int64, err error) { - return defsys.INCRBY(key, amount) -} /*Lock*/ func NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error) { @@ -262,46 +538,46 @@ func UnLock(key string) (err error) { } /*List*/ -func Lindex(key string, value interface{}) (err error) { - return defsys.Lindex(key, value) +func Lindex(key string, index int64, v interface{}) (err error) { + return defsys.Lindex(key, index, v) } -func Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { - return defsys.Linsert(key, isbefore, tager, value) +func Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + return defsys.Linsert(key, isbefore, pivot, value) } -func Llen(key string) (result int, err error) { +func Llen(key string) (int64, error) { return defsys.Llen(key) } func LPop(key string, value interface{}) (err error) { return defsys.LPop(key, value) } -func LPush(key string, values ...interface{}) (err error) { +func LPush(key string, values ...interface{}) (int64, error) { return defsys.LPush(key, values...) } -func LPushX(key string, values ...interface{}) (err error) { +func LPushX(key string, values ...interface{}) (int64, error) { return defsys.LPushX(key, values...) } -func LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { - return defsys.LRange(key, start, end, valuetype) +func LRange(key string, start int64, end int64) *redis.StringSliceCmd { + return defsys.LRange(key, start, end) } -func LRem(key string, count int, target interface{}) (err error) { - return defsys.LRem(key, count, target) +func LRem(key string, count int64, v interface{}) (int64, error) { + return defsys.LRem(key, count, v) } -func LSet(key string, index int, value interface{}) (err error) { - return defsys.LSet(key, index, value) +func LSet(key string, index int64, v interface{}) (string, error) { + return defsys.LSet(key, index, v) } -func Ltrim(key string, start, stop int) (err error) { - return defsys.Ltrim(key, start, stop) +func LTrim(key string, start int64, stop int64) (string, error) { + return defsys.LTrim(key, start, stop) } -func Rpop(key string, value interface{}) (err error) { - return defsys.Rpop(key, value) +func RPop(key string, v interface{}) (err error) { + return defsys.RPop(key, v) } func RPopLPush(oldkey string, newkey string, value interface{}) (err error) { return defsys.RPopLPush(oldkey, newkey, value) } -func RPush(key string, values ...interface{}) (err error) { +func RPush(key string, values ...interface{}) (int64, error) { return defsys.RPush(key, values...) } -func RPushX(key string, values ...interface{}) (err error) { +func RPushX(key string, values ...interface{}) (int64, error) { return defsys.RPushX(key, values...) } @@ -315,23 +591,23 @@ func HExists(key string, field string) (result bool, err error) { func HGet(key string, field string, value interface{}) (err error) { return defsys.HGet(key, field, value) } -func HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { - return defsys.HGetAll(key, valuetype) +func HGetAll(key string, v interface{}) (err error) { + return defsys.HGetAll(key, v) } -func HIncrBy(key string, field string, value int) (err error) { +func HIncrBy(key string, field string, value int64) (err error) { return defsys.HIncrBy(key, field, value) } -func HIncrByFloat(key string, field string, value float32) (err error) { +func HIncrByFloat(key string, field string, value float64) (err error) { return defsys.HIncrByFloat(key, field, value) } func Hkeys(key string) (result []string, err error) { return defsys.Hkeys(key) } -func Hlen(key string) (result int, err error) { +func Hlen(key string) (result int64, err error) { return defsys.Hlen(key) } -func HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { - return defsys.HMGet(key, valuetype, fields...) +func HMGet(key string, fields ...string) *redis.SliceCmd { + return defsys.HMGet(key, fields...) } func HMSet(key string, value map[string]interface{}) (err error) { return defsys.HMSet(key, value) @@ -344,54 +620,54 @@ func HSetNX(key string, field string, value interface{}) (err error) { } /*Set*/ -func SAdd(key string, values ...interface{}) (err error) { +func SAdd(key string, values ...interface{}) (int64, error) { return defsys.SAdd(key, values...) } func SCard(key string) (result int64, err error) { return defsys.SCard(key) } -func SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return defsys.SDiff(valuetype, keys...) +func SDiff(keys ...string) *redis.StringSliceCmd { + return defsys.SDiff(keys...) } func SDiffStore(destination string, keys ...string) (result int64, err error) { return defsys.SDiffStore(destination, keys...) } -func SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return defsys.SInter(valuetype, keys...) +func SInter(keys ...string) *redis.StringSliceCmd { + return defsys.SInter(keys...) } func SInterStore(destination string, keys ...string) (result int64, err error) { return defsys.SInterStore(destination, keys...) } -func Sismember(key string, value interface{}) (iskeep bool, err error) { - return defsys.Sismember(key, value) +func SIsMember(key string, value interface{}) (iskeep bool, err error) { + return defsys.SIsMember(key, value) } -func SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { - return defsys.SMembers(valuetype, key) +func SMembers(key string) *redis.StringSliceCmd { + return defsys.SMembers(key) } func SMove(source string, destination string, member interface{}) (result bool, err error) { return defsys.SMove(source, destination, member) } -func Spop(key string) (result string, err error) { - return defsys.Spop(key) +func SPop(key string) (result string, err error) { + return defsys.SPop(key) } -func Srandmember(key string) (result string, err error) { - return defsys.Srandmember(key) +func SRandMember(key string) (string, error) { + return defsys.SRandMember(key) } func SRem(key string, members ...interface{}) (result int64, err error) { return defsys.SRem(key, members...) } -func SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return defsys.SUnion(valuetype, keys...) +func SUnion(keys ...string) *redis.StringSliceCmd { + return defsys.SUnion(keys...) } -func Sunionstore(destination string, keys ...string) (result int64, err error) { - return defsys.Sunionstore(destination, keys...) +func SUnionStore(destination string, keys ...string) (int64, error) { + return defsys.SUnionStore(destination, keys...) } -func Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - return defsys.Sscan(key, _cursor, match, count) +func SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { + return defsys.SScan(key, _cursor, match, count) } /*ZSet*/ -func ZAdd(key string, members ...*redis.Z) (err error) { +func ZAdd(key string, members ...*redis.Z) (int64, error) { return defsys.ZAdd(key, members...) } func ZCard(key string) (result int64, err error) { @@ -409,14 +685,14 @@ func ZInterStore(destination string, store *redis.ZStore) (result int64, err err func ZLexCount(key string, min string, max string) (result int64, err error) { return defsys.ZLexCount(key, min, max) } -func ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return defsys.ZRange(valuetype, key, start, stop) +func ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return defsys.ZRange(key, start, stop) } -func ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return defsys.ZRangeByLex(valuetype, key, opt) +func ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return defsys.ZRangeByLex(key, opt) } -func ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return defsys.ZRangeByScore(valuetype, key, opt) +func ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return defsys.ZRangeByScore(key, opt) } func ZRank(key string, member string) (result int64, err error) { return defsys.ZRank(key, member) @@ -433,11 +709,11 @@ func ZRemRangeByRank(key string, start int64, stop int64) (result int64, err err func ZRemRangeByScore(key string, min string, max string) (result int64, err error) { return defsys.ZRemRangeByScore(key, min, max) } -func ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return defsys.ZRevRange(valuetype, key, start, stop) +func ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return defsys.ZRevRange(key, start, stop) } -func ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return defsys.ZRevRangeByScore(valuetype, key, opt) +func ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return defsys.ZRevRangeByScore(key, opt) } func ZRevRank(key string, member string) (result int64, err error) { return defsys.ZRevRank(key, member) diff --git a/lego/sys/redis/redis.go b/lego/sys/redis/redis.go index af1e22f0c..b5f0d791a 100644 --- a/lego/sys/redis/redis.go +++ b/lego/sys/redis/redis.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "reflect" "time" "go_dreamfactory/lego/sys/redis/cluster" @@ -99,40 +98,40 @@ func (this *Redis) Decode(value []byte, result interface{}) (err error) { return } -func (this *Redis) Delete(key string) (err error) { - return this.client.Delete(key) +func (this *Redis) Delete(key ...string) (int64, error) { + return this.client.Delete(key...) } -func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { - return this.client.ExistsKey(key) +func (this *Redis) Exists(key ...string) (int64, error) { + return this.client.Exists(key...) } -func (this *Redis) ExpireKey(key string, expire int) (err error) { - return this.client.ExpireKey(key, expire) +func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { + return this.client.Expire(key, expire) } -func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { - return this.client.ExpireatKey(key, expire_unix) +func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { + return this.client.ExpireAt(key, expire) } -func (this *Redis) Pexpirekey(key string, expire int) (err error) { - return this.client.Pexpirekey(key, expire) +func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { + return this.client.PExpire(key, expire) } -func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { - return this.client.PexpireatKey(key, expire_unix) +func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { + return this.client.PExpireAt(key, expire) } -func (this *Redis) PersistKey(key string) (err error) { - return this.client.PersistKey(key) +func (this *Redis) Persist(key string) (bool, error) { + return this.client.Persist(key) } -func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { - return this.client.PttlKey(key) +func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { + return this.client.PTTL(key) } -func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { - return this.client.TtlKey(key) +func (this *Redis) TTL(key string) (surplus time.Duration, err error) { + return this.client.TTL(key) } -func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { - return this.client.RenameKye(oldkey, newkey) +func (this *Redis) Rename(oldkey string, newkey string) (string, error) { + return this.client.Rename(oldkey, newkey) } -func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { - return this.client.RenamenxKey(oldkey, newkey) +func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { + return this.client.RenameNX(oldkey, newkey) } func (this *Redis) Keys(pattern string) (keys []string, err error) { return this.client.Keys(pattern) @@ -144,11 +143,11 @@ func (this *Redis) Type(key string) (ty string, err error) { } /*String*/ -func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { - return this.client.Set(key, value, expiration) +func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { + return this.client.Set(key, v, expiration) } -func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { - return this.client.SetNX(key, value) +func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return this.client.SetNX(key, v, expiration) } func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { return this.client.MSet(keyvalues) @@ -156,22 +155,22 @@ func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { return this.client.MSetNX(keyvalues) } -func (this *Redis) Incr(key string) (err error) { +func (this *Redis) Incr(key string) (int64, error) { return this.client.Incr(key) } -func (this *Redis) IncrBY(key string, value int) (err error) { +func (this *Redis) IncrBY(key string, value int64) (int64, error) { return this.client.IncrBY(key, value) } -func (this *Redis) Incrbyfloat(key string, value float32) (err error) { +func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { return this.client.Incrbyfloat(key, value) } -func (this *Redis) Decr(key string, value int) (err error) { +func (this *Redis) Decr(key string, value int) (int64, error) { return this.client.Decr(key, value) } -func (this *Redis) DecrBy(key string, value int) (err error) { +func (this *Redis) DecrBy(key string, value int64) (int64, error) { return this.client.DecrBy(key, value) } -func (this *Redis) Append(key string, value interface{}) (err error) { +func (this *Redis) Append(key string, value string) (err error) { return this.client.Append(key, value) } func (this *Redis) Get(key string, value interface{}) (err error) { @@ -180,54 +179,51 @@ func (this *Redis) Get(key string, value interface{}) (err error) { func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { return this.client.GetSet(key, value, result) } -func (this *Redis) MGet(keys ...string) (result []string, err error) { +func (this *Redis) MGet(keys ...string) *redis.SliceCmd { return this.client.MGet(keys...) } -func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { - return this.client.INCRBY(key, amount) -} /*List*/ -func (this *Redis) Lindex(key string, value interface{}) (err error) { - return this.client.Lindex(key, value) +func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { + return this.client.Lindex(key, index, v) } -func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { - return this.client.Linsert(key, isbefore, tager, value) +func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + return this.client.Linsert(key, isbefore, pivot, value) } -func (this *Redis) Llen(key string) (result int, err error) { +func (this *Redis) Llen(key string) (int64, error) { return this.client.Llen(key) } func (this *Redis) LPop(key string, value interface{}) (err error) { return this.client.LPop(key, value) } -func (this *Redis) LPush(key string, values ...interface{}) (err error) { +func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { return this.client.LPush(key, values...) } -func (this *Redis) LPushX(key string, values ...interface{}) (err error) { +func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { return this.client.LPushX(key, values...) } -func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { - return this.client.LRange(key, start, end, valuetype) +func (this *Redis) LRange(key string, start int64, end int64) *redis.StringSliceCmd { + return this.client.LRange(key, start, end) } -func (this *Redis) LRem(key string, count int, target interface{}) (err error) { - return this.client.LRem(key, count, target) +func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { + return this.client.LRem(key, count, v) } -func (this *Redis) LSet(key string, index int, value interface{}) (err error) { - return this.client.LSet(key, index, value) +func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { + return this.client.LSet(key, index, v) } -func (this *Redis) Ltrim(key string, start, stop int) (err error) { - return this.client.Ltrim(key, start, stop) +func (this *Redis) LTrim(key string, start int64, stop int64) (string, error) { + return this.client.LTrim(key, start, stop) } -func (this *Redis) Rpop(key string, value interface{}) (err error) { - return this.client.Rpop(key, value) +func (this *Redis) RPop(key string, value interface{}) (err error) { + return this.client.RPop(key, value) } func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { return this.client.RPopLPush(oldkey, newkey, value) } -func (this *Redis) RPush(key string, values ...interface{}) (err error) { +func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { return this.client.RPush(key, values...) } -func (this *Redis) RPushX(key string, values ...interface{}) (err error) { +func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { return this.client.RPushX(key, values...) } @@ -241,23 +237,23 @@ func (this *Redis) HExists(key string, field string) (result bool, err error) { func (this *Redis) HGet(key string, field string, value interface{}) (err error) { return this.client.HGet(key, field, value) } -func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { - return this.client.HGetAll(key, valuetype) +func (this *Redis) HGetAll(key string, v interface{}) (err error) { + return this.client.HGetAll(key, v) } -func (this *Redis) HIncrBy(key string, field string, value int) (err error) { +func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { return this.client.HIncrBy(key, field, value) } -func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { +func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { return this.client.HIncrByFloat(key, field, value) } func (this *Redis) Hkeys(key string) (result []string, err error) { return this.client.Hkeys(key) } -func (this *Redis) Hlen(key string) (result int, err error) { +func (this *Redis) Hlen(key string) (result int64, err error) { return this.client.Hlen(key) } -func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { - return this.client.HMGet(key, valuetype, fields...) +func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd { + return this.client.HMGet(key, fields...) } func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) { return this.client.HMSet(key, value) @@ -270,54 +266,54 @@ func (this *Redis) HSetNX(key string, field string, value interface{}) (err erro } /*Set*/ -func (this *Redis) SAdd(key string, values ...interface{}) (err error) { +func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { return this.client.SAdd(key, values...) } func (this *Redis) SCard(key string) (result int64, err error) { return this.client.SCard(key) } -func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return this.client.SDiff(valuetype, keys...) +func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { + return this.client.SDiff(keys...) } func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { return this.client.SDiffStore(destination, keys...) } -func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return this.client.SInter(valuetype, keys...) +func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { + return this.client.SInter(keys...) } func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { return this.client.SInterStore(destination, keys...) } -func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { - return this.client.Sismember(key, value) +func (this *Redis) SIsMember(key string, value interface{}) (iskeep bool, err error) { + return this.client.SIsMember(key, value) } -func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { - return this.client.SMembers(valuetype, key) +func (this *Redis) SMembers(key string) *redis.StringSliceCmd { + return this.client.SMembers(key) } func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { return this.client.SMove(source, destination, member) } -func (this *Redis) Spop(key string) (result string, err error) { - return this.client.Spop(key) +func (this *Redis) SPop(key string) (string, error) { + return this.client.SPop(key) } -func (this *Redis) Srandmember(key string) (result string, err error) { - return this.client.Srandmember(key) +func (this *Redis) SRandMember(key string) (string, error) { + return this.client.SRandMember(key) } func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { return this.client.SRem(key, members...) } -func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return this.client.SUnion(valuetype, keys...) +func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { + return this.client.SUnion(keys...) } -func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { - return this.client.Sunionstore(destination, keys...) +func (this *Redis) SUnionStore(destination string, keys ...string) (result int64, err error) { + return this.client.SUnionStore(destination, keys...) } -func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - return this.client.Sscan(key, _cursor, match, count) +func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { + return this.client.SScan(key, _cursor, match, count) } /*ZSet*/ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { +func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { return this.client.ZAdd(key, members...) } func (this *Redis) ZCard(key string) (result int64, err error) { @@ -335,14 +331,14 @@ func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { return this.client.ZLexCount(key, min, max) } -func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return this.client.ZRange(valuetype, key, start, stop) +func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRange(key, start, stop) } -func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return this.client.ZRangeByLex(valuetype, key, opt) +func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByLex(key, opt) } -func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return this.client.ZRangeByScore(valuetype, key, opt) +func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByScore(key, opt) } func (this *Redis) ZRank(key string, member string) (result int64, err error) { return this.client.ZRank(key, member) @@ -359,11 +355,11 @@ func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { return this.client.ZRemRangeByScore(key, min, max) } -func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return this.client.ZRevRange(valuetype, key, start, stop) +func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRevRange(key, start, stop) } -func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return this.client.ZRevRangeByScore(valuetype, key, opt) +func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRevRangeByScore(key, opt) } func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { return this.client.ZRevRank(key, member) diff --git a/lego/sys/redis/single/core.go b/lego/sys/redis/single/core.go index 8ea81ce6e..18f218336 100644 --- a/lego/sys/redis/single/core.go +++ b/lego/sys/redis/single/core.go @@ -87,6 +87,6 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) { //锁 func (this *Redis) UnLock(key string) (err error) { - err = this.Delete(key) + _, err = this.Delete(key) return } diff --git a/lego/sys/redis/single/hash.go b/lego/sys/redis/single/hash.go index 91204da1c..df9e48057 100644 --- a/lego/sys/redis/single/hash.go +++ b/lego/sys/redis/single/hash.go @@ -1,8 +1,6 @@ package single import ( - "reflect" - "github.com/go-redis/redis/v8" ) @@ -10,13 +8,7 @@ import ( Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略 */ func (this *Redis) HDel(key string, fields ...string) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "HDEL") - agrs = append(agrs, key) - for _, v := range fields { - agrs = append(agrs, v) - } - err = this.client.Do(this.getContext(), agrs...).Err() + err = this.client.HDel(this.getContext(), key, fields...).Err() return } @@ -24,18 +16,15 @@ func (this *Redis) HDel(key string, fields ...string) (err error) { Redis Hexists 命令用于查看哈希表的指定字段是否存在 */ func (this *Redis) HExists(key string, field string) (result bool, err error) { - result, err = this.client.Do(this.getContext(), "HEXISTS", key, field).Bool() + this.client.HExists(this.getContext(), key, field) return } /* Redis Hget 命令用于返回哈希表中指定字段的值 */ -func (this *Redis) HGet(key string, field string, value interface{}) (err error) { - var resultvalue string - if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) { - err = this.Decode([]byte(resultvalue), value) - } +func (this *Redis) HGet(key string, field string, v interface{}) (err error) { + err = this.client.HGet(this.getContext(), key, field).Scan(v) return } @@ -43,19 +32,8 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error) Redis Hgetall 命令用于返回哈希表中,所有的字段和值。 在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍 */ -func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) - this.client.Process(this.getContext(), cmd) - var _result []string - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } +func (this *Redis) HGetAll(key string, v interface{}) (err error) { + err = this.client.HGetAll(this.getContext(), key).Scan(v) return } @@ -67,8 +45,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 本操作的值被限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) HIncrBy(key string, field string, value int) (err error) { - err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() +func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { + err = this.client.HIncrBy(this.getContext(), key, field, value).Err() return } @@ -76,8 +54,8 @@ func (this *Redis) HIncrBy(key string, field string, value int) (err error) { Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 */ -func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { - err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() +func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { + err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err() return } @@ -85,17 +63,21 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er Redis Hkeys 命令用于获取哈希表中的所有域(field) */ func (this *Redis) Hkeys(key string) (result []string, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() + var cmd *redis.StringSliceCmd + cmd = this.client.HKeys(this.getContext(), key) + result = cmd.Val() + err = cmd.Err() return } /* Redis Hlen 命令用于获取哈希表中字段的数量 */ -func (this *Redis) Hlen(key string) (result int, err error) { - result, err = this.client.Do(this.getContext(), "HLEN", key).Int() +func (this *Redis) Hlen(key string) (result int64, err error) { + var cmd *redis.IntCmd + cmd = this.client.HLen(this.getContext(), key) + result = cmd.Val() + err = cmd.Err() return } @@ -103,26 +85,8 @@ func (this *Redis) Hlen(key string) (result int, err error) { Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 如果指定的字段不存在于哈希表,那么返回一个 nil 值 */ -func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "HMGET") - agrs = append(agrs, key) - for _, v := range fields { - agrs = append(agrs, v) - } - cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) - this.client.Process(this.getContext(), cmd) - var _result []string - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd { + return this.client.HMGet(this.getContext(), key, fields...) } /* @@ -148,10 +112,7 @@ Redis Hset 命令用于为哈希表中的字段赋值 如果字段已经存在于哈希表中,旧值将被覆盖 */ func (this *Redis) HSet(key string, field string, value interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err() - } + err = this.client.HSet(this.getContext(), key, field, value).Err() return } @@ -162,9 +123,6 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令 */ func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err() - } + err = this.client.HSetNX(this.getContext(), key, field, value).Err() return } diff --git a/lego/sys/redis/single/key.go b/lego/sys/redis/single/key.go index 8d6b46ec5..bc8d07f99 100644 --- a/lego/sys/redis/single/key.go +++ b/lego/sys/redis/single/key.go @@ -1,83 +1,69 @@ package single import ( - "github.com/go-redis/redis/v8" + "time" ) /* Key *******************************************************************************/ ///删除redis key -func (this *Redis) Delete(key string) (err error) { - err = this.client.Do(this.getContext(), "DEL", key).Err() - return +func (this *Redis) Delete(key ...string) (int64, error) { + return this.client.Del(this.getContext(), key...).Result() } ///判断是否存在key -func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { - iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() - return +func (this *Redis) Exists(key ...string) (int64, error) { + return this.client.Exists(this.getContext(), key...).Result() } ///设置key的过期时间 单位以秒级 -func (this *Redis) ExpireKey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() - return +func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { + return this.client.Expire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 秒级时间戳 -func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() - return +func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { + return this.client.ExpireAt(this.getContext(), key, expire).Result() } ///设置key的过期时间 单位以毫秒级 -func (this *Redis) Pexpirekey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() - return +func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { + return this.client.PExpire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 单位以豪秒级 -func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() - return +func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { + return this.client.PExpireAt(this.getContext(), key, expire).Result() } ///移除Key的过期时间 -func (this *Redis) PersistKey(key string) (err error) { - err = this.client.Do(this.getContext(), "PERSIST", key).Err() - return +func (this *Redis) Persist(key string) (bool, error) { + return this.client.Persist(this.getContext(), key).Result() } ///获取key剩余过期时间 单位毫秒 -func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() - return +func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { + return this.client.PTTL(this.getContext(), key).Result() } ///获取key剩余过期时间 单位秒 -func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() - return +func (this *Redis) TTL(key string) (surplus time.Duration, err error) { + return this.client.TTL(this.getContext(), key).Result() } ///重命名Key -func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() - return +func (this *Redis) Rename(oldkey string, newkey string) (string, error) { + return this.client.Rename(this.getContext(), oldkey, newkey).Result() } ///重命名key 在新的 key 不存在时修改 key 的名称 -func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() - return +func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { + return this.client.RenameNX(this.getContext(), oldkey, newkey).Result() } ///判断是否存在key pattern:key* func (this *Redis) Keys(pattern string) (keys []string, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) - this.client.Process(this.getContext(), cmd) - keys, err = cmd.Result() - return + return this.client.Keys(this.getContext(), pattern).Result() } ///获取键类型 diff --git a/lego/sys/redis/single/list.go b/lego/sys/redis/single/list.go index 18a0011d8..3adfa9907 100644 --- a/lego/sys/redis/single/list.go +++ b/lego/sys/redis/single/list.go @@ -1,23 +1,14 @@ package single import ( - "fmt" - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Lindex(key string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { + return this.client.LIndex(this.getContext(), key, index).Scan(v) } /* @@ -25,90 +16,55 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { - var ( - tagervalue []byte - resultvalue []byte - ) - if tagervalue, err = this.Encode(tager); err == nil { - if resultvalue, err = this.Encode(value); err == nil { - if isbefore { - err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err() - } else { - err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err() - } - } +func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + op := "BEFORE" + if !isbefore { + op = "AFTER" } - return + return this.client.LInsert(this.getContext(), key, op, pivot, value).Result() } /* Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Llen(key string) (result int, err error) { - result, err = this.client.Do(this.getContext(), "LLEN", key).Int() - return +func (this *Redis) Llen(key string) (int64, error) { + return this.client.LLen(this.getContext(), key).Result() } /* Redis Lpop 命令用于移除并返回列表的第一个元素 */ -func (this *Redis) LPop(key string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) LPop(key string, v interface{}) (err error) { + return this.client.LPop(this.getContext(), key).Scan(v) } /* Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 */ -func (this *Redis) LPush(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "LPUSH") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { + return this.client.LPush(this.getContext(), key, values...).Result() } /* Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 */ -func (this *Redis) LPushX(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "LPUSHX") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { + // agrs := make([]interface{}, 0) + // agrs = append(agrs, "LPUSHX") + // for _, v := range values { + // result, _ := this.Encode(v) + // agrs = append(agrs, result) + // } + // err = this.client.Do(this.getContext(), agrs...).Err() + return this.client.LPushX(this.getContext(), key, values...).Result() } /* Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { - var _result []string - cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end) - this.client.Process(this.getContext(), cmd) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd { + return this.client.LRange(this.getContext(), key, start, end) } /* @@ -118,24 +74,16 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素, count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 count = 0 : 移除表中所有与 VALUE 相等的值 */ -func (this *Redis) LRem(key string, count int, target interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(target); err == nil { - err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err() - } - return +func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { + return this.client.LRem(this.getContext(), key, count, v).Result() } /* -Redis Lset 通过索引来设置元素的值。 +Redis LSet 通过索引来设置元素的值。 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 */ -func (this *Redis) LSet(key string, index int, value interface{}) (err error) { - var resultvalue []byte - if resultvalue, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err() - } - return +func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { + return this.client.LSet(this.getContext(), key, index, v).Result() } /* @@ -143,35 +91,22 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Ltrim(key string, start, stop int) (err error) { - err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() - return +func (this *Redis) LTrim(key string, start, stop int64) (string, error) { + return this.client.LTrim(this.getContext(), key, start, stop).Result() } /* Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 */ -func (this *Redis) Rpop(key string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) RPop(key string, v interface{}) (err error) { + return this.client.RPop(this.getContext(), key).Scan(v) } /* Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 */ -func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { - var data string - if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), value) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - return +func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) { + return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v) } /* @@ -179,27 +114,13 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边) 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 */ -func (this *Redis) RPush(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "RPUSH") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { + return this.client.RPush(this.getContext(), key, values...).Result() } /* Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 */ -func (this *Redis) RPushX(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "RPUSHX") - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { + return this.client.RPushX(this.getContext(), key, values...).Result() } diff --git a/lego/sys/redis/single/set.go b/lego/sys/redis/single/set.go index 2e8c86d43..beda0d4b2 100644 --- a/lego/sys/redis/single/set.go +++ b/lego/sys/redis/single/set.go @@ -1,30 +1,23 @@ package single -import "reflect" +import ( + "github.com/go-redis/redis/v8" +) /* Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 当集合 key 不是集合类型时,返回一个错误。 */ -func (this *Redis) SAdd(key string, values ...interface{}) (err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "SADD") - agrs = append(agrs, key) - for _, v := range values { - result, _ := this.Encode(v) - agrs = append(agrs, result) - } - err = this.client.Do(this.getContext(), agrs...).Err() - return +func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { + return this.client.SAdd(this.getContext(), key, values...).Result() } /* Redis Scard 命令返回集合中元素的数量 */ -func (this *Redis) SCard(key string) (result int64, err error) { - result, err = this.client.SCard(this.getContext(), key).Result() - return +func (this *Redis) SCard(key string) (int64, error) { + return this.client.SCard(this.getContext(), key).Result() } /* @@ -32,79 +25,43 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 实例: */ -func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SDiff(this.getContext(), keys...) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { + return this.client.SDiff(this.getContext(), keys...) } /* Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 */ -func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) { + return this.client.SDiffStore(this.getContext(), destination, keys...).Result() } /* -Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 +Redis SInter 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 */ -func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SInter(this.getContext(), keys...) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { + return this.client.SInter(this.getContext(), keys...) } /* Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 */ -func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) { + return this.client.SInterStore(this.getContext(), destination, keys...).Result() } /* Redis Sismember 命令判断成员元素是否是集合的成员 */ -func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { - iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() - return +func (this *Redis) SIsMember(key string, value interface{}) (bool, error) { + return this.client.SIsMember(this.getContext(), key, value).Result() } /* Redis Smembers 号召返回集合中的所有成员。 */ -func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SMembers(this.getContext(), key) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SMembers(key string) *redis.StringSliceCmd { + return this.client.SMembers(this.getContext(), key) } /* @@ -114,18 +71,16 @@ SMOVE 是原子性操作。 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 当 source 或 destination 不是集合类型时,返回一个错误。 */ -func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { - result, err = this.client.SMove(this.getContext(), source, destination, member).Result() - return +func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) { + return this.client.SMove(this.getContext(), source, destination, member).Result() } /* Redis Spop命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 */ -func (this *Redis) Spop(key string) (result string, err error) { - result, err = this.client.SPop(this.getContext(), key).Result() - return +func (this *Redis) SPop(key string) (string, error) { + return this.client.SPop(this.getContext(), key).Result() } /* @@ -135,9 +90,8 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 */ -func (this *Redis) Srandmember(key string) (result string, err error) { - result, err = this.client.SRandMember(this.getContext(), key).Result() - return +func (this *Redis) SRandMember(key string) (string, error) { + return this.client.SRandMember(this.getContext(), key).Result() } /* @@ -145,41 +99,27 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在 当键不是集合类型,返回一个错误。 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 */ -func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.SRem(this.getContext(), key, members...).Result() - return +func (this *Redis) SRem(key string, members ...interface{}) (int64, error) { + return this.client.SRem(this.getContext(), key, members...).Result() } /* Redis Sunion 命令返回给定集合的并集。 */ -func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - var _result []string - cmd := this.client.SUnion(this.getContext(), keys...) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { + return this.client.SUnion(this.getContext(), keys...) } /* Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 */ -func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) { + return this.client.SUnionStore(this.getContext(), destination, keys...).Result() } /* Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 */ -func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - keys, cursor, err = this.client.SScan(this.getContext(), key, _cursor, match, count).Result() - return +func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { + return this.client.SScan(this.getContext(), key, _cursor, match, count).Result() } diff --git a/lego/sys/redis/single/string.go b/lego/sys/redis/single/string.go index 468d6026b..be6957d37 100644 --- a/lego/sys/redis/single/string.go +++ b/lego/sys/redis/single/string.go @@ -1,7 +1,6 @@ package single import ( - "fmt" "time" "github.com/go-redis/redis/v8" @@ -11,54 +10,39 @@ import ( /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 */ -func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { - var result []byte - if result, err = this.Encode(value); err == nil { - err = this.client.Set(this.getContext(), string(key), result, expiration).Err() - } - return +func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { + return this.client.Set(this.getContext(), string(key), v, expiration).Result() } /* 指定的 key 不存在时,为 key 设置指定的值 */ -func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { - // var _value []byte - // if result, err = this.Encode(value); err == nil { - // err = this.client.Do(this.getContext(), "SETNX", key, result).Err() - cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() - // } - return +func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return this.client.SetNX(this.getContext(), key, v, expiration).Result() } /* 同时设置一个或多个 key-value 对 */ -func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSet(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSET") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 */ -func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSetNX(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSETNX") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* @@ -67,9 +51,8 @@ Redis Incr 命令将 key 中储存的数字值增一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内。 */ -func (this *Redis) Incr(key string) (err error) { - err = this.client.Do(this.getContext(), "INCR", key).Err() - return +func (this *Redis) Incr(key string) (int64, error) { + return this.client.Incr(this.getContext(), key).Result() } /* @@ -78,18 +61,16 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) IncrBY(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() - return +func (this *Redis) IncrBY(key string, value int64) (int64, error) { + return this.client.IncrBy(this.getContext(), key, value).Result() } /* Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 */ -func (this *Redis) Incrbyfloat(key string, value float32) (err error) { - err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() - return +func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { + return this.client.IncrByFloat(this.getContext(), key, value).Result() } /* @@ -98,9 +79,8 @@ Redis Decr 命令将 key 中储存的数字值减一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) Decr(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECR", key, value).Err() - return +func (this *Redis) Decr(key string, value int) (int64, error) { + return this.client.Decr(this.getContext(), key).Result() } /* @@ -109,9 +89,8 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) DecrBy(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() - return +func (this *Redis) DecrBy(key string, value int64) (int64, error) { + return this.client.DecrBy(this.getContext(), key, value).Result() } /* @@ -119,62 +98,28 @@ Redis Append 命令用于为指定的 key 追加值。 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 */ -func (this *Redis) Append(key string, value interface{}) (err error) { - var result []byte - if result, err = this.Encode(value); err == nil { - err = this.client.Do(this.getContext(), "APPEND", key, result).Err() - } +func (this *Redis) Append(key string, value string) (err error) { + this.client.Append(this.getContext(), key, value).Result() return } /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 */ -func (this *Redis) Get(key string, value interface{}) (err error) { - var result []byte - if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil { - err = this.Decode(result, value) - } - return +func (this *Redis) Get(key string, v interface{}) (err error) { + return this.client.Get(this.getContext(), key).Scan(v) } /* 设置指定 key 的值,并返回 key 的旧值 */ -func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { - var ( - data string - _value []byte - ) - if _value, err = this.Encode(value); err == nil { - if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) { - err = this.Decode([]byte(data), result) - } else { - err = fmt.Errorf(string(redis.Nil)) - } - } - return +func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) { + return this.client.GetSet(this.getContext(), key, v).Scan(result) } /* 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil */ -func (this *Redis) MGet(keys ...string) (result []string, err error) { - agrs := make([]interface{}, 0) - agrs = append(agrs, "MGET") - for _, v := range keys { - agrs = append(agrs, v) - } - cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() - return -} - -///判断是否存在key pattern:key* -func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { - cmd := redis.NewIntCmd(this.getContext(), "INCRBY", key, amount) - this.client.Process(this.getContext(), cmd) - result, err = cmd.Result() - return +func (this *Redis) MGet(keys ...string) *redis.SliceCmd { + return this.client.MGet(this.getContext(), keys...) } diff --git a/lego/sys/redis/single/zset.go b/lego/sys/redis/single/zset.go index cadf0f13e..1bf044e86 100644 --- a/lego/sys/redis/single/zset.go +++ b/lego/sys/redis/single/zset.go @@ -1,217 +1,145 @@ package single import ( - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 */ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { - this.client.ZAdd(this.getContext(), key, members...) - return +func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { + return this.client.ZAdd(this.getContext(), key, members...).Result() } /* Redis Zcard 用于计算集合中元素的数量。 */ -func (this *Redis) ZCard(key string) (result int64, err error) { - result, err = this.client.ZCard(this.getContext(), key).Result() - return +func (this *Redis) ZCard(key string) (int64, error) { + return this.client.ZCard(this.getContext(), key).Result() } /* Redis ZCount 用于计算集合中指定的范围内的数量 */ -func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZCount(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZCount(key string, min string, max string) (int64, error) { + return this.client.ZCount(this.getContext(), key, min, max).Result() } /* Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment */ -func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { - result, err = this.client.ZIncrBy(this.getContext(), key, increment, member).Result() - return +func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) { + return this.client.ZIncrBy(this.getContext(), key, increment, member).Result() } /* Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 */ -func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() - return +func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) { + return this.client.ZInterStore(this.getContext(), destination, store).Result() } /* Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 */ -func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZLexCount(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) { + return this.client.ZLexCount(this.getContext(), key, min, max).Result() } /* Redis ZRange 通过索引区间返回有序集合指定区间内的成员 */ -func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRange(this.getContext(), key, start, stop) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRange(this.getContext(), key, start, stop) } /* Redis ZRangeByLex 通过字典区间返回有序集合的成员 */ -func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRangeByLex(this.getContext(), key, opt) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByLex(this.getContext(), key, opt) } /* Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 */ -func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRangeByScore(this.getContext(), key, opt) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByScore(this.getContext(), key, opt) } /* Redis ZRank 返回有序集合中指定成员的索引 */ -func (this *Redis) ZRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRank(key string, member string) (int64, error) { + return this.client.ZRank(this.getContext(), key, member).Result() } /* Redis ZRem 移除有序集合中的一个或多个成员 */ -func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.ZRem(this.getContext(), key, members...).Result() - return +func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) { + return this.client.ZRem(this.getContext(), key, members...).Result() } /* Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 */ -func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() } /* Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 */ -func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { - result, err = this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() - return +func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) { + return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() } /* Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 */ -func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { - result, err = this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() - return +func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() } /* Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE */ -func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRevRange(this.getContext(), key, start, stop) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRevRange(this.getContext(), key, start, stop) } /* Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - var _result []string - cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt) - if _result, err = cmd.Result(); err == nil { - result = make([]interface{}, len(_result)) - for i, v := range _result { - temp := reflect.New(valuetype.Elem()).Interface() - if err = this.Decode([]byte(v), &temp); err == nil { - result[i] = temp - } - } - } - return +func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRevRangeByScore(this.getContext(), key, opt) } /* Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRevRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRevRank(key string, member string) (int64, error) { + return this.client.ZRevRank(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZScore(key string, member string) (result float64, err error) { - result, err = this.client.ZScore(this.getContext(), key, member).Result() - return +func (this *Redis) ZScore(key string, member string) (float64, error) { + return this.client.ZScore(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE */ -func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() - return +func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) { + return this.client.ZUnionStore(this.getContext(), dest, store).Result() } /* Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) */ -func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - keys, cursor, err = this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() - return +func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { + return this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() } diff --git a/lego/sys/redis/sys_test.go b/lego/sys/redis/sys_test.go index 7d924ce30..9de853ab5 100644 --- a/lego/sys/redis/sys_test.go +++ b/lego/sys/redis/sys_test.go @@ -21,7 +21,7 @@ func Test_SysIPV6(t *testing.T) { return } fmt.Printf("Redis:succ \n") - if err = redis.Set("liwei1dao", 123, -1); err != nil { + if _, err = redis.Set("liwei1dao", 123, -1); err != nil { fmt.Printf("Redis:err:%v \n", err) } } @@ -37,10 +37,10 @@ func Test_Redis_ExpireatKey(t *testing.T) { return } fmt.Printf("Redis:succ \n") - if err = redis.Set("liwei1dao", 123, -1); err != nil { + if _, err = redis.Set("liwei1dao", 123, -1); err != nil { fmt.Printf("Redis:err:%v \n", err) } - if err = redis.ExpireKey("liwei1dao", 120); err != nil { + if _, err = redis.Expire("liwei1dao", time.Second*120); err != nil { fmt.Printf("Redis:err:%v \n", err) } fmt.Printf("Redis:end \n") @@ -66,8 +66,8 @@ func Test_Redis_SetNX(t *testing.T) { wg.Add(20) for i := 0; i < 20; i++ { go func(index int) { - result, err := redis.SetNX("liwei1dao", index) - fmt.Printf("Redis index:%d result:%d err:%v \n", index, result, err) + result, err := redis.SetNX("liwei1dao", index, -1) + fmt.Printf("Redis index:%d result:%v err:%v \n", index, result, err) wg.Done() }(i) } diff --git a/modules/cache_com.go b/modules/cache_com.go new file mode 100644 index 000000000..500a399dd --- /dev/null +++ b/modules/cache_com.go @@ -0,0 +1,26 @@ +package modules + +import ( + "go_dreamfactory/lego/base" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/core/cbase" + "go_dreamfactory/lego/sys/redis" +) + +/* +基础组件 缓存组件 读写缓存数据 +*/ +type MComp_CacheComp struct { + cbase.ModuleCompBase + S base.IRPCXService //rpc服务对象 + M core.IModule //当前业务模块 + Redis redis.ISys +} + +//组件初始化接口 +func (this *MComp_CacheComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.ModuleCompBase.Init(service, module, comp, options) + this.S = service.(base.IRPCXService) + this.M = module + return +} diff --git a/modules/db_comp.go b/modules/db_comp.go new file mode 100644 index 000000000..df3b7627a --- /dev/null +++ b/modules/db_comp.go @@ -0,0 +1,26 @@ +package modules + +import ( + "go_dreamfactory/lego/base" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/core/cbase" + "go_dreamfactory/lego/sys/mgo" +) + +/* +基础组件 存储组件 读写缓存数据 +*/ +type MComp_DBComp struct { + cbase.ModuleCompBase + S base.IRPCXService //rpc服务对象 + M core.IModule //当前业务模块 + DB mgo.ISys +} + +//组件初始化接口 +func (this *MComp_DBComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.ModuleCompBase.Init(service, module, comp, options) + this.S = service.(base.IRPCXService) + this.M = module + return +} diff --git a/modules/pack/api_getlist.go b/modules/pack/api_getlist.go index 4d65750a2..fa871f0e1 100644 --- a/modules/pack/api_getlist.go +++ b/modules/pack/api_getlist.go @@ -5,7 +5,6 @@ import ( "go_dreamfactory/comm" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" - "go_dreamfactory/sys/cache" "time" ) @@ -22,35 +21,38 @@ func (this *Api_Comp) Getlist_Check(ctx context.Context, session comm.IUserSessi func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (err error) { var ( code pb.ErrorCode - pack *pb.DB_UserPackData + items []*pb.DB_UserItemData nt int64 - tempgrids []*pb.DB_GridData - grids []*pb.DB_GridData - modifys []*pb.DB_GridData + tempgrids []*pb.DB_UserItemData + grids []*pb.DB_UserItemData + modifys []*pb.DB_UserItemData + dels []string ) defer func() { session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids}) if code == pb.ErrorCode_Success { go func() { //异步处理修改数据 - cache.Defsys.Pack_UpdateGridToUserPack(session.GetUserId(), modifys...) + this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...) + this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...) }() } }() if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success { return } - if pack, err = cache.Defsys.Pack_QueryUserPack(session.GetUserId()); err != nil { + if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil { log.Errorf("QueryUserPackReq err:%v", err) code = pb.ErrorCode_CacheReadError return } else { - tempgrids = this.module.configure_comp.GetPackItemByType(pack, req.IType) - modifys = make([]*pb.DB_GridData, 0, len(tempgrids)) - grids = make([]*pb.DB_GridData, 0, len(grids)) + tempgrids = this.module.configure_comp.GetPackItemByType(items, req.IType) + modifys = make([]*pb.DB_UserItemData, 0, len(tempgrids)) + dels = make([]string, 0, len(tempgrids)) + grids = make([]*pb.DB_UserItemData, 0, len(grids)) nt = time.Now().Unix() for _, v := range tempgrids { if v.ETime > 0 && v.ETime < nt { //已经过期 - modifys = append(modifys, &pb.DB_GridData{GridId: v.GridId, IsEmpty: true}) + dels = append(dels, v.GridId) } else { grids = append(grids, v) if v.IsNewItem { diff --git a/modules/pack/cache_comp.go b/modules/pack/cache_comp.go index 8aa8b5e03..d9acd20f2 100644 --- a/modules/pack/cache_comp.go +++ b/modules/pack/cache_comp.go @@ -2,29 +2,47 @@ package pack import ( "fmt" - "go_dreamfactory/lego/core/cbase" + "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/lego/sys/redis" + "go_dreamfactory/modules" "go_dreamfactory/pb" - "go_dreamfactory/sys/db" + + "go.mongodb.org/mongo-driver/bson/primitive" ) ///背包缓存数据管理组件 type Cache_Comp struct { - cbase.ModuleCompBase - redis redis.ISys + modules.MComp_CacheComp + module *Pack +} + +//组件初始化接口 +func (this *Cache_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.ModuleCompBase.Init(service, module, comp, options) + this.module = module.(*Pack) + return } ///查询用户背包数据 -func (this *Cache_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{ - UserId: uId, - } - if err = this.redis.Get(fmt.Sprintf(Redis_PackCache, uId), pack); err == nil { +func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) { + var ( + lists []interface{} + temp map[string]interface{} + ) + itmes = make([]*pb.DB_UserItemData, 0) + if err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), &itmes); err == nil { + for i, v := range lists { + itmes[i] = v.(*pb.DB_UserItemData) + } return } else if err == redis.RedisNil { - if pack, err = db.Defsys.Pack_QueryUserPack(uId); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) + if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil { + temp = make(map[string]interface{}) + for _, v := range itmes { + temp[v.GridId] = v + } + this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp) } else if err == mgo.MongodbNil { err = nil } @@ -32,16 +50,67 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData return } +///查询用户指定格子的物品数据 +func (this *Cache_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) { + var ( + itmes []*pb.DB_UserItemData + temp map[string]interface{} + ) + itme = &pb.DB_UserItemData{} + if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil { + return + } else if err == redis.RedisNil { + if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil { + temp = make(map[string]interface{}) + for _, v := range itmes { + temp[v.GridId] = v + } + this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp) + for _, v := range itmes { + if v.GridId == grid { + itme = v + return + } + } + err = fmt.Errorf("no found uid:%s grid:%s", uId, grid) + } else if err == mgo.MongodbNil { + err = nil + } + } + return +} + +//更新用户的背包信息 +func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItemData) (err error) { + temp := make(map[string]interface{}) + for _, v := range itmes { + temp[v.GridId] = v + } + if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil { + this.module.db_comp.Pack_UpdateGridToUserPack(uId, itmes...) + } + + return +} + +//更新用户的背包信息 +func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) { + if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil { + err = this.module.db_comp.Pack_DeleteGridToUserPack(uId, gridIds...) + } + return +} + //查询用户背包物品数量 func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) { var ( - pack *pb.DB_UserPackData - err error + itmes []*pb.DB_UserItemData + err error ) - if pack, err = this.Pack_QueryUserPack(uId); err != nil { + if itmes, err = this.Pack_QueryUserPack(uId); err != nil { return } - for _, v := range pack.Pack { + for _, v := range itmes { if !v.IsEmpty && v.ItemId == itemid { amount += v.Amount } @@ -52,17 +121,17 @@ func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) ( ///添加或则减少物品到用户背包 func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) { var ( - pack *pb.DB_UserPackData - modifys []*pb.DB_GridData + itmes []*pb.DB_UserItemData + modifys []*pb.DB_UserItemData leftnum int64 ) if addnum == 0 { return } - if pack, err = this.Pack_QueryUserPack(uId); err != nil { + if itmes, err = this.Pack_QueryUserPack(uId); err != nil { return } - modifys, leftnum = this.pack_addItemToUserPack(pack, itemId, addnum) + modifys, leftnum = this.pack_addItemToUserPack(itmes, itemId, addnum) if leftnum < 0 { err = ItemNotEnoughError return @@ -70,26 +139,24 @@ func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum err = PackGridNumUpper return } - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } + this.Pack_UpdateUserPack(uId, modifys...) return } ///添加或则减少多个物品到用户背包 func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) { var ( - pack *pb.DB_UserPackData - modifys []*pb.DB_GridData - tempmodifys []*pb.DB_GridData + itmes []*pb.DB_UserItemData + modifys []*pb.DB_UserItemData + tempmodifys []*pb.DB_UserItemData leftnum int64 iskeep bool ) - if pack, err = this.Pack_QueryUserPack(uId); err != nil { + if itmes, err = this.Pack_QueryUserPack(uId); err != nil { return } for k, v := range items { - tempmodifys, leftnum = this.pack_addItemToUserPack(pack, k, v) + tempmodifys, leftnum = this.pack_addItemToUserPack(itmes, k, v) if leftnum < 0 { err = ItemNotEnoughError return @@ -110,36 +177,25 @@ func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int3 } } - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } + this.Pack_UpdateUserPack(uId, modifys...) return } ///修改指定格子的物品数量 -func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid int32, itemId int32, addnum int32) (err error) { +func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) { var ( - pack *pb.DB_UserPackData - grid *pb.DB_GridData + itme *pb.DB_UserItemData + grid *pb.DB_UserItemData num int64 amount int64 ) if addnum == 0 { return } - if pack, err = this.Pack_QueryUserPack(uId); err != nil { + if itme, err = this.Pack_QueryUserPackByGridId(uId, gridid); err != nil { return } - if int32(len(pack.Pack)) <= gridid { - err = NoFoundGirdError - return - } - grid = pack.Pack[gridid] - if grid == nil { - err = NoFoundGirdError - return - } - amount = int64(grid.Amount) + amount = int64(itme.Amount) if grid.IsEmpty { amount = 0 } else if grid.ItemId != itemId { @@ -153,38 +209,15 @@ func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid int32, i err = GirdAmountUpper return } else { - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grid); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } + itme.Amount = uint32(num) + this.Pack_UpdateUserPack(uId, itme) } } return } -///修改目标格子的新获取标识 -func (this *Cache_Comp) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error) { - var ( - pack *pb.DB_UserPackData - ) - if pack, err = db.Defsys.Pack_ModifyPackGridIsNewItem(uId, grids); err == nil { - err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - return -} - -///修改目标格子的新获取标识 -func (this *Cache_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (err error) { - var ( - pack *pb.DB_UserPackData - ) - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grids...); err == nil { - err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - return -} - ///添加移除物品到用户背包 -func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId int32, addnum int32) (modifys []*pb.DB_GridData, leftnum int64) { +func (this *Cache_Comp) pack_addItemToUserPack(items []*pb.DB_UserItemData, itemId int32, addnum int32) (modifys []*pb.DB_UserItemData, leftnum int64) { var ( num int64 isNew bool @@ -194,8 +227,8 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId } isNew = true leftnum = int64(addnum) - modifys = make([]*pb.DB_GridData, 0) - for _, v := range pack.Pack { + modifys = make([]*pb.DB_UserItemData, 0) + for _, v := range items { if !v.IsEmpty && v.ItemId == itemId { isNew = false num = int64(v.Amount) + int64(leftnum) @@ -234,7 +267,7 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId return } if leftnum > 0 { //还没有放完 寻找空的格子填充 - for _, v := range pack.Pack { + for _, v := range items { if v.IsEmpty { if leftnum <= GridCapMaxNum { v.IsEmpty = false @@ -252,30 +285,30 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId } } } - index := int32(len(pack.Pack)) + index := int32(len(items)) for leftnum > 0 { //需要补充格子 if leftnum <= GridCapMaxNum { - grid := &pb.DB_GridData{ - GridId: index, + grid := &pb.DB_UserItemData{ + GridId: primitive.NewObjectID().Hex(), IsEmpty: false, ItemId: itemId, Amount: uint32(leftnum), IsNewItem: isNew, } - pack.Pack = append(pack.Pack, grid) + items = append(items, grid) modifys = append(modifys, grid) leftnum = 0 break } else { leftnum -= GridCapMaxNum - grid := &pb.DB_GridData{ - GridId: index, + grid := &pb.DB_UserItemData{ + GridId: primitive.NewObjectID().Hex(), IsEmpty: false, ItemId: itemId, Amount: uint32(GridCapMaxNum), IsNewItem: isNew, } - pack.Pack = append(pack.Pack, grid) + items = append(items, grid) modifys = append(modifys, grid) index++ } diff --git a/modules/pack/configure_comp.go b/modules/pack/configure_comp.go index 412297770..19e8e3e95 100644 --- a/modules/pack/configure_comp.go +++ b/modules/pack/configure_comp.go @@ -45,8 +45,8 @@ func (this *Configure_Comp) GetItemConfigure(id int32) (item *cfg.Game_itemData, } //获取指定类型的物品列表 -func (this *Configure_Comp) GetPackItemByType(pack *pb.DB_UserPackData, usetype int32) (result []*pb.DB_GridData) { - result = make([]*pb.DB_GridData, 0, len(pack.Pack)) +func (this *Configure_Comp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetype int32) (result []*pb.DB_UserItemData) { + result = make([]*pb.DB_UserItemData, 0, len(itmes)) var ( v interface{} table *cfg.Game_item @@ -59,7 +59,7 @@ func (this *Configure_Comp) GetPackItemByType(pack *pb.DB_UserPackData, usetype return } else { table = v.(*cfg.Game_item) - for _, v := range pack.Pack { + for _, v := range itmes { if !v.IsEmpty { if item, ok = table.GetDataMap()[id]; ok { if item.Usetype == usetype { diff --git a/modules/pack/db_comp.go b/modules/pack/db_comp.go index 1a5fc4c63..0243272c9 100644 --- a/modules/pack/db_comp.go +++ b/modules/pack/db_comp.go @@ -1,73 +1,82 @@ package pack import ( - "fmt" - "go_dreamfactory/lego/core/cbase" - "go_dreamfactory/lego/sys/mgo" + "context" + "go_dreamfactory/modules" "go_dreamfactory/pb" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) ///背包数据库数据管理组件 type DB_Comp struct { - cbase.ModuleCompBase - mgo mgo.ISys -} - -func (this *DB_Comp) Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{UserId: uId, Pack: make([]*pb.DB_GridData, 0)} - _, err = this.mgo.InsertOne(DB_PackTable, pack) - return + modules.MComp_DBComp } ///查询用户背包数据 -func (this *DB_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - err = this.mgo.FindOne(DB_PackTable, bson.M{"_id": uId}).Decode(pack) +func (this *DB_Comp) Pack_QueryUserPack(uId string) (items []*pb.DB_UserItemData, err error) { + var ( + cursor *mongo.Cursor + ) + items = make([]*pb.DB_UserItemData, 0) + if cursor, err = this.DB.Find(DB_PackTable, bson.M{"uid": uId}); err != nil { + return + } else { + for cursor.Next(context.Background()) { + temp := &pb.DB_UserItemData{} + if err = cursor.Decode(temp); err != nil { + return + } + items = append(items, temp) + } + } return } //更新背包格子数据 -func (this *DB_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - update := bson.M{} - for _, v := range grids { - update[fmt.Sprintf("pack.%d.gridid", v.GridId)] = v.GridId - update[fmt.Sprintf("pack.%d.isempty", v.GridId)] = v.IsEmpty - update[fmt.Sprintf("pack.%d.itemid", v.GridId)] = v.ItemId - update[fmt.Sprintf("pack.%d.amount", v.GridId)] = v.Amount - update[fmt.Sprintf("pack.%d.isnewitem", v.GridId)] = v.IsNewItem +func (this *DB_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_UserItemData) (err error) { + models := make([]mongo.WriteModel, len(grids)) + for i, v := range grids { + models[i] = mongo.NewUpdateOneModel().SetFilter(bson.M{"_id": v.GridId}).SetUpdate( + bson.M{"$set": bson.M{ + "isempty": v.IsEmpty, + "itemid": v.ItemId, + "amount": v.Amount, + "ctime": v.CTime, + "etime": v.ETime, + "isnewitem": v.IsNewItem, + }}).SetUpsert(true) } + this.DB.BulkWrite(DB_PackTable, models, options.BulkWrite().SetOrdered(false)) + return +} - err = this.mgo.FindOneAndUpdate(DB_PackTable, - bson.M{"_id": uId}, - bson.M{"$set": update}, - options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack) +//更新背包格子数据 +func (this *DB_Comp) Pack_DeleteGridToUserPack(uId string, grids ...string) (err error) { + _, err = this.DB.DeleteMany(DB_PackTable, bson.M{"_id": bson.M{"$inc": grids}}) return } //更新背包格子物品的数据 -func (this *DB_Comp) Pack_AddGridAmountToUserPack(uId string, grid int32, amount int32) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - err = this.mgo.FindOneAndUpdate(DB_PackTable, - bson.M{"_id": uId}, - bson.M{"$inc": bson.M{ - fmt.Sprintf("pack.%d.amount", grid): amount, +func (this *DB_Comp) Pack_AddGridAmountToUserPack(grid int32, amount int32) (pack *pb.DB_UserItemData, err error) { + pack = &pb.DB_UserItemData{} + err = this.DB.FindOneAndUpdate(DB_PackTable, + bson.M{"_id": grid}, + bson.M{"$set": bson.M{ + "amount": amount, }}, options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack) return } //修改背包格子IsNew标识 -func (this *DB_Comp) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - identifier := []interface{}{bson.M{"item.gridid": bson.M{"$in": grids}}} - err = this.mgo.FindOneAndUpdate(DB_PackTable, - bson.M{"_id": uId}, +func (this *DB_Comp) Pack_ModifyPackGridIsNewItem(grids []int32) (err error) { + _, err = this.DB.UpdateMany(DB_PackTable, + bson.M{"_id": bson.M{"$inc": grids}}, bson.M{"$set": bson.M{ - "pack.$[item].isNewitem": false, - }}, options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{Filters: identifier}).SetReturnDocument(options.After)).Decode(pack) + "isNewitem": false, + }}) return } diff --git a/modules/pack/module.go b/modules/pack/module.go index e170f86c1..43847c076 100644 --- a/modules/pack/module.go +++ b/modules/pack/module.go @@ -3,11 +3,8 @@ package pack import ( "go_dreamfactory/comm" "go_dreamfactory/modules" - "go_dreamfactory/sys/cache" - "go_dreamfactory/sys/db" "go_dreamfactory/lego/core" - "go_dreamfactory/lego/sys/event" "go_dreamfactory/lego/sys/log" ) @@ -24,6 +21,8 @@ func NewModule() core.IModule { type Pack struct { modules.ModuleBase api_comp *Api_Comp //背包模块 协议处理组件 + cache_comp *Cache_Comp //缓存组件 + db_comp *DB_Comp //存储组件 configure_comp *Configure_Comp //背包模块 配置相关接口封装组件 } @@ -35,7 +34,6 @@ func (this *Pack) GetType() core.M_Modules { //模块初始化接口 注册用户创建角色事件 func (this *Pack) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) - event.RegisterGO(comm.Event_CreateUser, this.event_CreateUser) return } @@ -43,6 +41,8 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core. func (this *Pack) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) + this.cache_comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp) + this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp) this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } @@ -50,14 +50,14 @@ func (this *Pack) OnInstallComp() { ///查询用户背包物品数量 func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) { defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount) - amount = cache.Defsys.Pack_QueryUserPackItemAmount(uId, itemid) + amount = this.cache_comp.Pack_QueryUserPackItemAmount(uId, itemid) return } ///添加单个物品到背包 (可以加物品和减物品) func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) { defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil) - if err = cache.Defsys.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { + if err = this.cache_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err) } return @@ -66,16 +66,10 @@ func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error ///添加多个物品到背包 (可以加物品和减物品) func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) { defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil) - if err = cache.Defsys.Pack_AddItemsToUserPack(uId, items); err != nil { + if err = this.cache_comp.Pack_AddItemsToUserPack(uId, items); err != nil { log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err) } return } //Evens-------------------------------------------------------------------------------------------------------------------------------- -//接收玩家创建角色事件 -func (this *Pack) event_CreateUser(uid string) { - if _, err := db.Defsys.Pack_InitUserPack(uid); err != nil { - log.Errorf("event_CreateUser err:%v", err) - } -} diff --git a/pb/pack_db.pb.go b/pb/pack_db.pb.go index dce5c063f..235c28ad7 100644 --- a/pb/pack_db.pb.go +++ b/pb/pack_db.pb.go @@ -21,22 +21,23 @@ const ( ) //背包格子 -type DB_GridData struct { +type DB_UserItemData struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - GridId int32 `protobuf:"varint,1,opt,name=GridId,proto3" json:"GridId,omitempty"` //背包格子Id 所在背包数组的下表 - IsEmpty bool `protobuf:"varint,2,opt,name=IsEmpty,proto3" json:"IsEmpty,omitempty"` //是否是空格子 - ItemId int32 `protobuf:"varint,3,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id - Amount uint32 `protobuf:"varint,4,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量 - CTime int64 `protobuf:"varint,5,opt,name=CTime,proto3" json:"CTime,omitempty"` //物品获取时间 - ETime int64 `protobuf:"varint,6,opt,name=ETime,proto3" json:"ETime,omitempty"` //物品过期时间 - IsNewItem bool `protobuf:"varint,7,opt,name=IsNewItem,proto3" json:"IsNewItem,omitempty"` //是否是新的 + GridId string `protobuf:"bytes,1,opt,name=GridId,proto3" json:"GridId,omitempty"` //背包格子Id + UId string `protobuf:"bytes,2,opt,name=UId,proto3" json:"UId,omitempty"` //用户id + IsEmpty bool `protobuf:"varint,3,opt,name=IsEmpty,proto3" json:"IsEmpty,omitempty"` //是否是空格子 + ItemId int32 `protobuf:"varint,4,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id + Amount uint32 `protobuf:"varint,5,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量 + CTime int64 `protobuf:"varint,6,opt,name=CTime,proto3" json:"CTime,omitempty"` //物品获取时间 + ETime int64 `protobuf:"varint,7,opt,name=ETime,proto3" json:"ETime,omitempty"` //物品过期时间 + IsNewItem bool `protobuf:"varint,8,opt,name=IsNewItem,proto3" json:"IsNewItem,omitempty"` //是否是新的 } -func (x *DB_GridData) Reset() { - *x = DB_GridData{} +func (x *DB_UserItemData) Reset() { + *x = DB_UserItemData{} if protoimpl.UnsafeEnabled { mi := &file_pack_pack_db_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -44,13 +45,13 @@ func (x *DB_GridData) Reset() { } } -func (x *DB_GridData) String() string { +func (x *DB_UserItemData) String() string { return protoimpl.X.MessageStringOf(x) } -func (*DB_GridData) ProtoMessage() {} +func (*DB_UserItemData) ProtoMessage() {} -func (x *DB_GridData) ProtoReflect() protoreflect.Message { +func (x *DB_UserItemData) ProtoReflect() protoreflect.Message { mi := &file_pack_pack_db_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -62,138 +63,86 @@ func (x *DB_GridData) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use DB_GridData.ProtoReflect.Descriptor instead. -func (*DB_GridData) Descriptor() ([]byte, []int) { +// Deprecated: Use DB_UserItemData.ProtoReflect.Descriptor instead. +func (*DB_UserItemData) Descriptor() ([]byte, []int) { return file_pack_pack_db_proto_rawDescGZIP(), []int{0} } -func (x *DB_GridData) GetGridId() int32 { +func (x *DB_UserItemData) GetGridId() string { if x != nil { return x.GridId } - return 0 + return "" } -func (x *DB_GridData) GetIsEmpty() bool { +func (x *DB_UserItemData) GetUId() string { + if x != nil { + return x.UId + } + return "" +} + +func (x *DB_UserItemData) GetIsEmpty() bool { if x != nil { return x.IsEmpty } return false } -func (x *DB_GridData) GetItemId() int32 { +func (x *DB_UserItemData) GetItemId() int32 { if x != nil { return x.ItemId } return 0 } -func (x *DB_GridData) GetAmount() uint32 { +func (x *DB_UserItemData) GetAmount() uint32 { if x != nil { return x.Amount } return 0 } -func (x *DB_GridData) GetCTime() int64 { +func (x *DB_UserItemData) GetCTime() int64 { if x != nil { return x.CTime } return 0 } -func (x *DB_GridData) GetETime() int64 { +func (x *DB_UserItemData) GetETime() int64 { if x != nil { return x.ETime } return 0 } -func (x *DB_GridData) GetIsNewItem() bool { +func (x *DB_UserItemData) GetIsNewItem() bool { if x != nil { return x.IsNewItem } return false } -//用户背包 -type DB_UserPackData struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - UserId string `protobuf:"bytes,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` // 用户Id - Pack []*DB_GridData `protobuf:"bytes,2,rep,name=Pack,proto3" json:"Pack,omitempty"` //背包列表 -} - -func (x *DB_UserPackData) Reset() { - *x = DB_UserPackData{} - if protoimpl.UnsafeEnabled { - mi := &file_pack_pack_db_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *DB_UserPackData) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*DB_UserPackData) ProtoMessage() {} - -func (x *DB_UserPackData) ProtoReflect() protoreflect.Message { - mi := &file_pack_pack_db_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use DB_UserPackData.ProtoReflect.Descriptor instead. -func (*DB_UserPackData) Descriptor() ([]byte, []int) { - return file_pack_pack_db_proto_rawDescGZIP(), []int{1} -} - -func (x *DB_UserPackData) GetUserId() string { - if x != nil { - return x.UserId - } - return "" -} - -func (x *DB_UserPackData) GetPack() []*DB_GridData { - if x != nil { - return x.Pack - } - return nil -} - var File_pack_pack_db_proto protoreflect.FileDescriptor var file_pack_pack_db_proto_rawDesc = []byte{ 0x0a, 0x12, 0x70, 0x61, 0x63, 0x6b, 0x2f, 0x70, 0x61, 0x63, 0x6b, 0x5f, 0x64, 0x62, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xb9, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x47, 0x72, 0x69, 0x64, - 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, - 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x49, - 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, - 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, - 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x18, - 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, - 0x45, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x45, 0x54, 0x69, - 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x18, - 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, - 0x22, 0x4b, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x50, 0x61, 0x63, 0x6b, 0x44, - 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x04, 0x50, - 0x61, 0x63, 0x6b, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x44, 0x42, 0x5f, 0x47, - 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x50, 0x61, 0x63, 0x6b, 0x42, 0x06, 0x5a, - 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xcf, 0x01, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, + 0x49, 0x74, 0x65, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, + 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, + 0x12, 0x10, 0x0a, 0x03, 0x55, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x55, + 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x07, 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x16, 0x0a, 0x06, + 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, + 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, + 0x43, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x43, 0x54, 0x69, + 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x05, 0x45, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x49, 0x73, 0x4e, 0x65, + 0x77, 0x49, 0x74, 0x65, 0x6d, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x49, 0x73, 0x4e, + 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -208,18 +157,16 @@ func file_pack_pack_db_proto_rawDescGZIP() []byte { return file_pack_pack_db_proto_rawDescData } -var file_pack_pack_db_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_pack_pack_db_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_pack_pack_db_proto_goTypes = []interface{}{ - (*DB_GridData)(nil), // 0: DB_GridData - (*DB_UserPackData)(nil), // 1: DB_UserPackData + (*DB_UserItemData)(nil), // 0: DB_UserItemData } var file_pack_pack_db_proto_depIdxs = []int32{ - 0, // 0: DB_UserPackData.Pack:type_name -> DB_GridData - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name } func init() { file_pack_pack_db_proto_init() } @@ -229,19 +176,7 @@ func file_pack_pack_db_proto_init() { } if !protoimpl.UnsafeEnabled { file_pack_pack_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*DB_GridData); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_pack_pack_db_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*DB_UserPackData); i { + switch v := v.(*DB_UserItemData); i { case 0: return &v.state case 1: @@ -259,7 +194,7 @@ func file_pack_pack_db_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_pack_pack_db_proto_rawDesc, NumEnums: 0, - NumMessages: 2, + NumMessages: 1, NumExtensions: 0, NumServices: 0, }, diff --git a/pb/pack_msg.pb.go b/pb/pack_msg.pb.go index cd1be6dad..ca8145d3d 100644 --- a/pb/pack_msg.pb.go +++ b/pb/pack_msg.pb.go @@ -26,7 +26,7 @@ type GetlistReq struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - IType int32 `protobuf:"varint,1,opt,name=IType,proto3" json:"IType,omitempty"` + IType int32 `protobuf:"varint,1,opt,name=IType,proto3" json:"IType,omitempty"` //道具类型 } func (x *GetlistReq) Reset() { @@ -74,7 +74,7 @@ type GetlistResp struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Grids []*DB_GridData `protobuf:"bytes,1,rep,name=Grids,proto3" json:"Grids,omitempty"` + Grids []*DB_UserItemData `protobuf:"bytes,1,rep,name=Grids,proto3" json:"Grids,omitempty"` //用户背包列表 } func (x *GetlistResp) Reset() { @@ -109,7 +109,7 @@ func (*GetlistResp) Descriptor() ([]byte, []int) { return file_pack_pack_msg_proto_rawDescGZIP(), []int{1} } -func (x *GetlistResp) GetGrids() []*DB_GridData { +func (x *GetlistResp) GetGrids() []*DB_UserItemData { if x != nil { return x.Grids } @@ -329,24 +329,24 @@ var file_pack_pack_msg_proto_rawDesc = []byte{ 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x12, 0x70, 0x61, 0x63, 0x6b, 0x2f, 0x70, 0x61, 0x63, 0x6b, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x22, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x6c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x54, 0x79, 0x70, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x49, 0x54, 0x79, 0x70, 0x65, 0x22, 0x31, 0x0a, - 0x0b, 0x47, 0x65, 0x74, 0x6c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x12, 0x22, 0x0a, 0x05, - 0x47, 0x72, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x44, 0x42, - 0x5f, 0x47, 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x05, 0x47, 0x72, 0x69, 0x64, 0x73, - 0x22, 0x54, 0x0a, 0x0a, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, - 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, - 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, - 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, - 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x0d, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, - 0x6d, 0x52, 0x65, 0x73, 0x70, 0x22, 0x55, 0x0a, 0x0b, 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x65, - 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, - 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, - 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x0e, 0x0a, 0x0c, - 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, - 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x49, 0x54, 0x79, 0x70, 0x65, 0x22, 0x35, 0x0a, + 0x0b, 0x47, 0x65, 0x74, 0x6c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x12, 0x26, 0x0a, 0x05, + 0x47, 0x72, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x44, 0x42, + 0x5f, 0x55, 0x73, 0x65, 0x72, 0x49, 0x74, 0x65, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x52, 0x05, 0x47, + 0x72, 0x69, 0x64, 0x73, 0x22, 0x54, 0x0a, 0x0a, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, + 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, + 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, + 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x0d, 0x0a, 0x0b, 0x55, 0x73, + 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x22, 0x55, 0x0a, 0x0b, 0x53, 0x65, 0x6c, + 0x6c, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, + 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, + 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, + 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, + 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, + 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x70, + 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -363,16 +363,16 @@ func file_pack_pack_msg_proto_rawDescGZIP() []byte { var file_pack_pack_msg_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_pack_pack_msg_proto_goTypes = []interface{}{ - (*GetlistReq)(nil), // 0: GetlistReq - (*GetlistResp)(nil), // 1: GetlistResp - (*UseItemReq)(nil), // 2: UseItemReq - (*UseItemResp)(nil), // 3: UseItemResp - (*SellItemReq)(nil), // 4: SellItemReq - (*SellItemResp)(nil), // 5: SellItemResp - (*DB_GridData)(nil), // 6: DB_GridData + (*GetlistReq)(nil), // 0: GetlistReq + (*GetlistResp)(nil), // 1: GetlistResp + (*UseItemReq)(nil), // 2: UseItemReq + (*UseItemResp)(nil), // 3: UseItemResp + (*SellItemReq)(nil), // 4: SellItemReq + (*SellItemResp)(nil), // 5: SellItemResp + (*DB_UserItemData)(nil), // 6: DB_UserItemData } var file_pack_pack_msg_proto_depIdxs = []int32{ - 6, // 0: GetlistResp.Grids:type_name -> DB_GridData + 6, // 0: GetlistResp.Grids:type_name -> DB_UserItemData 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name diff --git a/pb/proto/pack/pack_db.proto b/pb/proto/pack/pack_db.proto index de63fa96a..2afbf07a6 100644 --- a/pb/proto/pack/pack_db.proto +++ b/pb/proto/pack/pack_db.proto @@ -3,18 +3,13 @@ option go_package = ".;pb"; //背包格子 -message DB_GridData { - int32 GridId = 1; //背包格子Id 所在背包数组的下表 - bool IsEmpty = 2; //是否是空格子 - int32 ItemId = 3; //存放物品的Id - uint32 Amount = 4; //存放物品的数量 - int64 CTime = 5; //物品获取时间 - int64 ETime = 6; //物品过期时间 - bool IsNewItem = 7; //是否是新的 +message DB_UserItemData { + string GridId = 1; //背包格子Id + string UId = 2; //用户id + bool IsEmpty = 3; //是否是空格子 + int32 ItemId = 4; //存放物品的Id + uint32 Amount = 5; //存放物品的数量 + int64 CTime = 6; //物品获取时间 + int64 ETime = 7; //物品过期时间 + bool IsNewItem = 8; //是否是新的 } - -//用户背包 -message DB_UserPackData { - string UserId = 1; // @go_tags(`bson:"_id"`) 用户Id - repeated DB_GridData Pack = 2; //背包列表 -} \ No newline at end of file diff --git a/pb/proto/pack/pack_msg.proto b/pb/proto/pack/pack_msg.proto index 5bd1496b2..7b7085a92 100644 --- a/pb/proto/pack/pack_msg.proto +++ b/pb/proto/pack/pack_msg.proto @@ -9,7 +9,7 @@ message GetlistReq { //查询用户背包请求 回应 message GetlistResp { - repeated DB_GridData Grids = 1; //用户背包列表 + repeated DB_UserItemData Grids = 1; //用户背包列表 } //使用物品请求 diff --git a/pb_2.7.py b/pb_2.7.py deleted file mode 100644 index 77fd6d866..000000000 --- a/pb_2.7.py +++ /dev/null @@ -1,37 +0,0 @@ -import io -import os -import re - -def buildProto(outpath,ipath,pbpath,pbfile): - cmdstr = 'protoc.exe --go_out={0} -I{1} {2}/{3}.proto'.format(outpath,ipath,pbpath,pbfile) - os.system(cmdstr) - file_data = "" - tags = {} - tagsstr = "" - file = "{0}/{1}.pb.go".format(outpath,pbfile) - with io.open(file, "r", encoding='utf-8') as f: - for line in f: - if 'tags:' in line: - for v in re.findall(r"`(.+?)`",line)[0].split(' '): - tag = v.split(':') - tags[tag[0]] = tag[1] - for v in re.findall(r"tags:{(.+?)}",line)[0].split(' '): - tag = v.split(':') - tags[tag[0]] = tag[1] - for key,value in tags.items(): - tagsstr += "{0}:{1} ".format(key,value) - line = re.sub(r"`(.+?)`", "`{0}`".format(tagsstr[0:len(tagsstr)-1]), line) - file_data += line - with io.open(file,"w",encoding='utf-8') as f: - f.write(file_data) - -buildProto('./pb','./pb/proto','./pb/proto','comm') -buildProto('./pb','./pb/proto','./pb/proto','errorcode') -buildProto('./pb','./pb/proto','./pb/proto/user','user_db') -buildProto('./pb','./pb/proto','./pb/proto/user','user_msg') -buildProto('./pb','./pb/proto','./pb/proto/pack','pack_db') -buildProto('./pb','./pb/proto','./pb/proto/pack','pack_msg') -buildProto('./pb','./pb/proto','./pb/proto/mail','mail_db') -buildProto('./pb','./pb/proto','./pb/proto/mail','mail_msg') -buildProto('./pb','./pb/proto','./pb/proto/friend','friend_db') -buildProto('./pb','./pb/proto','./pb/proto/friend','friend_msg') \ No newline at end of file diff --git a/sys/cache/core.go b/sys/cache/core.go index 1f4236367..4912a06de 100644 --- a/sys/cache/core.go +++ b/sys/cache/core.go @@ -7,7 +7,6 @@ redis 缓存数据管理系统 type ( ISys interface { IUser //户模块的相关缓存接口 - IPack //背包模块的线管缓存接口 IFriend //好友相关的缓存接口 } ) diff --git a/sys/cache/friend.go b/sys/cache/friend.go index 628096486..ffa3139e1 100644 --- a/sys/cache/friend.go +++ b/sys/cache/friend.go @@ -26,7 +26,7 @@ type IFriend interface { func (this *Cache) Friend_Update(data *pb.Cache_FriendData) (err error) { if err = db.Defsys.Friend_SaveOrUpdate(data); err == nil { //更新缓存 - err = this.redis.Set(getRdsFriendKey(data.UserId), data, 0) + _, err = this.redis.Set(getRdsFriendKey(data.UserId), data, 0) } return } diff --git a/sys/cache/pack.go b/sys/cache/pack.go deleted file mode 100644 index b7a4e0a94..000000000 --- a/sys/cache/pack.go +++ /dev/null @@ -1,315 +0,0 @@ -package cache - -import ( - "errors" - "fmt" - "go_dreamfactory/pb" - "go_dreamfactory/sys/db" - - "go_dreamfactory/lego/sys/mgo" - "go_dreamfactory/lego/sys/redis" -) - -const ( //Redis - Redis_PackCache string = "pack:%s" //背包缓存数据存放key -) - -const ( - GridCapMaxNum = 99 //单个格子的最大容量 - GridMaxNUm = 200 //背包格子数量上限 - Pack_Expiration = -1 //背包缓存数据过期时间 -) - -var ( - ItemNotEnoughError = errors.New("item not enough!") //物品不足 - NoFoundGirdError = errors.New("no found gvrid!") //未找到格子 - PackGridNumUpper = errors.New("grid amount upper!") //背包格子达到上限 - GirdAmountUpper = errors.New("grid amount upper!") //格子容量达到上限 -) - -type IPack interface { - ///查询用户背包 - Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) - //查询用户背包物品数量 - Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) - ///添加物品到背包 (可以加物品和减物品) - Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) - ///添加多个物品到背包 (可以加物品和减物品) - Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) - //修改用户背包格子的标识 - Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error) - //更新用户背包格子信息 - Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (err error) -} - -///查询用户背包数据 -func (this *Cache) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{ - UserId: uId, - } - if err = this.redis.Get(fmt.Sprintf(Redis_PackCache, uId), pack); err == nil { - return - } else if err == redis.RedisNil { - if pack, err = db.Defsys.Pack_QueryUserPack(uId); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } else if err == mgo.MongodbNil { - err = nil - } - } - return -} - -//查询用户背包物品数量 -func (this *Cache) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) { - var ( - pack *pb.DB_UserPackData - err error - ) - if pack, err = this.Pack_QueryUserPack(uId); err != nil { - return - } - for _, v := range pack.Pack { - if !v.IsEmpty && v.ItemId == itemid { - amount += v.Amount - } - } - return -} - -///添加或则减少物品到用户背包 -func (this *Cache) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) { - var ( - pack *pb.DB_UserPackData - modifys []*pb.DB_GridData - leftnum int64 - ) - if addnum == 0 { - return - } - if pack, err = this.Pack_QueryUserPack(uId); err != nil { - return - } - modifys, leftnum = this.pack_addItemToUserPack(pack, itemId, addnum) - if leftnum < 0 { - err = ItemNotEnoughError - return - } else if leftnum > 0 { - err = PackGridNumUpper - return - } - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - return -} - -///添加或则减少多个物品到用户背包 -func (this *Cache) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) { - var ( - pack *pb.DB_UserPackData - modifys []*pb.DB_GridData - tempmodifys []*pb.DB_GridData - leftnum int64 - iskeep bool - ) - if pack, err = this.Pack_QueryUserPack(uId); err != nil { - return - } - for k, v := range items { - tempmodifys, leftnum = this.pack_addItemToUserPack(pack, k, v) - if leftnum < 0 { - err = ItemNotEnoughError - return - } else if leftnum > 0 { - err = PackGridNumUpper - return - } - for _, v1 := range tempmodifys { - iskeep = false - for _, v2 := range modifys { - if v1.GridId == v2.GridId { - iskeep = true - } - } - if !iskeep { - modifys = append(modifys, v1) - } - } - } - - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - return -} - -///修改指定格子的物品数量 -func (this *Cache) Pack_AddItemToUserPackByGrid(uId string, gridid int32, itemId int32, addnum int32) (err error) { - var ( - pack *pb.DB_UserPackData - grid *pb.DB_GridData - num int64 - amount int64 - ) - if addnum == 0 { - return - } - if pack, err = this.Pack_QueryUserPack(uId); err != nil { - return - } - if int32(len(pack.Pack)) <= gridid { - err = NoFoundGirdError - return - } - grid = pack.Pack[gridid] - if grid == nil { - err = NoFoundGirdError - return - } - amount = int64(grid.Amount) - if grid.IsEmpty { - amount = 0 - } else if grid.ItemId != itemId { - err = fmt.Errorf("target grid itemid:%d no is %d ", grid.ItemId, itemId) - } - num = amount + int64(addnum) - if num < 0 { - err = ItemNotEnoughError - } else { - if num > GridCapMaxNum { - err = GirdAmountUpper - return - } else { - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grid); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - } - } - return -} - -///修改目标格子的新获取标识 -func (this *Cache) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error) { - var ( - pack *pb.DB_UserPackData - ) - if pack, err = db.Defsys.Pack_ModifyPackGridIsNewItem(uId, grids); err == nil { - err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - return -} - -///修改目标格子的新获取标识 -func (this *Cache) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (err error) { - var ( - pack *pb.DB_UserPackData - ) - if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grids...); err == nil { - err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) - } - return -} - -///添加移除物品到用户背包 -func (this *Cache) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId int32, addnum int32) (modifys []*pb.DB_GridData, leftnum int64) { - var ( - num int64 - isNew bool - ) - if addnum == 0 { - return - } - isNew = true - leftnum = int64(addnum) - modifys = make([]*pb.DB_GridData, 0) - for _, v := range pack.Pack { - if !v.IsEmpty && v.ItemId == itemId { - isNew = false - num = int64(v.Amount) + int64(leftnum) - if num < 0 { - leftnum += int64(v.Amount) - v.Amount = 0 - v.IsEmpty = true - modifys = append(modifys, v) - } else if num > 0 && num < int64(v.Amount) { - leftnum = 0 - v.Amount = uint32(num) - modifys = append(modifys, v) - break - } else if num > 0 && num > int64(v.Amount) { - if num <= GridCapMaxNum { - leftnum = 0 - v.Amount = uint32(num) - modifys = append(modifys, v) - break - } else { - if v.Amount < GridCapMaxNum { - leftnum = int64(num - GridCapMaxNum) - v.Amount = uint32(GridCapMaxNum) - modifys = append(modifys, v) - } - } - } else if num == 0 { - leftnum = 0 - v.Amount = 0 - v.IsEmpty = true - modifys = append(modifys, v) - } - } - } - if leftnum < 0 { //背包物品不够 - return - } - if leftnum > 0 { //还没有放完 寻找空的格子填充 - for _, v := range pack.Pack { - if v.IsEmpty { - if leftnum <= GridCapMaxNum { - v.IsEmpty = false - v.ItemId = itemId - v.Amount = uint32(leftnum) - leftnum = 0 - modifys = append(modifys, v) - break - } else { - leftnum -= GridCapMaxNum - v.IsEmpty = false - v.ItemId = itemId - v.Amount = uint32(GridCapMaxNum) - modifys = append(modifys, v) - } - } - } - index := int32(len(pack.Pack)) - for leftnum > 0 { //需要补充格子 - if leftnum <= GridCapMaxNum { - grid := &pb.DB_GridData{ - GridId: index, - IsEmpty: false, - ItemId: itemId, - Amount: uint32(leftnum), - IsNewItem: isNew, - } - pack.Pack = append(pack.Pack, grid) - modifys = append(modifys, grid) - leftnum = 0 - break - } else { - leftnum -= GridCapMaxNum - grid := &pb.DB_GridData{ - GridId: index, - IsEmpty: false, - ItemId: itemId, - Amount: uint32(GridCapMaxNum), - IsNewItem: isNew, - } - pack.Pack = append(pack.Pack, grid) - modifys = append(modifys, grid) - index++ - } - if index > GridMaxNUm { //格子已达上限 - break - } - } - } - return -} diff --git a/sys/cache/pack_test.go b/sys/cache/pack_test.go deleted file mode 100644 index 2482d49b9..000000000 --- a/sys/cache/pack_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package cache_test - -import ( - "fmt" - "go_dreamfactory/sys/cache" - "testing" -) - -//测试用户背包 添加物品 -func Test_Pack_AddItemToUserPack(t *testing.T) { - err := cache.Defsys.Pack_AddItemToUserPack("liwei1dao", 1001, 100) - fmt.Printf("Pack_AddItemToUserPack err:%v\n", err) -} - -//测试用户背包 添加物品 -func Test_Pack_AddItemsToUserPack(t *testing.T) { - err := cache.Defsys.Pack_AddItemsToUserPack("liwei1dao", map[int32]int32{1003: -100}) - fmt.Printf("Pack_AddItemToUserPack err:%v\n", err) -} diff --git a/sys/cache/user.go b/sys/cache/user.go index 7dc2e9e6f..f28b63fb9 100644 --- a/sys/cache/user.go +++ b/sys/cache/user.go @@ -20,7 +20,7 @@ type IUser interface { func (this *Cache) Update(data *pb.Cache_UserData) (err error) { err = db.Defsys.User_Update(data.UserData) if err == nil { - err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.Uid), data, -1) + _, err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.Uid), data, -1) } return } diff --git a/sys/db/core.go b/sys/db/core.go index bbb107798..967c1431a 100644 --- a/sys/db/core.go +++ b/sys/db/core.go @@ -3,7 +3,6 @@ package db type ( ISys interface { IUser - IPack IMail IFriend } diff --git a/sys/db/pack.go b/sys/db/pack.go deleted file mode 100644 index e333b803d..000000000 --- a/sys/db/pack.go +++ /dev/null @@ -1,82 +0,0 @@ -package db - -import ( - "fmt" - "go_dreamfactory/pb" - - "go_dreamfactory/lego/core" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -const ( //Redis - DB_PackTable core.SqlTable = "pack" //背包表 -) - -type IPack interface { - ///初始化用户物品表 - Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error) - ///查询用户背包 - Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) - ///更新用户背包数据 - Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) - ///修改用户背包格子的标识 - Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) -} - -func (this *DB) Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{UserId: uId, Pack: make([]*pb.DB_GridData, 0)} - _, err = this.mgo.InsertOne(DB_PackTable, pack) - return -} - -///查询用户背包数据 -func (this *DB) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - err = this.mgo.FindOne(DB_PackTable, bson.M{"_id": uId}).Decode(pack) - return -} - -//更新背包格子数据 -func (this *DB) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - update := bson.M{} - for _, v := range grids { - update[fmt.Sprintf("pack.%d.gridid", v.GridId)] = v.GridId - update[fmt.Sprintf("pack.%d.isempty", v.GridId)] = v.IsEmpty - update[fmt.Sprintf("pack.%d.itemid", v.GridId)] = v.ItemId - update[fmt.Sprintf("pack.%d.amount", v.GridId)] = v.Amount - update[fmt.Sprintf("pack.%d.isnewitem", v.GridId)] = v.IsNewItem - } - - err = this.mgo.FindOneAndUpdate(DB_PackTable, - bson.M{"_id": uId}, - bson.M{"$set": update}, - options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack) - return -} - -//更新背包格子物品的数据 -func (this *DB) Pack_AddGridAmountToUserPack(uId string, grid int32, amount int32) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - err = this.mgo.FindOneAndUpdate(DB_PackTable, - bson.M{"_id": uId}, - bson.M{"$inc": bson.M{ - fmt.Sprintf("pack.%d.amount", grid): amount, - }}, - options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack) - return -} - -//修改背包格子IsNew标识 -func (this *DB) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) { - pack = &pb.DB_UserPackData{} - identifier := []interface{}{bson.M{"item.gridid": bson.M{"$in": grids}}} - err = this.mgo.FindOneAndUpdate(DB_PackTable, - bson.M{"_id": uId}, - bson.M{"$set": bson.M{ - "pack.$[item].isNewitem": false, - }}, options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{Filters: identifier}).SetReturnDocument(options.After)).Decode(pack) - return -} diff --git a/sys/db/pack_test.go b/sys/db/pack_test.go deleted file mode 100644 index 8123bb5a9..000000000 --- a/sys/db/pack_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package db - -import ( - "go_dreamfactory/pb" - "testing" - - "github.com/stretchr/testify/require" -) - -func Test_Pack_InitUserPack(t *testing.T) { - _, err := db.Pack_InitUserPack("liwei1dao") - require.Nil(t, err) -} - -func Test_Pack_UpdateGridToUserPack(t *testing.T) { - data, err := db.Pack_UpdateGridToUserPack("liwei1dao", - &pb.DB_GridData{ - - GridId: 0, - IsEmpty: false, - ItemId: 1001, - Amount: 202, - IsNewItem: true, - }, - &pb.DB_GridData{ - GridId: 1, - IsEmpty: false, - ItemId: 1002, - Amount: 202, - IsNewItem: true, - }, - &pb.DB_GridData{ - GridId: 2, - IsEmpty: false, - ItemId: 1003, - Amount: 202, - IsNewItem: true, - }, - ) - require.Nil(t, err) - require.Nil(t, data) -} -func Test_Pack_QueryUserPack(t *testing.T) { - data, err := db.Pack_QueryUserPack("liwei1dao") - require.Nil(t, err) - require.Nil(t, data) -} - -func Test_Pack_UpdateGridAmountToUserPack(t *testing.T) { - data, err := db.Pack_AddGridAmountToUserPack("liwei1dao", 0, 102) - require.Nil(t, err) - require.Nil(t, data) -} -func Test_Pack_ModifyPackGridIsNewItem(t *testing.T) { - data, err := db.Pack_ModifyPackGridIsNewItem("liwei1dao", []int32{0, 1}) - require.Nil(t, err) - require.Nil(t, data) -}