Merge branch 'dev' of http://git.legu.cc/liwei_3d/go_dreamfactory into meixiongfeng

This commit is contained in:
meixiongfeng 2022-06-13 17:40:26 +08:00
commit b9ddf78232
39 changed files with 1271 additions and 2125 deletions

View File

@ -30,6 +30,8 @@ type (
UpdateOne(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) 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) 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) 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 FindOneAndDelete(sqltable core.SqlTable, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteMany(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) DeleteMany(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

View File

@ -135,6 +135,16 @@ func (this *Mongodb) FindOneAndDelete(sqltable core.SqlTable, filter interface{}
return this.Collection(sqltable).FindOneAndDelete(this.getContext(), filter, opts...) 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) { func (this *Mongodb) DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
return this.Collection(sqltable).DeleteOne(this.getContext(), filter, opts...) return this.Collection(sqltable).DeleteOne(this.getContext(), filter, opts...)
} }

View File

@ -88,6 +88,6 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
//锁 //锁
func (this *Redis) UnLock(key string) (err error) { func (this *Redis) UnLock(key string) (err error) {
err = this.Delete(key) _, err = this.Delete(key)
return return
} }

View File

@ -1,8 +1,6 @@
package cluster package cluster
import ( import (
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
@ -10,13 +8,7 @@ import (
Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略 Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略
*/ */
func (this *Redis) HDel(key string, fields ...string) (err error) { func (this *Redis) HDel(key string, fields ...string) (err error) {
agrs := make([]interface{}, 0) err = this.client.HDel(this.getContext(), key, fields...).Err()
agrs = append(agrs, "HDEL")
agrs = append(agrs, key)
for _, v := range fields {
agrs = append(agrs, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
return return
} }
@ -24,18 +16,15 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
Redis Hexists 命令用于查看哈希表的指定字段是否存在 Redis Hexists 命令用于查看哈希表的指定字段是否存在
*/ */
func (this *Redis) HExists(key string, field string) (result bool, err error) { 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 return
} }
/* /*
Redis Hget 命令用于返回哈希表中指定字段的值 Redis Hget 命令用于返回哈希表中指定字段的值
*/ */
func (this *Redis) HGet(key string, field string, value interface{}) (err error) { func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
var resultvalue string err = this.client.HGet(this.getContext(), key, field).Scan(v)
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
err = this.Decode([]byte(resultvalue), value)
}
return return
} }
@ -43,19 +32,8 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error)
Redis Hgetall 命令用于返回哈希表中所有的字段和值 Redis Hgetall 命令用于返回哈希表中所有的字段和值
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍 在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/ */
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) err = this.client.HGetAll(this.getContext(), key).Scan(v)
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 return
} }
@ -67,8 +45,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误
本操作的值被限制在 64 (bit)有符号数字表示之内 本操作的值被限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) HIncrBy(key string, field string, value int) (err error) { func (this *Redis) HIncrBy(key string, field string, value int64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() err = this.client.HIncrBy(this.getContext(), key, field, value).Err()
return return
} }
@ -76,8 +54,8 @@ func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值 Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值
如果指定的字段不存在那么在执行命令前字段的值被初始化为 0 如果指定的字段不存在那么在执行命令前字段的值被初始化为 0
*/ */
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err()
return return
} }
@ -85,17 +63,21 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er
Redis Hkeys 命令用于获取哈希表中的所有域(field) Redis Hkeys 命令用于获取哈希表中的所有域(field)
*/ */
func (this *Redis) Hkeys(key string) (result []string, err error) { func (this *Redis) Hkeys(key string) (result []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) var cmd *redis.StringSliceCmd
this.client.Process(this.getContext(), cmd) cmd = this.client.HKeys(this.getContext(), key)
result, err = cmd.Result() result = cmd.Val()
err = cmd.Err()
return return
} }
/* /*
Redis Hlen 命令用于获取哈希表中字段的数量 Redis Hlen 命令用于获取哈希表中字段的数量
*/ */
func (this *Redis) Hlen(key string) (result int, err error) { func (this *Redis) Hlen(key string) (result int64, err error) {
result, err = this.client.Do(this.getContext(), "HLEN", key).Int() var cmd *redis.IntCmd
cmd = this.client.HLen(this.getContext(), key)
result = cmd.Val()
err = cmd.Err()
return return
} }
@ -103,26 +85,8 @@ func (this *Redis) Hlen(key string) (result int, err error) {
Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值 Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值
如果指定的字段不存在于哈希表那么返回一个 nil 如果指定的字段不存在于哈希表那么返回一个 nil
*/ */
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd {
agrs := make([]interface{}, 0) return this.client.HMGet(this.getContext(), key, fields...)
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
} }
/* /*
@ -148,10 +112,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
如果字段已经存在于哈希表中旧值将被覆盖 如果字段已经存在于哈希表中旧值将被覆盖
*/ */
func (this *Redis) HSet(key string, field string, value interface{}) (err error) { func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
var resultvalue []byte err = this.client.HSet(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
}
return return
} }
@ -162,9 +123,6 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令 如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令
*/ */
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
var resultvalue []byte err = this.client.HSetNX(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
}
return return
} }

View File

@ -1,84 +1,69 @@
package cluster package cluster
import ( import (
"github.com/go-redis/redis/v8" "time"
) )
/* Key *******************************************************************************/ /* Key *******************************************************************************/
///删除redis key ///删除redis key
func (this *Redis) Delete(key string) (err error) { func (this *Redis) Delete(key ...string) (int64, error) {
err = this.client.Do(this.getContext(), "DEL", key).Err() return this.client.Del(this.getContext(), key...).Result()
return
} }
///判断是否存在key ///判断是否存在key
func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { func (this *Redis) Exists(key ...string) (int64, error) {
iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() return this.client.Exists(this.getContext(), key...).Result()
return
} }
///设置key的过期时间 单位以秒级 ///设置key的过期时间 单位以秒级
func (this *Redis) ExpireKey(key string, expire int) (err error) { func (this *Redis) Expire(key string, expire time.Duration) (bool, error) {
err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() return this.client.Expire(this.getContext(), key, expire).Result()
return
} }
///设置key的过期时间戳 秒级时间戳 ///设置key的过期时间戳 秒级时间戳
func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) {
err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() return this.client.ExpireAt(this.getContext(), key, expire).Result()
return
} }
///设置key的过期时间 单位以毫秒级 ///设置key的过期时间 单位以毫秒级
func (this *Redis) Pexpirekey(key string, expire int) (err error) { func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) {
err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() return this.client.PExpire(this.getContext(), key, expire).Result()
return
} }
///设置key的过期时间戳 单位以豪秒级 ///设置key的过期时间戳 单位以豪秒级
func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) {
err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() return this.client.PExpireAt(this.getContext(), key, expire).Result()
return
} }
///移除Key的过期时间 ///移除Key的过期时间
func (this *Redis) PersistKey(key string) (err error) { func (this *Redis) Persist(key string) (bool, error) {
err = this.client.Do(this.getContext(), "PERSIST", key).Err() return this.client.Persist(this.getContext(), key).Result()
return
} }
///获取key剩余过期时间 单位毫秒 ///获取key剩余过期时间 单位毫秒
func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { func (this *Redis) PTTL(key string) (surplus time.Duration, err error) {
leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() return this.client.PTTL(this.getContext(), key).Result()
return
} }
///获取key剩余过期时间 单位秒 ///获取key剩余过期时间 单位秒
func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { func (this *Redis) TTL(key string) (surplus time.Duration, err error) {
leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() return this.client.TTL(this.getContext(), key).Result()
return
} }
///重命名Key ///重命名Key
func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { func (this *Redis) Rename(oldkey string, newkey string) (string, error) {
err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() return this.client.Rename(this.getContext(), oldkey, newkey).Result()
return
} }
///重命名key 在新的 key 不存在时修改 key 的名称 ///重命名key 在新的 key 不存在时修改 key 的名称
func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) {
err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() return this.client.RenameNX(this.getContext(), oldkey, newkey).Result()
return
} }
///判断是否存在key pattern:key* ///判断是否存在key pattern:key*
func (this *Redis) Keys(pattern string) (keys []string, err error) { func (this *Redis) Keys(pattern string) (keys []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) return this.client.Keys(this.getContext(), pattern).Result()
this.client.Process(this.getContext(), cmd)
keys, err = cmd.Result()
return
} }
///获取键类型 ///获取键类型

View File

@ -1,23 +1,14 @@
package cluster package cluster
import ( import (
"fmt"
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
/* /*
Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推 Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/ */
func (this *Redis) Lindex(key string, value interface{}) (err error) { func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) {
var data string return this.client.LIndex(this.getContext(), key, index).Scan(v)
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
} }
/* /*
@ -25,90 +16,55 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定
当列表不存在时被视为空列表不执行任何操作 当列表不存在时被视为空列表不执行任何操作
如果 key 不是列表类型返回一个错误 如果 key 不是列表类型返回一个错误
*/ */
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) {
var ( op := "BEFORE"
tagervalue []byte if !isbefore {
resultvalue []byte op = "AFTER"
)
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()
} }
} return this.client.LInsert(this.getContext(), key, op, pivot, value).Result()
}
return
} }
/* /*
Redis Llen 命令用于返回列表的长度 如果列表 key 不存在 key 被解释为一个空列表返回 0 如果 key 不是列表类型返回一个错误 Redis Llen 命令用于返回列表的长度 如果列表 key 不存在 key 被解释为一个空列表返回 0 如果 key 不是列表类型返回一个错误
*/ */
func (this *Redis) Llen(key string) (result int, err error) { func (this *Redis) Llen(key string) (int64, error) {
result, err = this.client.Do(this.getContext(), "LLEN", key).Int() return this.client.LLen(this.getContext(), key).Result()
return
} }
/* /*
Redis Lpop 命令用于移除并返回列表的第一个元素 Redis Lpop 命令用于移除并返回列表的第一个元素
*/ */
func (this *Redis) LPop(key string, value interface{}) (err error) { func (this *Redis) LPop(key string, v interface{}) (err error) {
var data string return this.client.LPop(this.getContext(), key).Scan(v)
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
} }
/* /*
Redis Lpush 命令将一个或多个值插入到列表头部 如果 key 不存在一个空列表会被创建并执行 LPUSH 操作 key 存在但不是列表类型时返回一个错误 Redis Lpush 命令将一个或多个值插入到列表头部 如果 key 不存在一个空列表会被创建并执行 LPUSH 操作 key 存在但不是列表类型时返回一个错误
*/ */
func (this *Redis) LPush(key string, values ...interface{}) (err error) { func (this *Redis) LPush(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.LPush(this.getContext(), key, values...).Result()
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
} }
/* /*
Redis Lpushx 将一个值插入到已存在的列表头部列表不存在时操作无效 Redis Lpushx 将一个值插入到已存在的列表头部列表不存在时操作无效
*/ */
func (this *Redis) LPushX(key string, values ...interface{}) (err error) { func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) // agrs := make([]interface{}, 0)
agrs = append(agrs, "LPUSHX") // agrs = append(agrs, "LPUSHX")
for _, v := range values { // for _, v := range values {
result, _ := this.Encode(v) // result, _ := this.Encode(v)
agrs = append(agrs, result) // agrs = append(agrs, result)
} // }
err = this.client.Do(this.getContext(), agrs...).Err() // err = this.client.Do(this.getContext(), agrs...).Err()
return return this.client.LPushX(this.getContext(), key, values...).Result()
} }
/* /*
Redis Lrange 返回列表中指定区间内的元素区间以偏移量 START END 指定 其中 0 表示列表的第一个元素 1 表示列表的第二个元素 Redis Lrange 返回列表中指定区间内的元素区间以偏移量 START END 指定 其中 0 表示列表的第一个元素 1 表示列表的第二个元素
以此类推 你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推 以此类推 你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/ */
func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd {
var _result []string return this.client.LRange(this.getContext(), key, start, end)
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
} }
/* /*
@ -118,24 +74,16 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,
count < 0 : 从表尾开始向表头搜索移除与 VALUE 相等的元素数量为 COUNT 的绝对值 count < 0 : 从表尾开始向表头搜索移除与 VALUE 相等的元素数量为 COUNT 的绝对值
count = 0 : 移除表中所有与 VALUE 相等的值 count = 0 : 移除表中所有与 VALUE 相等的值
*/ */
func (this *Redis) LRem(key string, count int, target interface{}) (err error) { func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) {
var resultvalue []byte return this.client.LRem(this.getContext(), key, count, v).Result()
if resultvalue, err = this.Encode(target); err == nil {
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
}
return
} }
/* /*
Redis Lset 通过索引来设置元素的值 Redis LSet 通过索引来设置元素的值
当索引参数超出范围或对一个空列表进行 LSET 返回一个错误 当索引参数超出范围或对一个空列表进行 LSET 返回一个错误
*/ */
func (this *Redis) LSet(key string, index int, value interface{}) (err error) { func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) {
var resultvalue []byte return this.client.LSet(this.getContext(), key, index, v).Result()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
}
return
} }
/* /*
@ -143,35 +91,22 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留
下标 0 表示列表的第一个元素 1 表示列表的第二个元素以此类推 你也可以使用负数下标 下标 0 表示列表的第一个元素 1 表示列表的第二个元素以此类推 你也可以使用负数下标
-1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/ */
func (this *Redis) Ltrim(key string, start, stop int) (err error) { func (this *Redis) LTrim(key string, start, stop int64) (string, error) {
err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() return this.client.LTrim(this.getContext(), key, start, stop).Result()
return
} }
/* /*
Redis Rpop 命令用于移除列表的最后一个元素返回值为移除的元素 Redis Rpop 命令用于移除列表的最后一个元素返回值为移除的元素
*/ */
func (this *Redis) Rpop(key string, value interface{}) (err error) { func (this *Redis) RPop(key string, v interface{}) (err error) {
var data string return this.client.RPop(this.getContext(), key).Scan(v)
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
} }
/* /*
Redis Rpoplpush 命令用于移除列表的最后一个元素并将该元素添加到另一个列表并返回 Redis Rpoplpush 命令用于移除列表的最后一个元素并将该元素添加到另一个列表并返回
*/ */
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) {
var data string return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v)
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
} }
/* /*
@ -179,27 +114,13 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)
如果列表不存在一个空列表会被创建并执行 RPUSH 操作 当列表存在但不是列表类型时返回一个错误 如果列表不存在一个空列表会被创建并执行 RPUSH 操作 当列表存在但不是列表类型时返回一个错误
注意 Redis 2.4 版本以前的 RPUSH 命令都只接受单个 value 注意 Redis 2.4 版本以前的 RPUSH 命令都只接受单个 value
*/ */
func (this *Redis) RPush(key string, values ...interface{}) (err error) { func (this *Redis) RPush(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.RPush(this.getContext(), key, values...).Result()
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
} }
/* /*
Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)如果列表不存在操作无效 Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)如果列表不存在操作无效
*/ */
func (this *Redis) RPushX(key string, values ...interface{}) (err error) { func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.RPushX(this.getContext(), key, values...).Result()
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
} }

View File

@ -1,30 +1,23 @@
package cluster package cluster
import "reflect" import (
"github.com/go-redis/redis/v8"
)
/* /*
Redis Sadd 命令将一个或多个成员元素加入到集合中已经存在于集合的成员元素将被忽略 Redis Sadd 命令将一个或多个成员元素加入到集合中已经存在于集合的成员元素将被忽略
假如集合 key 不存在则创建一个只包含添加的元素作成员的集合 假如集合 key 不存在则创建一个只包含添加的元素作成员的集合
当集合 key 不是集合类型时返回一个错误 当集合 key 不是集合类型时返回一个错误
*/ */
func (this *Redis) SAdd(key string, values ...interface{}) (err error) { func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.SAdd(this.getContext(), key, values...).Result()
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
} }
/* /*
Redis Scard 命令返回集合中元素的数量 Redis Scard 命令返回集合中元素的数量
*/ */
func (this *Redis) SCard(key string) (result int64, err error) { func (this *Redis) SCard(key string) (int64, error) {
result, err = this.client.SCard(this.getContext(), key).Result() return this.client.SCard(this.getContext(), key).Result()
return
} }
/* /*
@ -32,79 +25,43 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可
差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集
实例: 实例:
*/ */
func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd {
var _result []string return this.client.SDiff(this.getContext(), keys...)
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
} }
/* /*
Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中 Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中
*/ */
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) {
result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() return this.client.SDiffStore(this.getContext(), destination, keys...).Result()
return
} }
/* /*
Redis Sismember 命令返回给定所有给定集合的交集 不存在的集合 key 被视为空集 当给定集合当中有一个空集时结果也为空集(根据集合运算定律) Redis Sismember 命令返回给定所有给定集合的交集 不存在的集合 key 被视为空集 当给定集合当中有一个空集时结果也为空集(根据集合运算定律)
*/ */
func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd {
var _result []string return this.client.SInter(this.getContext(), keys...)
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
} }
/* /*
Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖 Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖
*/ */
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) {
result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() return this.client.SInterStore(this.getContext(), destination, keys...).Result()
return
} }
/* /*
Redis Sismember 命令判断成员元素是否是集合的成员 Redis Sismember 命令判断成员元素是否是集合的成员
*/ */
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { func (this *Redis) SIsMember(key string, value interface{}) (bool, error) {
iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() return this.client.SIsMember(this.getContext(), key, value).Result()
return
} }
/* /*
Redis Smembers 号召返回集合中的所有成员 Redis Smembers 号召返回集合中的所有成员
*/ */
func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { func (this *Redis) SMembers(key string) *redis.StringSliceCmd {
var _result []string return this.client.SMembers(this.getContext(), key)
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
} }
/* /*
@ -114,18 +71,16 @@ SMOVE 是原子性操作。
destination 集合已经包含 member 元素时 SMOVE 命令只是简单地将 source 集合中的 member 元素删除 destination 集合已经包含 member 元素时 SMOVE 命令只是简单地将 source 集合中的 member 元素删除
source destination 不是集合类型时返回一个错误 source destination 不是集合类型时返回一个错误
*/ */
func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) {
result, err = this.client.SMove(this.getContext(), source, destination, member).Result() return this.client.SMove(this.getContext(), source, destination, member).Result()
return
} }
/* /*
Redis Spop命令用于移除集合中的指定键的一个或多个随机元素移除后会返回移除的元素 Redis Spop命令用于移除集合中的指定键的一个或多个随机元素移除后会返回移除的元素
该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动 该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动
*/ */
func (this *Redis) Spop(key string) (result string, err error) { func (this *Redis) SPop(key string) (string, error) {
result, err = this.client.SPop(this.getContext(), key).Result() return this.client.SPop(this.getContext(), key).Result()
return
} }
/* /*
@ -135,9 +90,8 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。
如果 count 为负数那么命令返回一个数组数组中的元素可能会重复出现多次而数组的长度为 count 的绝对值 如果 count 为负数那么命令返回一个数组数组中的元素可能会重复出现多次而数组的长度为 count 的绝对值
该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动 该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动
*/ */
func (this *Redis) Srandmember(key string) (result string, err error) { func (this *Redis) SRandMember(key string) (string, error) {
result, err = this.client.SRandMember(this.getContext(), key).Result() return this.client.SRandMember(this.getContext(), key).Result()
return
} }
/* /*
@ -145,41 +99,27 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在
当键不是集合类型返回一个错误 当键不是集合类型返回一个错误
Redis 2.4 版本以前SREM 只接受个别成员值 Redis 2.4 版本以前SREM 只接受个别成员值
*/ */
func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { func (this *Redis) SRem(key string, members ...interface{}) (int64, error) {
result, err = this.client.SRem(this.getContext(), key, members...).Result() return this.client.SRem(this.getContext(), key, members...).Result()
return
} }
/* /*
Redis Sunion 命令返回给定集合的并集 Redis Sunion 命令返回给定集合的并集
*/ */
func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd {
var _result []string return this.client.SUnion(this.getContext(), keys...)
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
} }
/* /*
Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖 Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖
*/ */
func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) {
result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() return this.client.SUnionStore(this.getContext(), destination, keys...).Result()
return
} }
/* /*
Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan
*/ */
func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) {
keys, cursor, err = this.client.SScan(this.getContext(), key, _cursor, match, count).Result() return this.client.SScan(this.getContext(), key, _cursor, match, count).Result()
return
} }

View File

@ -1,7 +1,6 @@
package cluster package cluster
import ( import (
"fmt"
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
@ -11,54 +10,39 @@ import (
/* /*
命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型 命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型
*/ */
func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) {
var result []byte return this.client.Set(this.getContext(), string(key), v, expiration).Result()
if result, err = this.Encode(value); err == nil {
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
}
return
} }
/* /*
指定的 key 不存在时 key 设置指定的值 指定的 key 不存在时 key 设置指定的值
*/ */
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) {
// var _value []byte return this.client.SetNX(this.getContext(), key, v, expiration).Result()
// 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
} }
/* /*
同时设置一个或多个 key-value 同时设置一个或多个 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 := make([]interface{}, 0)
agrs = append(agrs, "MSET") agrs = append(agrs, "MSET")
for k, v := range keyvalues { for k, v := range keyvalues {
result, _ := this.Encode(v) agrs = append(agrs, k, v)
agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() return this.client.Do(this.getContext(), agrs...).Err()
return
} }
/* /*
命令用于所有给定 key 都不存在时同时设置一个或多个 key-value 命令用于所有给定 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 := make([]interface{}, 0)
agrs = append(agrs, "MSETNX") agrs = append(agrs, "MSETNX")
for k, v := range keyvalues { for k, v := range keyvalues {
result, _ := this.Encode(v) agrs = append(agrs, k, v)
agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() return this.client.Do(this.getContext(), agrs...).Err()
return
} }
/* /*
@ -67,9 +51,8 @@ Redis Incr 命令将 key 中储存的数字值增一。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Incr(key string) (err error) { func (this *Redis) Incr(key string) (int64, error) {
err = this.client.Do(this.getContext(), "INCR", key).Err() return this.client.Incr(this.getContext(), key).Result()
return
} }
/* /*
@ -78,18 +61,16 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) IncrBY(key string, value int) (err error) { func (this *Redis) IncrBY(key string, value int64) (int64, error) {
err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() return this.client.IncrBy(this.getContext(), key, value).Result()
return
} }
/* /*
Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值 Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值
如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作 如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作
*/ */
func (this *Redis) Incrbyfloat(key string, value float32) (err error) { func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) {
err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() return this.client.IncrByFloat(this.getContext(), key, value).Result()
return
} }
/* /*
@ -98,9 +79,8 @@ Redis Decr 命令将 key 中储存的数字值减一。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Decr(key string, value int) (err error) { func (this *Redis) Decr(key string, value int) (int64, error) {
err = this.client.Do(this.getContext(), "DECR", key, value).Err() return this.client.Decr(this.getContext(), key).Result()
return
} }
/* /*
@ -109,9 +89,8 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) DecrBy(key string, value int) (err error) { func (this *Redis) DecrBy(key string, value int64) (int64, error) {
err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() return this.client.DecrBy(this.getContext(), key, value).Result()
return
} }
/* /*
@ -119,62 +98,28 @@ Redis Append 命令用于为指定的 key 追加值。
如果 key 已经存在并且是一个字符串 APPEND 命令将 value 追加到 key 原来的值的末尾 如果 key 已经存在并且是一个字符串 APPEND 命令将 value 追加到 key 原来的值的末尾
如果 key 不存在 APPEND 就简单地将给定 key 设为 value 就像执行 SET key value 一样 如果 key 不存在 APPEND 就简单地将给定 key 设为 value 就像执行 SET key value 一样
*/ */
func (this *Redis) Append(key string, value interface{}) (err error) { func (this *Redis) Append(key string, value string) (err error) {
var result []byte this.client.Append(this.getContext(), key, value).Result()
if result, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
}
return return
} }
/* /*
命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型 命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型
*/ */
func (this *Redis) Get(key string, value interface{}) (err error) { func (this *Redis) Get(key string, v interface{}) (err error) {
var result []byte return this.client.Get(this.getContext(), key).Scan(v)
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
err = this.Decode(result, value)
}
return
} }
/* /*
设置指定 key 的值并返回 key 的旧值 设置指定 key 的值并返回 key 的旧值
*/ */
func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) {
var ( return this.client.GetSet(this.getContext(), key, v).Scan(result)
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
} }
/* /*
返回所有(一个或多个)给定 key 的值 如果给定的 key 里面有某个 key 不存在那么这个 key 返回特殊值 nil 返回所有(一个或多个)给定 key 的值 如果给定的 key 里面有某个 key 不存在那么这个 key 返回特殊值 nil
*/ */
func (this *Redis) MGet(keys ...string) (result []string, err error) { func (this *Redis) MGet(keys ...string) *redis.SliceCmd {
agrs := make([]interface{}, 0) return this.client.MGet(this.getContext(), keys...)
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
} }

View File

@ -1,217 +1,145 @@
package cluster package cluster
import ( import (
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
/* /*
Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数 Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数
*/ */
func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) {
this.client.ZAdd(this.getContext(), key, members...) return this.client.ZAdd(this.getContext(), key, members...).Result()
return
} }
/* /*
Redis Zcard 用于计算集合中元素的数量 Redis Zcard 用于计算集合中元素的数量
*/ */
func (this *Redis) ZCard(key string) (result int64, err error) { func (this *Redis) ZCard(key string) (int64, error) {
result, err = this.client.ZCard(this.getContext(), key).Result() return this.client.ZCard(this.getContext(), key).Result()
return
} }
/* /*
Redis ZCount 用于计算集合中指定的范围内的数量 Redis ZCount 用于计算集合中指定的范围内的数量
*/ */
func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { func (this *Redis) ZCount(key string, min string, max string) (int64, error) {
result, err = this.client.ZCount(this.getContext(), key, min, max).Result() return this.client.ZCount(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment
*/ */
func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) {
result, err = this.client.ZIncrBy(this.getContext(), key, increment, member).Result() return this.client.ZIncrBy(this.getContext(), key, increment, member).Result()
return
} }
/* /*
Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination
*/ */
func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) {
result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() return this.client.ZInterStore(this.getContext(), destination, store).Result()
return
} }
/* /*
Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 Redis ZLexCount 在有序集合中计算指定字典区间内成员数量
*/ */
func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) {
result, err = this.client.ZLexCount(this.getContext(), key, min, max).Result() return this.client.ZLexCount(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZRange 通过索引区间返回有序集合指定区间内的成员 Redis ZRange 通过索引区间返回有序集合指定区间内的成员
*/ */
func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd {
var _result []string return this.client.ZRange(this.getContext(), key, start, stop)
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
} }
/* /*
Redis ZRangeByLex 通过字典区间返回有序集合的成员 Redis ZRangeByLex 通过字典区间返回有序集合的成员
*/ */
func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
var _result []string return this.client.ZRangeByLex(this.getContext(), key, opt)
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
} }
/* /*
Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员
*/ */
func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
var _result []string return this.client.ZRangeByScore(this.getContext(), key, opt)
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
} }
/* /*
Redis ZRank 返回有序集合中指定成员的索引 Redis ZRank 返回有序集合中指定成员的索引
*/ */
func (this *Redis) ZRank(key string, member string) (result int64, err error) { func (this *Redis) ZRank(key string, member string) (int64, error) {
result, err = this.client.ZRank(this.getContext(), key, member).Result() return this.client.ZRank(this.getContext(), key, member).Result()
return
} }
/* /*
Redis ZRem 移除有序集合中的一个或多个成员 Redis ZRem 移除有序集合中的一个或多个成员
*/ */
func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) {
result, err = this.client.ZRem(this.getContext(), key, members...).Result() return this.client.ZRem(this.getContext(), key, members...).Result()
return
} }
/* /*
Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员
*/ */
func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) {
result, err = this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员
*/ */
func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) {
result, err = this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result()
return
} }
/* /*
Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员
*/ */
func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) {
result, err = this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZRevRange 返回有序集中指定区间内的成员通过索引分数从高到低 ZREVRANGE Redis ZRevRange 返回有序集中指定区间内的成员通过索引分数从高到低 ZREVRANGE
*/ */
func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd {
var _result []string return this.client.ZRevRange(this.getContext(), key, start, stop)
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
} }
/* /*
Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员分数从高到低排序 Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员分数从高到低排序
*/ */
func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
var _result []string return this.client.ZRevRangeByScore(this.getContext(), key, opt)
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
} }
/* /*
Redis ZRevRank 返回有序集中指定分数区间内的成员分数从高到低排序 Redis ZRevRank 返回有序集中指定分数区间内的成员分数从高到低排序
*/ */
func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { func (this *Redis) ZRevRank(key string, member string) (int64, error) {
result, err = this.client.ZRevRank(this.getContext(), key, member).Result() return this.client.ZRevRank(this.getContext(), key, member).Result()
return
} }
/* /*
Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序 Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序
*/ */
func (this *Redis) ZScore(key string, member string) (result float64, err error) { func (this *Redis) ZScore(key string, member string) (float64, error) {
result, err = this.client.ZScore(this.getContext(), key, member).Result() return this.client.ZScore(this.getContext(), key, member).Result()
return
} }
/* /*
Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序 ZUNIONSTORE Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序 ZUNIONSTORE
*/ */
func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) {
result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() return this.client.ZUnionStore(this.getContext(), dest, store).Result()
return
} }
/* /*
Redis ZScan 迭代有序集合中的元素包括元素成员和元素分值 Redis ZScan 迭代有序集合中的元素包括元素成员和元素分值
*/ */
func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) {
keys, cursor, err = this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() return this.client.ZScan(this.getContext(), key, _cursor, match, count).Result()
return
} }

View File

@ -2,7 +2,6 @@ package redis
import ( import (
"context" "context"
"reflect"
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
@ -18,99 +17,382 @@ type (
TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) 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) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error)
/*Key*/ /*Key*/
Delete(key string) (err error) ///删除redis key
ExistsKey(key string) (iskeep bool, err error) Delete(key ...string) (int64, error)
ExpireKey(key string, expire int) (err error) ///判断是否存在key
ExpireatKey(key string, expire_unix int64) (err error) Exists(key ...string) (int64, error)
Pexpirekey(key string, expire int) (err error) ///设置key的过期时间 单位以秒级
PexpireatKey(key string, expire_unix int64) (err error) Expire(key string, expire time.Duration) (bool, error)
PersistKey(key string) (err error) ///设置key的过期时间戳 秒级时间戳
PttlKey(key string) (leftexpire int64, err error) ExpireAt(key string, expire time.Time) (bool, error)
TtlKey(key string) (leftexpire int64, err error) ///设置key的过期时间 单位以毫秒级
RenameKye(oldkey string, newkey string) (err error) PExpire(key string, expire time.Duration) (bool, error)
RenamenxKey(oldkey string, newkey string) (err 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) Keys(pattern string) (keys []string, err error)
///获取键类型
Type(key string) (ty string, err error) Type(key string) (ty string, err error)
/*String*/ /*String*/
Set(key string, value interface{}, expiration time.Duration) (err error) /*
SetNX(key string, value interface{}) (result int64, err error) 命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型
MSet(keyvalues map[string]interface{}) (err error) */
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) MSetNX(keyvalues map[string]interface{}) (err error)
Incr(key string) (err error) /*
IncrBY(key string, value int) (err error) Redis Incr 命令将 key 中储存的数字值增一
Incrbyfloat(key string, value float32) (err error) 如果 key 不存在那么 key 的值会先被初始化为 0 然后再执行 INCR 操作
Decr(key string, value int) (err error) 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
DecrBy(key string, value int) (err error) 本操作的值限制在 64 (bit)有符号数字表示之内
Append(key string, value interface{}) (err error) */
Get(key string, value interface{}) (err error) Incr(key string) (int64, error)
GetSet(key string, value interface{}, result interface{}) (err error) /*
MGet(keys ...string) (result []string, err error) Redis Incrby 命令将 key 中储存的数字加上指定的增量值
INCRBY(key string, amount int64) (result int64, err error) 如果 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*/ /*List*/
Lindex(key string, value interface{}) (err error) /*
Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
Llen(key string) (result int, err error) */
LPop(key string, value interface{}) (err error) Lindex(key string, index int64, v interface{}) (err error)
LPush(key string, values ...interface{}) (err error) /*
LPushX(key string, values ...interface{}) (err error) Redis Linsert 命令用于在列表的元素前或者后插入元素当指定元素不存在于列表中时不执行任何操作
LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) 当列表不存在时被视为空列表不执行任何操作
LRem(key string, count int, target interface{}) (err error) 如果 key 不是列表类型返回一个错误
LSet(key string, index int, value interface{}) (err error) */
Ltrim(key string, start, stop int) (err error) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error)
Rpop(key string, value interface{}) (err error) /*
RPopLPush(oldkey string, newkey string, value interface{}) (err error) Redis Llen 命令用于返回列表的长度 如果列表 key 不存在 key 被解释为一个空列表返回 0 如果 key 不是列表类型返回一个错误
RPush(key string, values ...interface{}) (err error) */
RPushX(key string, values ...interface{}) (err error) 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*/ /*Hash*/
///Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略
HDel(key string, fields ...string) (err error) HDel(key string, fields ...string) (err error)
///Hexists 命令用于查看哈希表的指定字段是否存在
HExists(key string, field string) (result bool, err error) HExists(key string, field string) (result bool, err error)
HGet(key string, field string, value interface{}) (err error) ///Hget 命令用于返回哈希表中指定字段的值
HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) HGet(key string, field string, v interface{}) (err error)
HIncrBy(key string, field string, value int) (err error) ///Hgetall 命令用于返回哈希表中,所有的字段和值。
HIncrByFloat(key string, field string, value float32) (err error) ///在返回值里,紧跟每个字段名(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) 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) HMSet(key string, value map[string]interface{}) (err error)
/*
Redis Hset 命令用于为哈希表中的字段赋值
如果哈希表不存在一个新的哈希表被创建并进行 HSET 操作
如果字段已经存在于哈希表中旧值将被覆盖
*/
HSet(key string, field string, value interface{}) (err error) HSet(key string, field string, value interface{}) (err error)
/*
Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
如果哈希表不存在一个新的哈希表被创建并进行 HSET 操作
如果字段已经存在于哈希表中操作无效
如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令
*/
HSetNX(key string, field string, value interface{}) (err error) HSetNX(key string, field string, value interface{}) (err error)
/*Set*/ /*Set*/
SAdd(key string, values ...interface{}) (err error) /*
SCard(key string) (result int64, err error) Redis Sadd 命令将一个或多个成员元素加入到集合中已经存在于集合的成员元素将被忽略
SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) 假如集合 key 不存在则创建一个只包含添加的元素作成员的集合
SDiffStore(destination string, keys ...string) (result int64, err error) 当集合 key 不是集合类型时返回一个错误
SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) */
SInterStore(destination string, keys ...string) (result int64, err error) SAdd(key string, values ...interface{}) (int64, error)
Sismember(key string, value interface{}) (iskeep bool, err error) /*
SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) Redis Scard 命令返回集合中元素的数量
SMove(source string, destination string, member interface{}) (result bool, err error) */
Spop(key string) (result string, err error) SCard(key string) (int64, error)
Srandmember(key string) (result string, err error) /*
SRem(key string, members ...interface{}) (result int64, err error) Redis Sdiff 命令返回第一个集合与其他集合之间的差异也可以认为说第一个集合中独有的元素不存在的集合 key 将视为空集
SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集
Sunionstore(destination string, keys ...string) (result int64, err error) 实例:
Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) */
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*/ /*ZSet*/
ZAdd(key string, members ...*redis.Z) (err error) /*
ZCard(key string) (result int64, err error) Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数
ZCount(key string, min string, max string) (result int64, err error) */
ZIncrBy(key string, increment float64, member string) (result float64, err error) ZAdd(key string, members ...*redis.Z) (int64, error)
ZInterStore(destination string, store *redis.ZStore) (result int64, err error) /*
ZLexCount(key string, min string, max string) (result int64, err error) Redis Zcard 用于计算集合中元素的数量
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) ZCard(key string) (int64, error)
ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) /*
ZRank(key string, member string) (result int64, err error) Redis ZCount 用于计算集合中指定的范围内的数量
ZRem(key string, members ...interface{}) (result int64, err error) */
ZRemRangeByLex(key string, min string, max string) (result int64, err error) ZCount(key string, min string, max string) (int64, error)
ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) /*
ZRemRangeByScore(key string, min string, max string) (result int64, err error) Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment
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) ZIncrBy(key string, increment float64, member string) (float64, error)
ZRevRank(key string, member string) (result int64, err error) /*
ZScore(key string, member string) (result float64, err error) Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination
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) 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 { ISys interface {
@ -162,55 +444,52 @@ func Encode(value interface{}) (result []byte, err error) {
func Decode(value []byte, result interface{}) (err error) { func Decode(value []byte, result interface{}) (err error) {
return defsys.Decode(value, result) return defsys.Decode(value, result)
} }
func Delete(key string) (err error) { func Delete(key ...string) (int64, error) {
return defsys.Delete(key) return defsys.Delete(key...)
} }
func ExistsKey(key string) (iskeep bool, err error) { func Exists(key ...string) (int64, error) {
return defsys.ExistsKey(key) return defsys.Exists(key...)
} }
func ExpireKey(key string, expire int) (err error) { func Expire(key string, expire time.Duration) (bool, error) {
return defsys.ExpireKey(key, expire) return defsys.Expire(key, expire)
} }
func ExpireatKey(key string, expire_unix int64) (err error) { func ExpireAt(key string, expire time.Time) (bool, error) {
return defsys.ExpireatKey(key, expire_unix) return defsys.ExpireAt(key, expire)
} }
func Pexpirekey(key string, expire int) (err error) { func PExpire(key string, expire time.Duration) (bool, error) {
return defsys.Pexpirekey(key, expire) return defsys.PExpire(key, expire)
} }
func PexpireatKey(key string, expire_unix int64) (err error) { func PExpireAt(key string, expire time.Time) (bool, error) {
return defsys.PexpireatKey(key, expire_unix) return defsys.PExpireAt(key, expire)
} }
func PersistKey(key string) (err error) { func Persist(key string) (bool, error) {
return defsys.PersistKey(key) return defsys.Persist(key)
} }
func PttlKey(key string) (leftexpire int64, err error) { func PTTL(key string) (surplus time.Duration, err error) {
return defsys.PttlKey(key) return defsys.PTTL(key)
} }
func TtlKey(key string) (leftexpire int64, err error) { func TTL(key string) (surplus time.Duration, err error) {
return defsys.TtlKey(key) return defsys.TTL(key)
} }
func RenameKye(oldkey string, newkey string) (err error) { func Rename(oldkey string, newkey string) (string, error) {
return defsys.RenameKye(oldkey, newkey) return defsys.Rename(oldkey, newkey)
} }
func RenamenxKey(oldkey string, newkey string) (err error) { func RenameNX(oldkey string, newkey string) (bool, error) {
return defsys.RenamenxKey(oldkey, newkey) return defsys.RenameNX(oldkey, newkey)
} }
func Keys(pattern string) (keys []string, err error) { func Keys(pattern string) (keys []string, err error) {
return defsys.Keys(pattern) return defsys.Keys(pattern)
} }
///获取键类型
func Type(key string) (ty string, err error) { func Type(key string) (ty string, err error) {
return defsys.Type(key) return defsys.Type(key)
} }
/*String*/ /*String*/
func Set(key string, value interface{}, expiration time.Duration) (err error) { func Set(key string, v interface{}, expiration time.Duration) (string, error) {
return defsys.Set(key, value, expiration) return defsys.Set(key, v, expiration)
} }
func SetNX(key string, value interface{}) (result int64, err error) { func SetNX(key string, v interface{}, expiration time.Duration) (bool, error) {
return defsys.SetNX(key, value) return defsys.SetNX(key, v, expiration)
} }
func MSet(keyvalues map[string]interface{}) (err error) { func MSet(keyvalues map[string]interface{}) (err error) {
return defsys.MSet(keyvalues) return defsys.MSet(keyvalues)
@ -218,36 +497,33 @@ func MSet(keyvalues map[string]interface{}) (err error) {
func MSetNX(keyvalues map[string]interface{}) (err error) { func MSetNX(keyvalues map[string]interface{}) (err error) {
return defsys.MSetNX(keyvalues) return defsys.MSetNX(keyvalues)
} }
func Incr(key string) (err error) { func Incr(key string) (int64, error) {
return defsys.Incr(key) 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) 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) 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) 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) 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) return defsys.Append(key, value)
} }
func Get(key string, value interface{}) (err error) { func Get(key string, v interface{}) (err error) {
return defsys.Get(key, value) return defsys.Get(key, v)
} }
func GetSet(key string, value interface{}, result interface{}) (err error) { func GetSet(key string, v interface{}, result interface{}) (err error) {
return defsys.GetSet(key, value, result) return defsys.GetSet(key, v, result)
} }
func MGet(keys ...string) (result []string, err error) { func MGet(keys ...string) *redis.SliceCmd {
return defsys.MGet(keys...) return defsys.MGet(keys...)
} }
func INCRBY(key string, amount int64) (result int64, err error) {
return defsys.INCRBY(key, amount)
}
/*Lock*/ /*Lock*/
func NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error) { func NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error) {
@ -262,46 +538,46 @@ func UnLock(key string) (err error) {
} }
/*List*/ /*List*/
func Lindex(key string, value interface{}) (err error) { func Lindex(key string, index int64, v interface{}) (err error) {
return defsys.Lindex(key, value) return defsys.Lindex(key, index, v)
} }
func Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { func Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) {
return defsys.Linsert(key, isbefore, tager, value) 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) return defsys.Llen(key)
} }
func LPop(key string, value interface{}) (err error) { func LPop(key string, value interface{}) (err error) {
return defsys.LPop(key, value) 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...) 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...) return defsys.LPushX(key, values...)
} }
func LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { func LRange(key string, start int64, end int64) *redis.StringSliceCmd {
return defsys.LRange(key, start, end, valuetype) return defsys.LRange(key, start, end)
} }
func LRem(key string, count int, target interface{}) (err error) { func LRem(key string, count int64, v interface{}) (int64, error) {
return defsys.LRem(key, count, target) return defsys.LRem(key, count, v)
} }
func LSet(key string, index int, value interface{}) (err error) { func LSet(key string, index int64, v interface{}) (string, error) {
return defsys.LSet(key, index, value) return defsys.LSet(key, index, v)
} }
func Ltrim(key string, start, stop int) (err error) { func LTrim(key string, start int64, stop int64) (string, error) {
return defsys.Ltrim(key, start, stop) return defsys.LTrim(key, start, stop)
} }
func Rpop(key string, value interface{}) (err error) { func RPop(key string, v interface{}) (err error) {
return defsys.Rpop(key, value) return defsys.RPop(key, v)
} }
func RPopLPush(oldkey string, newkey string, value interface{}) (err error) { func RPopLPush(oldkey string, newkey string, value interface{}) (err error) {
return defsys.RPopLPush(oldkey, newkey, value) 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...) 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...) 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) { func HGet(key string, field string, value interface{}) (err error) {
return defsys.HGet(key, field, value) return defsys.HGet(key, field, value)
} }
func HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func HGetAll(key string, v interface{}) (err error) {
return defsys.HGetAll(key, valuetype) 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) 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) return defsys.HIncrByFloat(key, field, value)
} }
func Hkeys(key string) (result []string, err error) { func Hkeys(key string) (result []string, err error) {
return defsys.Hkeys(key) return defsys.Hkeys(key)
} }
func Hlen(key string) (result int, err error) { func Hlen(key string) (result int64, err error) {
return defsys.Hlen(key) return defsys.Hlen(key)
} }
func HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func HMGet(key string, fields ...string) *redis.SliceCmd {
return defsys.HMGet(key, valuetype, fields...) return defsys.HMGet(key, fields...)
} }
func HMSet(key string, value map[string]interface{}) (err error) { func HMSet(key string, value map[string]interface{}) (err error) {
return defsys.HMSet(key, value) return defsys.HMSet(key, value)
@ -344,54 +620,54 @@ func HSetNX(key string, field string, value interface{}) (err error) {
} }
/*Set*/ /*Set*/
func SAdd(key string, values ...interface{}) (err error) { func SAdd(key string, values ...interface{}) (int64, error) {
return defsys.SAdd(key, values...) return defsys.SAdd(key, values...)
} }
func SCard(key string) (result int64, err error) { func SCard(key string) (result int64, err error) {
return defsys.SCard(key) return defsys.SCard(key)
} }
func SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func SDiff(keys ...string) *redis.StringSliceCmd {
return defsys.SDiff(valuetype, keys...) return defsys.SDiff(keys...)
} }
func SDiffStore(destination string, keys ...string) (result int64, err error) { func SDiffStore(destination string, keys ...string) (result int64, err error) {
return defsys.SDiffStore(destination, keys...) return defsys.SDiffStore(destination, keys...)
} }
func SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func SInter(keys ...string) *redis.StringSliceCmd {
return defsys.SInter(valuetype, keys...) return defsys.SInter(keys...)
} }
func SInterStore(destination string, keys ...string) (result int64, err error) { func SInterStore(destination string, keys ...string) (result int64, err error) {
return defsys.SInterStore(destination, keys...) return defsys.SInterStore(destination, keys...)
} }
func Sismember(key string, value interface{}) (iskeep bool, err error) { func SIsMember(key string, value interface{}) (iskeep bool, err error) {
return defsys.Sismember(key, value) return defsys.SIsMember(key, value)
} }
func SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { func SMembers(key string) *redis.StringSliceCmd {
return defsys.SMembers(valuetype, key) return defsys.SMembers(key)
} }
func SMove(source string, destination string, member interface{}) (result bool, err error) { func SMove(source string, destination string, member interface{}) (result bool, err error) {
return defsys.SMove(source, destination, member) return defsys.SMove(source, destination, member)
} }
func Spop(key string) (result string, err error) { func SPop(key string) (result string, err error) {
return defsys.Spop(key) return defsys.SPop(key)
} }
func Srandmember(key string) (result string, err error) { func SRandMember(key string) (string, error) {
return defsys.Srandmember(key) return defsys.SRandMember(key)
} }
func SRem(key string, members ...interface{}) (result int64, err error) { func SRem(key string, members ...interface{}) (result int64, err error) {
return defsys.SRem(key, members...) return defsys.SRem(key, members...)
} }
func SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func SUnion(keys ...string) *redis.StringSliceCmd {
return defsys.SUnion(valuetype, keys...) return defsys.SUnion(keys...)
} }
func Sunionstore(destination string, keys ...string) (result int64, err error) { func SUnionStore(destination string, keys ...string) (int64, error) {
return defsys.Sunionstore(destination, keys...) return defsys.SUnionStore(destination, keys...)
} }
func Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { func SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) {
return defsys.Sscan(key, _cursor, match, count) return defsys.SScan(key, _cursor, match, count)
} }
/*ZSet*/ /*ZSet*/
func ZAdd(key string, members ...*redis.Z) (err error) { func ZAdd(key string, members ...*redis.Z) (int64, error) {
return defsys.ZAdd(key, members...) return defsys.ZAdd(key, members...)
} }
func ZCard(key string) (result int64, err error) { 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) { func ZLexCount(key string, min string, max string) (result int64, err error) {
return defsys.ZLexCount(key, min, max) return defsys.ZLexCount(key, min, max)
} }
func ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func ZRange(key string, start int64, stop int64) *redis.StringSliceCmd {
return defsys.ZRange(valuetype, key, start, stop) return defsys.ZRange(key, start, stop)
} }
func ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return defsys.ZRangeByLex(valuetype, key, opt) return defsys.ZRangeByLex(key, opt)
} }
func ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return defsys.ZRangeByScore(valuetype, key, opt) return defsys.ZRangeByScore(key, opt)
} }
func ZRank(key string, member string) (result int64, err error) { func ZRank(key string, member string) (result int64, err error) {
return defsys.ZRank(key, member) 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) { func ZRemRangeByScore(key string, min string, max string) (result int64, err error) {
return defsys.ZRemRangeByScore(key, min, max) return defsys.ZRemRangeByScore(key, min, max)
} }
func ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd {
return defsys.ZRevRange(valuetype, key, start, stop) return defsys.ZRevRange(key, start, stop)
} }
func ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return defsys.ZRevRangeByScore(valuetype, key, opt) return defsys.ZRevRangeByScore(key, opt)
} }
func ZRevRank(key string, member string) (result int64, err error) { func ZRevRank(key string, member string) (result int64, err error) {
return defsys.ZRevRank(key, member) return defsys.ZRevRank(key, member)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"reflect"
"time" "time"
"go_dreamfactory/lego/sys/redis/cluster" "go_dreamfactory/lego/sys/redis/cluster"
@ -99,40 +98,40 @@ func (this *Redis) Decode(value []byte, result interface{}) (err error) {
return return
} }
func (this *Redis) Delete(key string) (err error) { func (this *Redis) Delete(key ...string) (int64, error) {
return this.client.Delete(key) return this.client.Delete(key...)
} }
func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { func (this *Redis) Exists(key ...string) (int64, error) {
return this.client.ExistsKey(key) return this.client.Exists(key...)
} }
func (this *Redis) ExpireKey(key string, expire int) (err error) { func (this *Redis) Expire(key string, expire time.Duration) (bool, error) {
return this.client.ExpireKey(key, expire) return this.client.Expire(key, expire)
} }
func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) {
return this.client.ExpireatKey(key, expire_unix) return this.client.ExpireAt(key, expire)
} }
func (this *Redis) Pexpirekey(key string, expire int) (err error) { func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) {
return this.client.Pexpirekey(key, expire) return this.client.PExpire(key, expire)
} }
func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) {
return this.client.PexpireatKey(key, expire_unix) return this.client.PExpireAt(key, expire)
} }
func (this *Redis) PersistKey(key string) (err error) { func (this *Redis) Persist(key string) (bool, error) {
return this.client.PersistKey(key) return this.client.Persist(key)
} }
func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { func (this *Redis) PTTL(key string) (surplus time.Duration, err error) {
return this.client.PttlKey(key) return this.client.PTTL(key)
} }
func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { func (this *Redis) TTL(key string) (surplus time.Duration, err error) {
return this.client.TtlKey(key) return this.client.TTL(key)
} }
func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { func (this *Redis) Rename(oldkey string, newkey string) (string, error) {
return this.client.RenameKye(oldkey, newkey) return this.client.Rename(oldkey, newkey)
} }
func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) {
return this.client.RenamenxKey(oldkey, newkey) return this.client.RenameNX(oldkey, newkey)
} }
func (this *Redis) Keys(pattern string) (keys []string, err error) { func (this *Redis) Keys(pattern string) (keys []string, err error) {
return this.client.Keys(pattern) return this.client.Keys(pattern)
@ -144,11 +143,11 @@ func (this *Redis) Type(key string) (ty string, err error) {
} }
/*String*/ /*String*/
func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) {
return this.client.Set(key, value, expiration) return this.client.Set(key, v, expiration)
} }
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) {
return this.client.SetNX(key, value) return this.client.SetNX(key, v, expiration)
} }
func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) {
return this.client.MSet(keyvalues) 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) { func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) {
return this.client.MSetNX(keyvalues) 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) 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) 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) 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) 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) 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) return this.client.Append(key, value)
} }
func (this *Redis) Get(key string, value interface{}) (err error) { 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) { func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) {
return this.client.GetSet(key, value, result) 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...) return this.client.MGet(keys...)
} }
func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) {
return this.client.INCRBY(key, amount)
}
/*List*/ /*List*/
func (this *Redis) Lindex(key string, value interface{}) (err error) { func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) {
return this.client.Lindex(key, value) return this.client.Lindex(key, index, v)
} }
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) {
return this.client.Linsert(key, isbefore, tager, value) 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) return this.client.Llen(key)
} }
func (this *Redis) LPop(key string, value interface{}) (err error) { func (this *Redis) LPop(key string, value interface{}) (err error) {
return this.client.LPop(key, value) 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...) 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...) return this.client.LPushX(key, values...)
} }
func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) LRange(key string, start int64, end int64) *redis.StringSliceCmd {
return this.client.LRange(key, start, end, valuetype) return this.client.LRange(key, start, end)
} }
func (this *Redis) LRem(key string, count int, target interface{}) (err error) { func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) {
return this.client.LRem(key, count, target) return this.client.LRem(key, count, v)
} }
func (this *Redis) LSet(key string, index int, value interface{}) (err error) { func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) {
return this.client.LSet(key, index, value) return this.client.LSet(key, index, v)
} }
func (this *Redis) Ltrim(key string, start, stop int) (err error) { func (this *Redis) LTrim(key string, start int64, stop int64) (string, error) {
return this.client.Ltrim(key, start, stop) return this.client.LTrim(key, start, stop)
} }
func (this *Redis) Rpop(key string, value interface{}) (err error) { func (this *Redis) RPop(key string, value interface{}) (err error) {
return this.client.Rpop(key, value) return this.client.RPop(key, value)
} }
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) {
return this.client.RPopLPush(oldkey, newkey, value) 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...) 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...) 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) { func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
return this.client.HGet(key, field, value) return this.client.HGet(key, field, value)
} }
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
return this.client.HGetAll(key, valuetype) 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) 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) return this.client.HIncrByFloat(key, field, value)
} }
func (this *Redis) Hkeys(key string) (result []string, err error) { func (this *Redis) Hkeys(key string) (result []string, err error) {
return this.client.Hkeys(key) 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) return this.client.Hlen(key)
} }
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd {
return this.client.HMGet(key, valuetype, fields...) return this.client.HMGet(key, fields...)
} }
func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) { func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) {
return this.client.HMSet(key, value) return this.client.HMSet(key, value)
@ -270,54 +266,54 @@ func (this *Redis) HSetNX(key string, field string, value interface{}) (err erro
} }
/*Set*/ /*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...) return this.client.SAdd(key, values...)
} }
func (this *Redis) SCard(key string) (result int64, err error) { func (this *Redis) SCard(key string) (result int64, err error) {
return this.client.SCard(key) return this.client.SCard(key)
} }
func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd {
return this.client.SDiff(valuetype, keys...) return this.client.SDiff(keys...)
} }
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) {
return this.client.SDiffStore(destination, keys...) return this.client.SDiffStore(destination, keys...)
} }
func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd {
return this.client.SInter(valuetype, keys...) return this.client.SInter(keys...)
} }
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) {
return this.client.SInterStore(destination, keys...) return this.client.SInterStore(destination, keys...)
} }
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { func (this *Redis) SIsMember(key string, value interface{}) (iskeep bool, err error) {
return this.client.Sismember(key, value) return this.client.SIsMember(key, value)
} }
func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { func (this *Redis) SMembers(key string) *redis.StringSliceCmd {
return this.client.SMembers(valuetype, key) return this.client.SMembers(key)
} }
func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) {
return this.client.SMove(source, destination, member) return this.client.SMove(source, destination, member)
} }
func (this *Redis) Spop(key string) (result string, err error) { func (this *Redis) SPop(key string) (string, error) {
return this.client.Spop(key) return this.client.SPop(key)
} }
func (this *Redis) Srandmember(key string) (result string, err error) { func (this *Redis) SRandMember(key string) (string, error) {
return this.client.Srandmember(key) return this.client.SRandMember(key)
} }
func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) {
return this.client.SRem(key, members...) return this.client.SRem(key, members...)
} }
func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd {
return this.client.SUnion(valuetype, keys...) return this.client.SUnion(keys...)
} }
func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SUnionStore(destination string, keys ...string) (result int64, err error) {
return this.client.Sunionstore(destination, keys...) return this.client.SUnionStore(destination, keys...)
} }
func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { 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) return this.client.SScan(key, _cursor, match, count)
} }
/*ZSet*/ /*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...) return this.client.ZAdd(key, members...)
} }
func (this *Redis) ZCard(key string) (result int64, err error) { 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) { func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) {
return this.client.ZLexCount(key, min, max) return this.client.ZLexCount(key, min, max)
} }
func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd {
return this.client.ZRange(valuetype, key, start, stop) return this.client.ZRange(key, start, stop)
} }
func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return this.client.ZRangeByLex(valuetype, key, opt) return this.client.ZRangeByLex(key, opt)
} }
func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return this.client.ZRangeByScore(valuetype, key, opt) return this.client.ZRangeByScore(key, opt)
} }
func (this *Redis) ZRank(key string, member string) (result int64, err error) { func (this *Redis) ZRank(key string, member string) (result int64, err error) {
return this.client.ZRank(key, member) 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) { func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) {
return this.client.ZRemRangeByScore(key, min, max) return this.client.ZRemRangeByScore(key, min, max)
} }
func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd {
return this.client.ZRevRange(valuetype, key, start, stop) return this.client.ZRevRange(key, start, stop)
} }
func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return this.client.ZRevRangeByScore(valuetype, key, opt) return this.client.ZRevRangeByScore(key, opt)
} }
func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { func (this *Redis) ZRevRank(key string, member string) (result int64, err error) {
return this.client.ZRevRank(key, member) return this.client.ZRevRank(key, member)

View File

@ -87,6 +87,6 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
//锁 //锁
func (this *Redis) UnLock(key string) (err error) { func (this *Redis) UnLock(key string) (err error) {
err = this.Delete(key) _, err = this.Delete(key)
return return
} }

View File

@ -1,8 +1,6 @@
package single package single
import ( import (
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
@ -10,13 +8,7 @@ import (
Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略 Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略
*/ */
func (this *Redis) HDel(key string, fields ...string) (err error) { func (this *Redis) HDel(key string, fields ...string) (err error) {
agrs := make([]interface{}, 0) err = this.client.HDel(this.getContext(), key, fields...).Err()
agrs = append(agrs, "HDEL")
agrs = append(agrs, key)
for _, v := range fields {
agrs = append(agrs, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
return return
} }
@ -24,18 +16,15 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
Redis Hexists 命令用于查看哈希表的指定字段是否存在 Redis Hexists 命令用于查看哈希表的指定字段是否存在
*/ */
func (this *Redis) HExists(key string, field string) (result bool, err error) { 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 return
} }
/* /*
Redis Hget 命令用于返回哈希表中指定字段的值 Redis Hget 命令用于返回哈希表中指定字段的值
*/ */
func (this *Redis) HGet(key string, field string, value interface{}) (err error) { func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
var resultvalue string err = this.client.HGet(this.getContext(), key, field).Scan(v)
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
err = this.Decode([]byte(resultvalue), value)
}
return return
} }
@ -43,19 +32,8 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error)
Redis Hgetall 命令用于返回哈希表中所有的字段和值 Redis Hgetall 命令用于返回哈希表中所有的字段和值
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍 在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/ */
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) err = this.client.HGetAll(this.getContext(), key).Scan(v)
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 return
} }
@ -67,8 +45,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误
本操作的值被限制在 64 (bit)有符号数字表示之内 本操作的值被限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) HIncrBy(key string, field string, value int) (err error) { func (this *Redis) HIncrBy(key string, field string, value int64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() err = this.client.HIncrBy(this.getContext(), key, field, value).Err()
return return
} }
@ -76,8 +54,8 @@ func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值 Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值
如果指定的字段不存在那么在执行命令前字段的值被初始化为 0 如果指定的字段不存在那么在执行命令前字段的值被初始化为 0
*/ */
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err()
return return
} }
@ -85,17 +63,21 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er
Redis Hkeys 命令用于获取哈希表中的所有域(field) Redis Hkeys 命令用于获取哈希表中的所有域(field)
*/ */
func (this *Redis) Hkeys(key string) (result []string, err error) { func (this *Redis) Hkeys(key string) (result []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) var cmd *redis.StringSliceCmd
this.client.Process(this.getContext(), cmd) cmd = this.client.HKeys(this.getContext(), key)
result, err = cmd.Result() result = cmd.Val()
err = cmd.Err()
return return
} }
/* /*
Redis Hlen 命令用于获取哈希表中字段的数量 Redis Hlen 命令用于获取哈希表中字段的数量
*/ */
func (this *Redis) Hlen(key string) (result int, err error) { func (this *Redis) Hlen(key string) (result int64, err error) {
result, err = this.client.Do(this.getContext(), "HLEN", key).Int() var cmd *redis.IntCmd
cmd = this.client.HLen(this.getContext(), key)
result = cmd.Val()
err = cmd.Err()
return return
} }
@ -103,26 +85,8 @@ func (this *Redis) Hlen(key string) (result int, err error) {
Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值 Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值
如果指定的字段不存在于哈希表那么返回一个 nil 如果指定的字段不存在于哈希表那么返回一个 nil
*/ */
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd {
agrs := make([]interface{}, 0) return this.client.HMGet(this.getContext(), key, fields...)
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
} }
/* /*
@ -148,10 +112,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
如果字段已经存在于哈希表中旧值将被覆盖 如果字段已经存在于哈希表中旧值将被覆盖
*/ */
func (this *Redis) HSet(key string, field string, value interface{}) (err error) { func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
var resultvalue []byte err = this.client.HSet(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
}
return return
} }
@ -162,9 +123,6 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令 如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令
*/ */
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
var resultvalue []byte err = this.client.HSetNX(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
}
return return
} }

View File

@ -1,83 +1,69 @@
package single package single
import ( import (
"github.com/go-redis/redis/v8" "time"
) )
/* Key *******************************************************************************/ /* Key *******************************************************************************/
///删除redis key ///删除redis key
func (this *Redis) Delete(key string) (err error) { func (this *Redis) Delete(key ...string) (int64, error) {
err = this.client.Do(this.getContext(), "DEL", key).Err() return this.client.Del(this.getContext(), key...).Result()
return
} }
///判断是否存在key ///判断是否存在key
func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { func (this *Redis) Exists(key ...string) (int64, error) {
iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() return this.client.Exists(this.getContext(), key...).Result()
return
} }
///设置key的过期时间 单位以秒级 ///设置key的过期时间 单位以秒级
func (this *Redis) ExpireKey(key string, expire int) (err error) { func (this *Redis) Expire(key string, expire time.Duration) (bool, error) {
err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() return this.client.Expire(this.getContext(), key, expire).Result()
return
} }
///设置key的过期时间戳 秒级时间戳 ///设置key的过期时间戳 秒级时间戳
func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) {
err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() return this.client.ExpireAt(this.getContext(), key, expire).Result()
return
} }
///设置key的过期时间 单位以毫秒级 ///设置key的过期时间 单位以毫秒级
func (this *Redis) Pexpirekey(key string, expire int) (err error) { func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) {
err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() return this.client.PExpire(this.getContext(), key, expire).Result()
return
} }
///设置key的过期时间戳 单位以豪秒级 ///设置key的过期时间戳 单位以豪秒级
func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) {
err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() return this.client.PExpireAt(this.getContext(), key, expire).Result()
return
} }
///移除Key的过期时间 ///移除Key的过期时间
func (this *Redis) PersistKey(key string) (err error) { func (this *Redis) Persist(key string) (bool, error) {
err = this.client.Do(this.getContext(), "PERSIST", key).Err() return this.client.Persist(this.getContext(), key).Result()
return
} }
///获取key剩余过期时间 单位毫秒 ///获取key剩余过期时间 单位毫秒
func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { func (this *Redis) PTTL(key string) (surplus time.Duration, err error) {
leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() return this.client.PTTL(this.getContext(), key).Result()
return
} }
///获取key剩余过期时间 单位秒 ///获取key剩余过期时间 单位秒
func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { func (this *Redis) TTL(key string) (surplus time.Duration, err error) {
leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() return this.client.TTL(this.getContext(), key).Result()
return
} }
///重命名Key ///重命名Key
func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { func (this *Redis) Rename(oldkey string, newkey string) (string, error) {
err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() return this.client.Rename(this.getContext(), oldkey, newkey).Result()
return
} }
///重命名key 在新的 key 不存在时修改 key 的名称 ///重命名key 在新的 key 不存在时修改 key 的名称
func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) {
err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() return this.client.RenameNX(this.getContext(), oldkey, newkey).Result()
return
} }
///判断是否存在key pattern:key* ///判断是否存在key pattern:key*
func (this *Redis) Keys(pattern string) (keys []string, err error) { func (this *Redis) Keys(pattern string) (keys []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) return this.client.Keys(this.getContext(), pattern).Result()
this.client.Process(this.getContext(), cmd)
keys, err = cmd.Result()
return
} }
///获取键类型 ///获取键类型

View File

@ -1,23 +1,14 @@
package single package single
import ( import (
"fmt"
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
/* /*
Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推 Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/ */
func (this *Redis) Lindex(key string, value interface{}) (err error) { func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) {
var data string return this.client.LIndex(this.getContext(), key, index).Scan(v)
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
} }
/* /*
@ -25,90 +16,55 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定
当列表不存在时被视为空列表不执行任何操作 当列表不存在时被视为空列表不执行任何操作
如果 key 不是列表类型返回一个错误 如果 key 不是列表类型返回一个错误
*/ */
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) {
var ( op := "BEFORE"
tagervalue []byte if !isbefore {
resultvalue []byte op = "AFTER"
)
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()
} }
} return this.client.LInsert(this.getContext(), key, op, pivot, value).Result()
}
return
} }
/* /*
Redis Llen 命令用于返回列表的长度 如果列表 key 不存在 key 被解释为一个空列表返回 0 如果 key 不是列表类型返回一个错误 Redis Llen 命令用于返回列表的长度 如果列表 key 不存在 key 被解释为一个空列表返回 0 如果 key 不是列表类型返回一个错误
*/ */
func (this *Redis) Llen(key string) (result int, err error) { func (this *Redis) Llen(key string) (int64, error) {
result, err = this.client.Do(this.getContext(), "LLEN", key).Int() return this.client.LLen(this.getContext(), key).Result()
return
} }
/* /*
Redis Lpop 命令用于移除并返回列表的第一个元素 Redis Lpop 命令用于移除并返回列表的第一个元素
*/ */
func (this *Redis) LPop(key string, value interface{}) (err error) { func (this *Redis) LPop(key string, v interface{}) (err error) {
var data string return this.client.LPop(this.getContext(), key).Scan(v)
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
} }
/* /*
Redis Lpush 命令将一个或多个值插入到列表头部 如果 key 不存在一个空列表会被创建并执行 LPUSH 操作 key 存在但不是列表类型时返回一个错误 Redis Lpush 命令将一个或多个值插入到列表头部 如果 key 不存在一个空列表会被创建并执行 LPUSH 操作 key 存在但不是列表类型时返回一个错误
*/ */
func (this *Redis) LPush(key string, values ...interface{}) (err error) { func (this *Redis) LPush(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.LPush(this.getContext(), key, values...).Result()
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
} }
/* /*
Redis Lpushx 将一个值插入到已存在的列表头部列表不存在时操作无效 Redis Lpushx 将一个值插入到已存在的列表头部列表不存在时操作无效
*/ */
func (this *Redis) LPushX(key string, values ...interface{}) (err error) { func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) // agrs := make([]interface{}, 0)
agrs = append(agrs, "LPUSHX") // agrs = append(agrs, "LPUSHX")
for _, v := range values { // for _, v := range values {
result, _ := this.Encode(v) // result, _ := this.Encode(v)
agrs = append(agrs, result) // agrs = append(agrs, result)
} // }
err = this.client.Do(this.getContext(), agrs...).Err() // err = this.client.Do(this.getContext(), agrs...).Err()
return return this.client.LPushX(this.getContext(), key, values...).Result()
} }
/* /*
Redis Lrange 返回列表中指定区间内的元素区间以偏移量 START END 指定 其中 0 表示列表的第一个元素 1 表示列表的第二个元素 Redis Lrange 返回列表中指定区间内的元素区间以偏移量 START END 指定 其中 0 表示列表的第一个元素 1 表示列表的第二个元素
以此类推 你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推 以此类推 你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/ */
func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd {
var _result []string return this.client.LRange(this.getContext(), key, start, end)
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
} }
/* /*
@ -118,24 +74,16 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,
count < 0 : 从表尾开始向表头搜索移除与 VALUE 相等的元素数量为 COUNT 的绝对值 count < 0 : 从表尾开始向表头搜索移除与 VALUE 相等的元素数量为 COUNT 的绝对值
count = 0 : 移除表中所有与 VALUE 相等的值 count = 0 : 移除表中所有与 VALUE 相等的值
*/ */
func (this *Redis) LRem(key string, count int, target interface{}) (err error) { func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) {
var resultvalue []byte return this.client.LRem(this.getContext(), key, count, v).Result()
if resultvalue, err = this.Encode(target); err == nil {
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
}
return
} }
/* /*
Redis Lset 通过索引来设置元素的值 Redis LSet 通过索引来设置元素的值
当索引参数超出范围或对一个空列表进行 LSET 返回一个错误 当索引参数超出范围或对一个空列表进行 LSET 返回一个错误
*/ */
func (this *Redis) LSet(key string, index int, value interface{}) (err error) { func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) {
var resultvalue []byte return this.client.LSet(this.getContext(), key, index, v).Result()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
}
return
} }
/* /*
@ -143,35 +91,22 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留
下标 0 表示列表的第一个元素 1 表示列表的第二个元素以此类推 你也可以使用负数下标 下标 0 表示列表的第一个元素 1 表示列表的第二个元素以此类推 你也可以使用负数下标
-1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/ */
func (this *Redis) Ltrim(key string, start, stop int) (err error) { func (this *Redis) LTrim(key string, start, stop int64) (string, error) {
err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() return this.client.LTrim(this.getContext(), key, start, stop).Result()
return
} }
/* /*
Redis Rpop 命令用于移除列表的最后一个元素返回值为移除的元素 Redis Rpop 命令用于移除列表的最后一个元素返回值为移除的元素
*/ */
func (this *Redis) Rpop(key string, value interface{}) (err error) { func (this *Redis) RPop(key string, v interface{}) (err error) {
var data string return this.client.RPop(this.getContext(), key).Scan(v)
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
} }
/* /*
Redis Rpoplpush 命令用于移除列表的最后一个元素并将该元素添加到另一个列表并返回 Redis Rpoplpush 命令用于移除列表的最后一个元素并将该元素添加到另一个列表并返回
*/ */
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) {
var data string return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v)
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
} }
/* /*
@ -179,27 +114,13 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)
如果列表不存在一个空列表会被创建并执行 RPUSH 操作 当列表存在但不是列表类型时返回一个错误 如果列表不存在一个空列表会被创建并执行 RPUSH 操作 当列表存在但不是列表类型时返回一个错误
注意 Redis 2.4 版本以前的 RPUSH 命令都只接受单个 value 注意 Redis 2.4 版本以前的 RPUSH 命令都只接受单个 value
*/ */
func (this *Redis) RPush(key string, values ...interface{}) (err error) { func (this *Redis) RPush(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.RPush(this.getContext(), key, values...).Result()
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
} }
/* /*
Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)如果列表不存在操作无效 Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)如果列表不存在操作无效
*/ */
func (this *Redis) RPushX(key string, values ...interface{}) (err error) { func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.RPushX(this.getContext(), key, values...).Result()
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
} }

View File

@ -1,30 +1,23 @@
package single package single
import "reflect" import (
"github.com/go-redis/redis/v8"
)
/* /*
Redis Sadd 命令将一个或多个成员元素加入到集合中已经存在于集合的成员元素将被忽略 Redis Sadd 命令将一个或多个成员元素加入到集合中已经存在于集合的成员元素将被忽略
假如集合 key 不存在则创建一个只包含添加的元素作成员的集合 假如集合 key 不存在则创建一个只包含添加的元素作成员的集合
当集合 key 不是集合类型时返回一个错误 当集合 key 不是集合类型时返回一个错误
*/ */
func (this *Redis) SAdd(key string, values ...interface{}) (err error) { func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) {
agrs := make([]interface{}, 0) return this.client.SAdd(this.getContext(), key, values...).Result()
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
} }
/* /*
Redis Scard 命令返回集合中元素的数量 Redis Scard 命令返回集合中元素的数量
*/ */
func (this *Redis) SCard(key string) (result int64, err error) { func (this *Redis) SCard(key string) (int64, error) {
result, err = this.client.SCard(this.getContext(), key).Result() return this.client.SCard(this.getContext(), key).Result()
return
} }
/* /*
@ -32,79 +25,43 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可
差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集
实例: 实例:
*/ */
func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd {
var _result []string return this.client.SDiff(this.getContext(), keys...)
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
} }
/* /*
Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中 Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中
*/ */
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) {
result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() return this.client.SDiffStore(this.getContext(), destination, keys...).Result()
return
} }
/* /*
Redis Sismember 命令返回给定所有给定集合的交集 不存在的集合 key 被视为空集 当给定集合当中有一个空集时结果也为空集(根据集合运算定律) Redis SInter 命令返回给定所有给定集合的交集 不存在的集合 key 被视为空集 当给定集合当中有一个空集时结果也为空集(根据集合运算定律)
*/ */
func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd {
var _result []string return this.client.SInter(this.getContext(), keys...)
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
} }
/* /*
Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖 Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖
*/ */
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) {
result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() return this.client.SInterStore(this.getContext(), destination, keys...).Result()
return
} }
/* /*
Redis Sismember 命令判断成员元素是否是集合的成员 Redis Sismember 命令判断成员元素是否是集合的成员
*/ */
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { func (this *Redis) SIsMember(key string, value interface{}) (bool, error) {
iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() return this.client.SIsMember(this.getContext(), key, value).Result()
return
} }
/* /*
Redis Smembers 号召返回集合中的所有成员 Redis Smembers 号召返回集合中的所有成员
*/ */
func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { func (this *Redis) SMembers(key string) *redis.StringSliceCmd {
var _result []string return this.client.SMembers(this.getContext(), key)
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
} }
/* /*
@ -114,18 +71,16 @@ SMOVE 是原子性操作。
destination 集合已经包含 member 元素时 SMOVE 命令只是简单地将 source 集合中的 member 元素删除 destination 集合已经包含 member 元素时 SMOVE 命令只是简单地将 source 集合中的 member 元素删除
source destination 不是集合类型时返回一个错误 source destination 不是集合类型时返回一个错误
*/ */
func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) {
result, err = this.client.SMove(this.getContext(), source, destination, member).Result() return this.client.SMove(this.getContext(), source, destination, member).Result()
return
} }
/* /*
Redis Spop命令用于移除集合中的指定键的一个或多个随机元素移除后会返回移除的元素 Redis Spop命令用于移除集合中的指定键的一个或多个随机元素移除后会返回移除的元素
该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动 该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动
*/ */
func (this *Redis) Spop(key string) (result string, err error) { func (this *Redis) SPop(key string) (string, error) {
result, err = this.client.SPop(this.getContext(), key).Result() return this.client.SPop(this.getContext(), key).Result()
return
} }
/* /*
@ -135,9 +90,8 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。
如果 count 为负数那么命令返回一个数组数组中的元素可能会重复出现多次而数组的长度为 count 的绝对值 如果 count 为负数那么命令返回一个数组数组中的元素可能会重复出现多次而数组的长度为 count 的绝对值
该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动 该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动
*/ */
func (this *Redis) Srandmember(key string) (result string, err error) { func (this *Redis) SRandMember(key string) (string, error) {
result, err = this.client.SRandMember(this.getContext(), key).Result() return this.client.SRandMember(this.getContext(), key).Result()
return
} }
/* /*
@ -145,41 +99,27 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在
当键不是集合类型返回一个错误 当键不是集合类型返回一个错误
Redis 2.4 版本以前SREM 只接受个别成员值 Redis 2.4 版本以前SREM 只接受个别成员值
*/ */
func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { func (this *Redis) SRem(key string, members ...interface{}) (int64, error) {
result, err = this.client.SRem(this.getContext(), key, members...).Result() return this.client.SRem(this.getContext(), key, members...).Result()
return
} }
/* /*
Redis Sunion 命令返回给定集合的并集 Redis Sunion 命令返回给定集合的并集
*/ */
func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd {
var _result []string return this.client.SUnion(this.getContext(), keys...)
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
} }
/* /*
Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖 Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖
*/ */
func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) {
result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() return this.client.SUnionStore(this.getContext(), destination, keys...).Result()
return
} }
/* /*
Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan
*/ */
func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) {
keys, cursor, err = this.client.SScan(this.getContext(), key, _cursor, match, count).Result() return this.client.SScan(this.getContext(), key, _cursor, match, count).Result()
return
} }

View File

@ -1,7 +1,6 @@
package single package single
import ( import (
"fmt"
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
@ -11,54 +10,39 @@ import (
/* /*
命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型 命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型
*/ */
func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) {
var result []byte return this.client.Set(this.getContext(), string(key), v, expiration).Result()
if result, err = this.Encode(value); err == nil {
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
}
return
} }
/* /*
指定的 key 不存在时 key 设置指定的值 指定的 key 不存在时 key 设置指定的值
*/ */
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) {
// var _value []byte return this.client.SetNX(this.getContext(), key, v, expiration).Result()
// 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
} }
/* /*
同时设置一个或多个 key-value 同时设置一个或多个 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 := make([]interface{}, 0)
agrs = append(agrs, "MSET") agrs = append(agrs, "MSET")
for k, v := range keyvalues { for k, v := range keyvalues {
result, _ := this.Encode(v) agrs = append(agrs, k, v)
agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() return this.client.Do(this.getContext(), agrs...).Err()
return
} }
/* /*
命令用于所有给定 key 都不存在时同时设置一个或多个 key-value 命令用于所有给定 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 := make([]interface{}, 0)
agrs = append(agrs, "MSETNX") agrs = append(agrs, "MSETNX")
for k, v := range keyvalues { for k, v := range keyvalues {
result, _ := this.Encode(v) agrs = append(agrs, k, v)
agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() return this.client.Do(this.getContext(), agrs...).Err()
return
} }
/* /*
@ -67,9 +51,8 @@ Redis Incr 命令将 key 中储存的数字值增一。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Incr(key string) (err error) { func (this *Redis) Incr(key string) (int64, error) {
err = this.client.Do(this.getContext(), "INCR", key).Err() return this.client.Incr(this.getContext(), key).Result()
return
} }
/* /*
@ -78,18 +61,16 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) IncrBY(key string, value int) (err error) { func (this *Redis) IncrBY(key string, value int64) (int64, error) {
err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() return this.client.IncrBy(this.getContext(), key, value).Result()
return
} }
/* /*
Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值 Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值
如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作 如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作
*/ */
func (this *Redis) Incrbyfloat(key string, value float32) (err error) { func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) {
err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() return this.client.IncrByFloat(this.getContext(), key, value).Result()
return
} }
/* /*
@ -98,9 +79,8 @@ Redis Decr 命令将 key 中储存的数字值减一。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Decr(key string, value int) (err error) { func (this *Redis) Decr(key string, value int) (int64, error) {
err = this.client.Do(this.getContext(), "DECR", key, value).Err() return this.client.Decr(this.getContext(), key).Result()
return
} }
/* /*
@ -109,9 +89,8 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。
如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误 如果值包含错误的类型或字符串类型的值不能表示为数字那么返回一个错误
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) DecrBy(key string, value int) (err error) { func (this *Redis) DecrBy(key string, value int64) (int64, error) {
err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() return this.client.DecrBy(this.getContext(), key, value).Result()
return
} }
/* /*
@ -119,62 +98,28 @@ Redis Append 命令用于为指定的 key 追加值。
如果 key 已经存在并且是一个字符串 APPEND 命令将 value 追加到 key 原来的值的末尾 如果 key 已经存在并且是一个字符串 APPEND 命令将 value 追加到 key 原来的值的末尾
如果 key 不存在 APPEND 就简单地将给定 key 设为 value 就像执行 SET key value 一样 如果 key 不存在 APPEND 就简单地将给定 key 设为 value 就像执行 SET key value 一样
*/ */
func (this *Redis) Append(key string, value interface{}) (err error) { func (this *Redis) Append(key string, value string) (err error) {
var result []byte this.client.Append(this.getContext(), key, value).Result()
if result, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
}
return return
} }
/* /*
命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型 命令用于设置给定 key 的值如果 key 已经存储其他值 SET 就覆写旧值且无视类型
*/ */
func (this *Redis) Get(key string, value interface{}) (err error) { func (this *Redis) Get(key string, v interface{}) (err error) {
var result []byte return this.client.Get(this.getContext(), key).Scan(v)
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
err = this.Decode(result, value)
}
return
} }
/* /*
设置指定 key 的值并返回 key 的旧值 设置指定 key 的值并返回 key 的旧值
*/ */
func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) {
var ( return this.client.GetSet(this.getContext(), key, v).Scan(result)
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
} }
/* /*
返回所有(一个或多个)给定 key 的值 如果给定的 key 里面有某个 key 不存在那么这个 key 返回特殊值 nil 返回所有(一个或多个)给定 key 的值 如果给定的 key 里面有某个 key 不存在那么这个 key 返回特殊值 nil
*/ */
func (this *Redis) MGet(keys ...string) (result []string, err error) { func (this *Redis) MGet(keys ...string) *redis.SliceCmd {
agrs := make([]interface{}, 0) return this.client.MGet(this.getContext(), keys...)
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
} }

View File

@ -1,217 +1,145 @@
package single package single
import ( import (
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
/* /*
Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数 Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数
*/ */
func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) {
this.client.ZAdd(this.getContext(), key, members...) return this.client.ZAdd(this.getContext(), key, members...).Result()
return
} }
/* /*
Redis Zcard 用于计算集合中元素的数量 Redis Zcard 用于计算集合中元素的数量
*/ */
func (this *Redis) ZCard(key string) (result int64, err error) { func (this *Redis) ZCard(key string) (int64, error) {
result, err = this.client.ZCard(this.getContext(), key).Result() return this.client.ZCard(this.getContext(), key).Result()
return
} }
/* /*
Redis ZCount 用于计算集合中指定的范围内的数量 Redis ZCount 用于计算集合中指定的范围内的数量
*/ */
func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { func (this *Redis) ZCount(key string, min string, max string) (int64, error) {
result, err = this.client.ZCount(this.getContext(), key, min, max).Result() return this.client.ZCount(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment
*/ */
func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) {
result, err = this.client.ZIncrBy(this.getContext(), key, increment, member).Result() return this.client.ZIncrBy(this.getContext(), key, increment, member).Result()
return
} }
/* /*
Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination
*/ */
func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) {
result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() return this.client.ZInterStore(this.getContext(), destination, store).Result()
return
} }
/* /*
Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 Redis ZLexCount 在有序集合中计算指定字典区间内成员数量
*/ */
func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) {
result, err = this.client.ZLexCount(this.getContext(), key, min, max).Result() return this.client.ZLexCount(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZRange 通过索引区间返回有序集合指定区间内的成员 Redis ZRange 通过索引区间返回有序集合指定区间内的成员
*/ */
func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd {
var _result []string return this.client.ZRange(this.getContext(), key, start, stop)
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
} }
/* /*
Redis ZRangeByLex 通过字典区间返回有序集合的成员 Redis ZRangeByLex 通过字典区间返回有序集合的成员
*/ */
func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
var _result []string return this.client.ZRangeByLex(this.getContext(), key, opt)
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
} }
/* /*
Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员
*/ */
func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
var _result []string return this.client.ZRangeByScore(this.getContext(), key, opt)
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
} }
/* /*
Redis ZRank 返回有序集合中指定成员的索引 Redis ZRank 返回有序集合中指定成员的索引
*/ */
func (this *Redis) ZRank(key string, member string) (result int64, err error) { func (this *Redis) ZRank(key string, member string) (int64, error) {
result, err = this.client.ZRank(this.getContext(), key, member).Result() return this.client.ZRank(this.getContext(), key, member).Result()
return
} }
/* /*
Redis ZRem 移除有序集合中的一个或多个成员 Redis ZRem 移除有序集合中的一个或多个成员
*/ */
func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) {
result, err = this.client.ZRem(this.getContext(), key, members...).Result() return this.client.ZRem(this.getContext(), key, members...).Result()
return
} }
/* /*
Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员
*/ */
func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) {
result, err = this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员
*/ */
func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) {
result, err = this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result()
return
} }
/* /*
Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员
*/ */
func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) {
result, err = this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result()
return
} }
/* /*
Redis ZRevRange 返回有序集中指定区间内的成员通过索引分数从高到低 ZREVRANGE Redis ZRevRange 返回有序集中指定区间内的成员通过索引分数从高到低 ZREVRANGE
*/ */
func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd {
var _result []string return this.client.ZRevRange(this.getContext(), key, start, stop)
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
} }
/* /*
Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员分数从高到低排序 Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员分数从高到低排序
*/ */
func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
var _result []string return this.client.ZRevRangeByScore(this.getContext(), key, opt)
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
} }
/* /*
Redis ZRevRank 返回有序集中指定分数区间内的成员分数从高到低排序 Redis ZRevRank 返回有序集中指定分数区间内的成员分数从高到低排序
*/ */
func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { func (this *Redis) ZRevRank(key string, member string) (int64, error) {
result, err = this.client.ZRevRank(this.getContext(), key, member).Result() return this.client.ZRevRank(this.getContext(), key, member).Result()
return
} }
/* /*
Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序 Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序
*/ */
func (this *Redis) ZScore(key string, member string) (result float64, err error) { func (this *Redis) ZScore(key string, member string) (float64, error) {
result, err = this.client.ZScore(this.getContext(), key, member).Result() return this.client.ZScore(this.getContext(), key, member).Result()
return
} }
/* /*
Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序 ZUNIONSTORE Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序 ZUNIONSTORE
*/ */
func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) {
result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() return this.client.ZUnionStore(this.getContext(), dest, store).Result()
return
} }
/* /*
Redis ZScan 迭代有序集合中的元素包括元素成员和元素分值 Redis ZScan 迭代有序集合中的元素包括元素成员和元素分值
*/ */
func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) {
keys, cursor, err = this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() return this.client.ZScan(this.getContext(), key, _cursor, match, count).Result()
return
} }

View File

@ -21,7 +21,7 @@ func Test_SysIPV6(t *testing.T) {
return return
} }
fmt.Printf("Redis:succ \n") 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) fmt.Printf("Redis:err:%v \n", err)
} }
} }
@ -37,10 +37,10 @@ func Test_Redis_ExpireatKey(t *testing.T) {
return return
} }
fmt.Printf("Redis:succ \n") 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) 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:err:%v \n", err)
} }
fmt.Printf("Redis:end \n") fmt.Printf("Redis:end \n")
@ -66,8 +66,8 @@ func Test_Redis_SetNX(t *testing.T) {
wg.Add(20) wg.Add(20)
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
go func(index int) { go func(index int) {
result, err := redis.SetNX("liwei1dao", index) result, err := redis.SetNX("liwei1dao", index, -1)
fmt.Printf("Redis index:%d result:%d err:%v \n", index, result, err) fmt.Printf("Redis index:%d result:%v err:%v \n", index, result, err)
wg.Done() wg.Done()
}(i) }(i)
} }

26
modules/cache_com.go Normal file
View File

@ -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
}

26
modules/db_comp.go Normal file
View File

@ -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
}

View File

@ -5,7 +5,6 @@ import (
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/sys/cache"
"time" "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) { func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (err error) {
var ( var (
code pb.ErrorCode code pb.ErrorCode
pack *pb.DB_UserPackData items []*pb.DB_UserItemData
nt int64 nt int64
tempgrids []*pb.DB_GridData tempgrids []*pb.DB_UserItemData
grids []*pb.DB_GridData grids []*pb.DB_UserItemData
modifys []*pb.DB_GridData modifys []*pb.DB_UserItemData
dels []string
) )
defer func() { defer func() {
session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids}) session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids})
if code == pb.ErrorCode_Success { if code == pb.ErrorCode_Success {
go func() { //异步处理修改数据 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 { if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success {
return 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) log.Errorf("QueryUserPackReq err:%v", err)
code = pb.ErrorCode_CacheReadError code = pb.ErrorCode_CacheReadError
return return
} else { } else {
tempgrids = this.module.configure_comp.GetPackItemByType(pack, req.IType) tempgrids = this.module.configure_comp.GetPackItemByType(items, req.IType)
modifys = make([]*pb.DB_GridData, 0, len(tempgrids)) modifys = make([]*pb.DB_UserItemData, 0, len(tempgrids))
grids = make([]*pb.DB_GridData, 0, len(grids)) dels = make([]string, 0, len(tempgrids))
grids = make([]*pb.DB_UserItemData, 0, len(grids))
nt = time.Now().Unix() nt = time.Now().Unix()
for _, v := range tempgrids { for _, v := range tempgrids {
if v.ETime > 0 && v.ETime < nt { //已经过期 if v.ETime > 0 && v.ETime < nt { //已经过期
modifys = append(modifys, &pb.DB_GridData{GridId: v.GridId, IsEmpty: true}) dels = append(dels, v.GridId)
} else { } else {
grids = append(grids, v) grids = append(grids, v)
if v.IsNewItem { if v.IsNewItem {

View File

@ -2,29 +2,47 @@ package pack
import ( import (
"fmt" "fmt"
"go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo" "go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/lego/sys/redis" "go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
///背包缓存数据管理组件 ///背包缓存数据管理组件
type Cache_Comp struct { type Cache_Comp struct {
cbase.ModuleCompBase modules.MComp_CacheComp
redis redis.ISys 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) { func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
pack = &pb.DB_UserPackData{ var (
UserId: uId, 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)
} }
if err = this.redis.Get(fmt.Sprintf(Redis_PackCache, uId), pack); err == nil {
return return
} else if err == redis.RedisNil { } else if err == redis.RedisNil {
if pack, err = db.Defsys.Pack_QueryUserPack(uId); err == nil { if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil {
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) 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 { } else if err == mgo.MongodbNil {
err = nil err = nil
} }
@ -32,16 +50,67 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData
return 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) { func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
var ( var (
pack *pb.DB_UserPackData itmes []*pb.DB_UserItemData
err error err error
) )
if pack, err = this.Pack_QueryUserPack(uId); err != nil { if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
return return
} }
for _, v := range pack.Pack { for _, v := range itmes {
if !v.IsEmpty && v.ItemId == itemid { if !v.IsEmpty && v.ItemId == itemid {
amount += v.Amount 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) { func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
var ( var (
pack *pb.DB_UserPackData itmes []*pb.DB_UserItemData
modifys []*pb.DB_GridData modifys []*pb.DB_UserItemData
leftnum int64 leftnum int64
) )
if addnum == 0 { if addnum == 0 {
return return
} }
if pack, err = this.Pack_QueryUserPack(uId); err != nil { if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
return return
} }
modifys, leftnum = this.pack_addItemToUserPack(pack, itemId, addnum) modifys, leftnum = this.pack_addItemToUserPack(itmes, itemId, addnum)
if leftnum < 0 { if leftnum < 0 {
err = ItemNotEnoughError err = ItemNotEnoughError
return return
@ -70,26 +139,24 @@ func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum
err = PackGridNumUpper err = PackGridNumUpper
return return
} }
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { this.Pack_UpdateUserPack(uId, modifys...)
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
}
return return
} }
///添加或则减少多个物品到用户背包 ///添加或则减少多个物品到用户背包
func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) { func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
var ( var (
pack *pb.DB_UserPackData itmes []*pb.DB_UserItemData
modifys []*pb.DB_GridData modifys []*pb.DB_UserItemData
tempmodifys []*pb.DB_GridData tempmodifys []*pb.DB_UserItemData
leftnum int64 leftnum int64
iskeep bool iskeep bool
) )
if pack, err = this.Pack_QueryUserPack(uId); err != nil { if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
return return
} }
for k, v := range items { for k, v := range items {
tempmodifys, leftnum = this.pack_addItemToUserPack(pack, k, v) tempmodifys, leftnum = this.pack_addItemToUserPack(itmes, k, v)
if leftnum < 0 { if leftnum < 0 {
err = ItemNotEnoughError err = ItemNotEnoughError
return 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.Pack_UpdateUserPack(uId, modifys...)
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
}
return 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 ( var (
pack *pb.DB_UserPackData itme *pb.DB_UserItemData
grid *pb.DB_GridData grid *pb.DB_UserItemData
num int64 num int64
amount int64 amount int64
) )
if addnum == 0 { if addnum == 0 {
return return
} }
if pack, err = this.Pack_QueryUserPack(uId); err != nil { if itme, err = this.Pack_QueryUserPackByGridId(uId, gridid); err != nil {
return return
} }
if int32(len(pack.Pack)) <= gridid { amount = int64(itme.Amount)
err = NoFoundGirdError
return
}
grid = pack.Pack[gridid]
if grid == nil {
err = NoFoundGirdError
return
}
amount = int64(grid.Amount)
if grid.IsEmpty { if grid.IsEmpty {
amount = 0 amount = 0
} else if grid.ItemId != itemId { } else if grid.ItemId != itemId {
@ -153,38 +209,15 @@ func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid int32, i
err = GirdAmountUpper err = GirdAmountUpper
return return
} else { } else {
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grid); err == nil { itme.Amount = uint32(num)
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) 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 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 ( var (
num int64 num int64
isNew bool isNew bool
@ -194,8 +227,8 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId
} }
isNew = true isNew = true
leftnum = int64(addnum) leftnum = int64(addnum)
modifys = make([]*pb.DB_GridData, 0) modifys = make([]*pb.DB_UserItemData, 0)
for _, v := range pack.Pack { for _, v := range items {
if !v.IsEmpty && v.ItemId == itemId { if !v.IsEmpty && v.ItemId == itemId {
isNew = false isNew = false
num = int64(v.Amount) + int64(leftnum) num = int64(v.Amount) + int64(leftnum)
@ -234,7 +267,7 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId
return return
} }
if leftnum > 0 { //还没有放完 寻找空的格子填充 if leftnum > 0 { //还没有放完 寻找空的格子填充
for _, v := range pack.Pack { for _, v := range items {
if v.IsEmpty { if v.IsEmpty {
if leftnum <= GridCapMaxNum { if leftnum <= GridCapMaxNum {
v.IsEmpty = false 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 { //需要补充格子 for leftnum > 0 { //需要补充格子
if leftnum <= GridCapMaxNum { if leftnum <= GridCapMaxNum {
grid := &pb.DB_GridData{ grid := &pb.DB_UserItemData{
GridId: index, GridId: primitive.NewObjectID().Hex(),
IsEmpty: false, IsEmpty: false,
ItemId: itemId, ItemId: itemId,
Amount: uint32(leftnum), Amount: uint32(leftnum),
IsNewItem: isNew, IsNewItem: isNew,
} }
pack.Pack = append(pack.Pack, grid) items = append(items, grid)
modifys = append(modifys, grid) modifys = append(modifys, grid)
leftnum = 0 leftnum = 0
break break
} else { } else {
leftnum -= GridCapMaxNum leftnum -= GridCapMaxNum
grid := &pb.DB_GridData{ grid := &pb.DB_UserItemData{
GridId: index, GridId: primitive.NewObjectID().Hex(),
IsEmpty: false, IsEmpty: false,
ItemId: itemId, ItemId: itemId,
Amount: uint32(GridCapMaxNum), Amount: uint32(GridCapMaxNum),
IsNewItem: isNew, IsNewItem: isNew,
} }
pack.Pack = append(pack.Pack, grid) items = append(items, grid)
modifys = append(modifys, grid) modifys = append(modifys, grid)
index++ index++
} }

View File

@ -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) { func (this *Configure_Comp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetype int32) (result []*pb.DB_UserItemData) {
result = make([]*pb.DB_GridData, 0, len(pack.Pack)) result = make([]*pb.DB_UserItemData, 0, len(itmes))
var ( var (
v interface{} v interface{}
table *cfg.Game_item table *cfg.Game_item
@ -59,7 +59,7 @@ func (this *Configure_Comp) GetPackItemByType(pack *pb.DB_UserPackData, usetype
return return
} else { } else {
table = v.(*cfg.Game_item) table = v.(*cfg.Game_item)
for _, v := range pack.Pack { for _, v := range itmes {
if !v.IsEmpty { if !v.IsEmpty {
if item, ok = table.GetDataMap()[id]; ok { if item, ok = table.GetDataMap()[id]; ok {
if item.Usetype == usetype { if item.Usetype == usetype {

View File

@ -1,73 +1,82 @@
package pack package pack
import ( import (
"fmt" "context"
"go_dreamfactory/lego/core/cbase" "go_dreamfactory/modules"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
///背包数据库数据管理组件 ///背包数据库数据管理组件
type DB_Comp struct { type DB_Comp struct {
cbase.ModuleCompBase modules.MComp_DBComp
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
} }
///查询用户背包数据 ///查询用户背包数据
func (this *DB_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) { func (this *DB_Comp) Pack_QueryUserPack(uId string) (items []*pb.DB_UserItemData, err error) {
pack = &pb.DB_UserPackData{} var (
err = this.mgo.FindOne(DB_PackTable, bson.M{"_id": uId}).Decode(pack) 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 return
} }
//更新背包格子数据 //更新背包格子数据
func (this *DB_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) { func (this *DB_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_UserItemData) (err error) {
pack = &pb.DB_UserPackData{} models := make([]mongo.WriteModel, len(grids))
update := bson.M{} for i, v := range grids {
for _, v := range grids { models[i] = mongo.NewUpdateOneModel().SetFilter(bson.M{"_id": v.GridId}).SetUpdate(
update[fmt.Sprintf("pack.%d.gridid", v.GridId)] = v.GridId bson.M{"$set": bson.M{
update[fmt.Sprintf("pack.%d.isempty", v.GridId)] = v.IsEmpty "isempty": v.IsEmpty,
update[fmt.Sprintf("pack.%d.itemid", v.GridId)] = v.ItemId "itemid": v.ItemId,
update[fmt.Sprintf("pack.%d.amount", v.GridId)] = v.Amount "amount": v.Amount,
update[fmt.Sprintf("pack.%d.isnewitem", v.GridId)] = v.IsNewItem "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}, func (this *DB_Comp) Pack_DeleteGridToUserPack(uId string, grids ...string) (err error) {
bson.M{"$set": update}, _, err = this.DB.DeleteMany(DB_PackTable, bson.M{"_id": bson.M{"$inc": grids}})
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
return return
} }
//更新背包格子物品的数据 //更新背包格子物品的数据
func (this *DB_Comp) Pack_AddGridAmountToUserPack(uId string, grid int32, amount int32) (pack *pb.DB_UserPackData, err error) { func (this *DB_Comp) Pack_AddGridAmountToUserPack(grid int32, amount int32) (pack *pb.DB_UserItemData, err error) {
pack = &pb.DB_UserPackData{} pack = &pb.DB_UserItemData{}
err = this.mgo.FindOneAndUpdate(DB_PackTable, err = this.DB.FindOneAndUpdate(DB_PackTable,
bson.M{"_id": uId}, bson.M{"_id": grid},
bson.M{"$inc": bson.M{ bson.M{"$set": bson.M{
fmt.Sprintf("pack.%d.amount", grid): amount, "amount": amount,
}}, }},
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack) options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
return return
} }
//修改背包格子IsNew标识 //修改背包格子IsNew标识
func (this *DB_Comp) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) { func (this *DB_Comp) Pack_ModifyPackGridIsNewItem(grids []int32) (err error) {
pack = &pb.DB_UserPackData{} _, err = this.DB.UpdateMany(DB_PackTable,
identifier := []interface{}{bson.M{"item.gridid": bson.M{"$in": grids}}} bson.M{"_id": bson.M{"$inc": grids}},
err = this.mgo.FindOneAndUpdate(DB_PackTable,
bson.M{"_id": uId},
bson.M{"$set": bson.M{ bson.M{"$set": bson.M{
"pack.$[item].isNewitem": false, "isNewitem": false,
}}, options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{Filters: identifier}).SetReturnDocument(options.After)).Decode(pack) }})
return return
} }

View File

@ -3,11 +3,8 @@ package pack
import ( import (
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/modules" "go_dreamfactory/modules"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
) )
@ -24,6 +21,8 @@ func NewModule() core.IModule {
type Pack struct { type Pack struct {
modules.ModuleBase modules.ModuleBase
api_comp *Api_Comp //背包模块 协议处理组件 api_comp *Api_Comp //背包模块 协议处理组件
cache_comp *Cache_Comp //缓存组件
db_comp *DB_Comp //存储组件
configure_comp *Configure_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) { func (this *Pack) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options) err = this.ModuleBase.Init(service, module, options)
event.RegisterGO(comm.Event_CreateUser, this.event_CreateUser)
return return
} }
@ -43,6 +41,8 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core.
func (this *Pack) OnInstallComp() { func (this *Pack) OnInstallComp() {
this.ModuleBase.OnInstallComp() this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) 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) 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) { func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount) 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 return
} }
///添加单个物品到背包 (可以加物品和减物品) ///添加单个物品到背包 (可以加物品和减物品)
func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) { 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) 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) log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
} }
return 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) { 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) 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) log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
} }
return return
} }
//Evens-------------------------------------------------------------------------------------------------------------------------------- //Evens--------------------------------------------------------------------------------------------------------------------------------
//接收玩家创建角色事件
func (this *Pack) event_CreateUser(uid string) {
if _, err := db.Defsys.Pack_InitUserPack(uid); err != nil {
log.Errorf("event_CreateUser err:%v", err)
}
}

View File

@ -21,22 +21,23 @@ const (
) )
//背包格子 //背包格子
type DB_GridData struct { type DB_UserItemData struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
GridId int32 `protobuf:"varint,1,opt,name=GridId,proto3" json:"GridId,omitempty"` //背包格子Id 所在背包数组的下表 GridId string `protobuf:"bytes,1,opt,name=GridId,proto3" json:"GridId,omitempty"` //背包格子Id
IsEmpty bool `protobuf:"varint,2,opt,name=IsEmpty,proto3" json:"IsEmpty,omitempty"` //是否是空格子 UId string `protobuf:"bytes,2,opt,name=UId,proto3" json:"UId,omitempty"` //用户id
ItemId int32 `protobuf:"varint,3,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id IsEmpty bool `protobuf:"varint,3,opt,name=IsEmpty,proto3" json:"IsEmpty,omitempty"` //是否是空格子
Amount uint32 `protobuf:"varint,4,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量 ItemId int32 `protobuf:"varint,4,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id
CTime int64 `protobuf:"varint,5,opt,name=CTime,proto3" json:"CTime,omitempty"` //物品获取时间 Amount uint32 `protobuf:"varint,5,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量
ETime int64 `protobuf:"varint,6,opt,name=ETime,proto3" json:"ETime,omitempty"` //物品过期时间 CTime int64 `protobuf:"varint,6,opt,name=CTime,proto3" json:"CTime,omitempty"` //物品获取时间
IsNewItem bool `protobuf:"varint,7,opt,name=IsNewItem,proto3" json:"IsNewItem,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() { func (x *DB_UserItemData) Reset() {
*x = DB_GridData{} *x = DB_UserItemData{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_pack_pack_db_proto_msgTypes[0] mi := &file_pack_pack_db_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 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) 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] mi := &file_pack_pack_db_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
@ -62,138 +63,86 @@ func (x *DB_GridData) ProtoReflect() protoreflect.Message {
return mi.MessageOf(x) return mi.MessageOf(x)
} }
// Deprecated: Use DB_GridData.ProtoReflect.Descriptor instead. // Deprecated: Use DB_UserItemData.ProtoReflect.Descriptor instead.
func (*DB_GridData) Descriptor() ([]byte, []int) { func (*DB_UserItemData) Descriptor() ([]byte, []int) {
return file_pack_pack_db_proto_rawDescGZIP(), []int{0} return file_pack_pack_db_proto_rawDescGZIP(), []int{0}
} }
func (x *DB_GridData) GetGridId() int32 { func (x *DB_UserItemData) GetGridId() string {
if x != nil { if x != nil {
return x.GridId 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 { if x != nil {
return x.IsEmpty return x.IsEmpty
} }
return false return false
} }
func (x *DB_GridData) GetItemId() int32 { func (x *DB_UserItemData) GetItemId() int32 {
if x != nil { if x != nil {
return x.ItemId return x.ItemId
} }
return 0 return 0
} }
func (x *DB_GridData) GetAmount() uint32 { func (x *DB_UserItemData) GetAmount() uint32 {
if x != nil { if x != nil {
return x.Amount return x.Amount
} }
return 0 return 0
} }
func (x *DB_GridData) GetCTime() int64 { func (x *DB_UserItemData) GetCTime() int64 {
if x != nil { if x != nil {
return x.CTime return x.CTime
} }
return 0 return 0
} }
func (x *DB_GridData) GetETime() int64 { func (x *DB_UserItemData) GetETime() int64 {
if x != nil { if x != nil {
return x.ETime return x.ETime
} }
return 0 return 0
} }
func (x *DB_GridData) GetIsNewItem() bool { func (x *DB_UserItemData) GetIsNewItem() bool {
if x != nil { if x != nil {
return x.IsNewItem return x.IsNewItem
} }
return false 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 protoreflect.FileDescriptor
var file_pack_pack_db_proto_rawDesc = []byte{ var file_pack_pack_db_proto_rawDesc = []byte{
0x0a, 0x12, 0x70, 0x61, 0x63, 0x6b, 0x2f, 0x70, 0x61, 0x63, 0x6b, 0x5f, 0x64, 0x62, 0x2e, 0x70, 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, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xcf, 0x01, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72,
0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x49, 0x74, 0x65, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64,
0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64,
0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x49, 0x12, 0x10, 0x0a, 0x03, 0x55, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x55,
0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x18, 0x03, 0x20,
0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x01, 0x28, 0x08, 0x52, 0x07, 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x16, 0x0a, 0x06,
0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74,
0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05,
0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05,
0x45, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x45, 0x54, 0x69, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x43, 0x54, 0x69,
0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x18, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28,
0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x03, 0x52, 0x05, 0x45, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x49, 0x73, 0x4e, 0x65,
0x22, 0x4b, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x50, 0x61, 0x63, 0x6b, 0x44, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x49, 0x73, 0x4e,
0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06,
0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x04, 0x50, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
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,
} }
var ( var (
@ -208,18 +157,16 @@ func file_pack_pack_db_proto_rawDescGZIP() []byte {
return file_pack_pack_db_proto_rawDescData 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{}{ var file_pack_pack_db_proto_goTypes = []interface{}{
(*DB_GridData)(nil), // 0: DB_GridData (*DB_UserItemData)(nil), // 0: DB_UserItemData
(*DB_UserPackData)(nil), // 1: DB_UserPackData
} }
var file_pack_pack_db_proto_depIdxs = []int32{ var file_pack_pack_db_proto_depIdxs = []int32{
0, // 0: DB_UserPackData.Pack:type_name -> DB_GridData 0, // [0:0] is the sub-list for method output_type
1, // [1:1] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type
1, // [1:1] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee
1, // [1:1] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name
0, // [0:1] is the sub-list for field type_name
} }
func init() { file_pack_pack_db_proto_init() } func init() { file_pack_pack_db_proto_init() }
@ -229,19 +176,7 @@ func file_pack_pack_db_proto_init() {
} }
if !protoimpl.UnsafeEnabled { if !protoimpl.UnsafeEnabled {
file_pack_pack_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { file_pack_pack_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DB_GridData); i { switch v := v.(*DB_UserItemData); 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 {
case 0: case 0:
return &v.state return &v.state
case 1: case 1:
@ -259,7 +194,7 @@ func file_pack_pack_db_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_pack_pack_db_proto_rawDesc, RawDescriptor: file_pack_pack_db_proto_rawDesc,
NumEnums: 0, NumEnums: 0,
NumMessages: 2, NumMessages: 1,
NumExtensions: 0, NumExtensions: 0,
NumServices: 0, NumServices: 0,
}, },

View File

@ -26,7 +26,7 @@ type GetlistReq struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields 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() { func (x *GetlistReq) Reset() {
@ -74,7 +74,7 @@ type GetlistResp struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields 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() { func (x *GetlistResp) Reset() {
@ -109,7 +109,7 @@ func (*GetlistResp) Descriptor() ([]byte, []int) {
return file_pack_pack_msg_proto_rawDescGZIP(), []int{1} return file_pack_pack_msg_proto_rawDescGZIP(), []int{1}
} }
func (x *GetlistResp) GetGrids() []*DB_GridData { func (x *GetlistResp) GetGrids() []*DB_UserItemData {
if x != nil { if x != nil {
return x.Grids 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, 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, 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, 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, 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, 0x22, 0x0a, 0x05, 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, 0x0c, 0x2e, 0x44, 0x42, 0x47, 0x72, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x44, 0x42,
0x5f, 0x47, 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x05, 0x47, 0x72, 0x69, 0x64, 0x73, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x49, 0x74, 0x65, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x52, 0x05, 0x47,
0x22, 0x54, 0x0a, 0x0a, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x72, 0x69, 0x64, 0x73, 0x22, 0x54, 0x0a, 0x0a, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52,
0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01,
0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74,
0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d,
0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01,
0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x0d, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x0d, 0x0a, 0x0b, 0x55, 0x73,
0x6d, 0x52, 0x65, 0x73, 0x70, 0x22, 0x55, 0x0a, 0x0b, 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x65, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x22, 0x55, 0x0a, 0x0b, 0x53, 0x65, 0x6c,
0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x6c, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64,
0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64,
0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05,
0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75,
0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x0e, 0x0a, 0x0c, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74,
0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x70,
0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@ -369,10 +369,10 @@ var file_pack_pack_msg_proto_goTypes = []interface{}{
(*UseItemResp)(nil), // 3: UseItemResp (*UseItemResp)(nil), // 3: UseItemResp
(*SellItemReq)(nil), // 4: SellItemReq (*SellItemReq)(nil), // 4: SellItemReq
(*SellItemResp)(nil), // 5: SellItemResp (*SellItemResp)(nil), // 5: SellItemResp
(*DB_GridData)(nil), // 6: DB_GridData (*DB_UserItemData)(nil), // 6: DB_UserItemData
} }
var file_pack_pack_msg_proto_depIdxs = []int32{ 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 output_type
1, // [1:1] is the sub-list for method input_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 type_name

View File

@ -3,18 +3,13 @@ option go_package = ".;pb";
// //
message DB_GridData { message DB_UserItemData {
int32 GridId = 1; //Id string GridId = 1; //Id
bool IsEmpty = 2; // string UId = 2; //id
int32 ItemId = 3; //Id bool IsEmpty = 3; //
uint32 Amount = 4; // int32 ItemId = 4; //Id
int64 CTime = 5; // uint32 Amount = 5; //
int64 ETime = 6; // int64 CTime = 6; //
bool IsNewItem = 7; // int64 ETime = 7; //
} bool IsNewItem = 8; //
//
message DB_UserPackData {
string UserId = 1; // @go_tags(`bson:"_id"`) Id
repeated DB_GridData Pack = 2; //
} }

View File

@ -9,7 +9,7 @@ message GetlistReq {
// //
message GetlistResp { message GetlistResp {
repeated DB_GridData Grids = 1; // repeated DB_UserItemData Grids = 1; //
} }
//使 //使

View File

@ -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')

1
sys/cache/core.go vendored
View File

@ -7,7 +7,6 @@ redis 缓存数据管理系统
type ( type (
ISys interface { ISys interface {
IUser //户模块的相关缓存接口 IUser //户模块的相关缓存接口
IPack //背包模块的线管缓存接口
IFriend //好友相关的缓存接口 IFriend //好友相关的缓存接口
} }
) )

2
sys/cache/friend.go vendored
View File

@ -26,7 +26,7 @@ type IFriend interface {
func (this *Cache) Friend_Update(data *pb.Cache_FriendData) (err error) { func (this *Cache) Friend_Update(data *pb.Cache_FriendData) (err error) {
if err = db.Defsys.Friend_SaveOrUpdate(data); err == nil { 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 return
} }

315
sys/cache/pack.go vendored
View File

@ -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
}

View File

@ -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)
}

2
sys/cache/user.go vendored
View File

@ -20,7 +20,7 @@ type IUser interface {
func (this *Cache) Update(data *pb.Cache_UserData) (err error) { func (this *Cache) Update(data *pb.Cache_UserData) (err error) {
err = db.Defsys.User_Update(data.UserData) err = db.Defsys.User_Update(data.UserData)
if err == nil { 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 return
} }

View File

@ -3,7 +3,6 @@ package db
type ( type (
ISys interface { ISys interface {
IUser IUser
IPack
IMail IMail
IFriend IFriend
} }

View File

@ -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
}

View File

@ -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)
}