优化redis 数据编解码
This commit is contained in:
parent
c3ae56797a
commit
6149c29e86
@ -1,9 +1,6 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go_dreamfactory/lego/utils/codec"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,13 +35,12 @@ func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "HMSET")
|
agrs = append(agrs, "HMSET")
|
||||||
agrs = append(agrs, key)
|
agrs = append(agrs, key)
|
||||||
var data map[string][]byte
|
var data map[string]string
|
||||||
if data, err = this.encode.EncoderToMap(v); err != nil {
|
if data, err = this.encode.EncoderToMapString(v); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for k, v := range data {
|
for k, v := range data {
|
||||||
result, _ := this.encode.Encoder(v)
|
agrs = append(agrs, k, v)
|
||||||
agrs = append(agrs, k, result)
|
|
||||||
}
|
}
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
return
|
return
|
||||||
@ -58,7 +54,7 @@ func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
|
|||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
var _result string
|
var _result string
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = this.decode.Decoder(codec.StringToBytes(_result), v)
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -72,7 +68,7 @@ func (this *Redis) HGetAll(key string, v interface{}) (err error) {
|
|||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
var _result map[string]string
|
var _result map[string]string
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = this.decode.DecoderMap(_result, v)
|
err = this.decode.DecoderMapString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -121,24 +117,18 @@ 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, v interface{}, fields ...string) (err error) {
|
||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "HMGET")
|
agrs = append(agrs, "HMGET")
|
||||||
agrs = append(agrs, key)
|
agrs = append(agrs, key)
|
||||||
for _, v := range fields {
|
for _, v := range fields {
|
||||||
agrs = append(agrs, v)
|
agrs = append(agrs, v)
|
||||||
}
|
}
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
|
cmd := redis.NewStringStringMapCmd(this.getContext(), agrs...)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
var _result []string
|
var _result map[string]string
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderMapString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
///删除redis key
|
///删除redis key
|
||||||
func (this *Redis) Delete(key string) (err error) {
|
func (this *Redis) Delete(key string) (err error) {
|
||||||
err = this.client.Do(this.getContext(), "DEL", key).Err()
|
err = this.client.Do(this.getContext(), "DEL", key).Err()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,18 @@
|
|||||||
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, v interface{}) (err error) {
|
||||||
var data string
|
cmd := redis.NewStringCmd(this.getContext(), "LINDEX", key)
|
||||||
if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -27,17 +24,19 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定
|
|||||||
*/
|
*/
|
||||||
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) {
|
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) {
|
||||||
var (
|
var (
|
||||||
tagervalue []byte
|
tagervalue string
|
||||||
resultvalue []byte
|
resultvalue string
|
||||||
)
|
)
|
||||||
if tagervalue, err = this.encode.Encoder(tager); err == nil {
|
if tagervalue, err = this.encode.EncoderString(tager); err != nil {
|
||||||
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
return
|
||||||
if isbefore {
|
}
|
||||||
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
|
if resultvalue, err = this.encode.EncoderString(value); err != nil {
|
||||||
} else {
|
return
|
||||||
err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err()
|
}
|
||||||
}
|
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
|
return
|
||||||
}
|
}
|
||||||
@ -53,12 +52,12 @@ func (this *Redis) Llen(key string) (result int, err error) {
|
|||||||
/*
|
/*
|
||||||
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
|
cmd := redis.NewStringCmd(this.getContext(), "LPOP", key)
|
||||||
if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -70,7 +69,7 @@ func (this *Redis) LPush(key string, values ...interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "LPUSH")
|
agrs = append(agrs, "LPUSH")
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -84,7 +83,7 @@ func (this *Redis) LPushX(key string, values ...interface{}) (err 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.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -95,18 +94,12 @@ func (this *Redis) LPushX(key string, values ...interface{}) (err error) {
|
|||||||
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 int, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end)
|
cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -119,10 +112,11 @@ count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,
|
|||||||
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 int, target interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue string
|
||||||
if resultvalue, err = this.encode.Encoder(target); err == nil {
|
if resultvalue, err = this.encode.EncoderString(target); err != nil {
|
||||||
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,10 +125,11 @@ Redis Lset 通过索引来设置元素的值。
|
|||||||
当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误
|
当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误
|
||||||
*/
|
*/
|
||||||
func (this *Redis) LSet(key string, index int, value interface{}) (err error) {
|
func (this *Redis) LSet(key string, index int, value interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue string
|
||||||
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
if resultvalue, err = this.encode.EncoderString(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,12 +146,12 @@ func (this *Redis) Ltrim(key string, start, stop int) (err error) {
|
|||||||
/*
|
/*
|
||||||
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
|
cmd := redis.NewStringCmd(this.getContext(), "RPOP", key)
|
||||||
if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -164,12 +159,12 @@ func (this *Redis) Rpop(key string, value interface{}) (err error) {
|
|||||||
/*
|
/*
|
||||||
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
|
cmd := redis.NewStringCmd(this.getContext(), "RPOPLPUSH", oldkey, newkey)
|
||||||
if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -183,7 +178,7 @@ func (this *Redis) RPush(key string, values ...interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "RPUSH")
|
agrs = append(agrs, "RPUSH")
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -197,7 +192,7 @@ func (this *Redis) RPushX(key string, values ...interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "RPUSHX")
|
agrs = append(agrs, "RPUSHX")
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import "reflect"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。
|
Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。
|
||||||
假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。
|
假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。
|
||||||
@ -12,7 +10,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
|
|||||||
agrs = append(agrs, "SADD")
|
agrs = append(agrs, "SADD")
|
||||||
agrs = append(agrs, key)
|
agrs = append(agrs, key)
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -32,17 +30,11 @@ 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(v interface{}, keys ...string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SDiff(this.getContext(), keys...)
|
cmd := this.client.SDiff(this.getContext(), keys...)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -58,17 +50,11 @@ func (this *Redis) SDiffStore(destination string, keys ...string) (result int64,
|
|||||||
/*
|
/*
|
||||||
Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
|
Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) {
|
func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SInter(this.getContext(), keys...)
|
cmd := this.client.SInter(this.getContext(), keys...)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -92,17 +78,11 @@ func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err er
|
|||||||
/*
|
/*
|
||||||
Redis Smembers 号召返回集合中的所有成员。
|
Redis Smembers 号召返回集合中的所有成员。
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) {
|
func (this *Redis) SMembers(v interface{}, key string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SMembers(this.getContext(), key)
|
cmd := this.client.SMembers(this.getContext(), key)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -153,17 +133,11 @@ func (this *Redis) SRem(key string, members ...interface{}) (result int64, err e
|
|||||||
/*
|
/*
|
||||||
Redis Sunion 命令返回给定集合的并集。
|
Redis Sunion 命令返回给定集合的并集。
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) {
|
func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SUnion(this.getContext(), keys...)
|
cmd := this.client.SUnion(this.getContext(), keys...)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
@ -12,10 +11,11 @@ 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, value interface{}, expiration time.Duration) (err error) {
|
||||||
var result []byte
|
var result string
|
||||||
if result, err = this.encode.Encoder(value); err == nil {
|
if result, err = this.encode.EncoderString(value); err != nil {
|
||||||
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Set(this.getContext(), key, result, expiration).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,9 +23,6 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
|
|||||||
指定的 key 不存在时,为 key 设置指定的值
|
指定的 key 不存在时,为 key 设置指定的值
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
|
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
|
||||||
// var _value []byte
|
|
||||||
// if result, err = this.Encode(value); err == nil {
|
|
||||||
// err = this.client.Do(this.getContext(), "SETNX", key, result).Err()
|
|
||||||
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value)
|
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
result, err = cmd.Result()
|
result, err = cmd.Result()
|
||||||
@ -36,11 +33,11 @@ func (this *Redis) SetNX(key string, value interface{}) (result int64, err error
|
|||||||
/*
|
/*
|
||||||
同时设置一个或多个 key-value 对
|
同时设置一个或多个 key-value 对
|
||||||
*/
|
*/
|
||||||
func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) {
|
func (this *Redis) MSet(v map[string]interface{}) (err 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 v {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(v)
|
||||||
agrs = append(agrs, k, result)
|
agrs = append(agrs, k, result)
|
||||||
}
|
}
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
@ -50,11 +47,11 @@ func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) {
|
|||||||
/*
|
/*
|
||||||
命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对
|
命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对
|
||||||
*/
|
*/
|
||||||
func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) {
|
func (this *Redis) MSetNX(v map[string]interface{}) (err 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 v {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(v)
|
||||||
agrs = append(agrs, k, result)
|
agrs = append(agrs, k, result)
|
||||||
}
|
}
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
@ -120,10 +117,11 @@ Redis Append 命令用于为指定的 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 interface{}) (err error) {
|
||||||
var result []byte
|
var result string
|
||||||
if result, err = this.encode.Encoder(value); err == nil {
|
if result, err = this.encode.EncoderString(value); err != nil {
|
||||||
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,9 +129,9 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
|
|||||||
命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型
|
命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型
|
||||||
*/
|
*/
|
||||||
func (this *Redis) Get(key string, value interface{}) (err error) {
|
func (this *Redis) Get(key string, value interface{}) (err error) {
|
||||||
var result []byte
|
var result string
|
||||||
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
|
if result, err = this.client.Get(this.getContext(), key).Result(); err == nil {
|
||||||
err = this.decode.Decoder(result, value)
|
err = this.decode.DecoderString(result, value)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -143,14 +141,14 @@ 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) {
|
||||||
var (
|
var (
|
||||||
data string
|
_value string
|
||||||
_value []byte
|
|
||||||
)
|
)
|
||||||
if _value, err = this.encode.Encoder(value); err == nil {
|
if _value, err = this.encode.EncoderString(value); err == nil {
|
||||||
if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) {
|
cmd := redis.NewStringCmd(this.getContext(), "GETSET", key, _value)
|
||||||
err = this.decode.Decoder([]byte(data), result)
|
this.client.Process(this.getContext(), cmd)
|
||||||
} else {
|
var _result string
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
if _result, err = cmd.Result(); err == nil {
|
||||||
|
err = this.decode.DecoderString(_result, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -159,7 +157,7 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
|
|||||||
/*
|
/*
|
||||||
返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil
|
返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil
|
||||||
*/
|
*/
|
||||||
func (this *Redis) MGet(keys ...string) (result []string, err error) {
|
func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
|
||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "MGET")
|
agrs = append(agrs, "MGET")
|
||||||
for _, v := range keys {
|
for _, v := range keys {
|
||||||
@ -167,7 +165,11 @@ func (this *Redis) MGet(keys ...string) (result []string, err error) {
|
|||||||
}
|
}
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
|
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
result, err = cmd.Result()
|
var result []string
|
||||||
|
if result, err = cmd.Result(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = this.decode.DecoderSliceString(result, v)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,17 +55,11 @@ func (this *Redis) ZLexCount(key string, min string, max string) (result int64,
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRange(this.getContext(), key, start, stop)
|
cmd := this.client.ZRange(this.getContext(), key, start, stop)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -75,17 +67,11 @@ func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRangeByLex(this.getContext(), key, opt)
|
cmd := this.client.ZRangeByLex(this.getContext(), key, opt)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -93,17 +79,11 @@ func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZR
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRangeByScore(this.getContext(), key, opt)
|
cmd := this.client.ZRangeByScore(this.getContext(), key, opt)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -151,17 +131,11 @@ func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRevRange(this.getContext(), key, start, stop)
|
cmd := this.client.ZRevRange(this.getContext(), key, start, stop)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -169,17 +143,11 @@ func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, st
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt)
|
cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
@ -44,7 +43,7 @@ type (
|
|||||||
Append(key string, value interface{}) (err error)
|
Append(key string, value interface{}) (err error)
|
||||||
Get(key string, value interface{}) (err error)
|
Get(key string, value interface{}) (err error)
|
||||||
GetSet(key string, value interface{}, result interface{}) (err error)
|
GetSet(key string, value interface{}, result interface{}) (err error)
|
||||||
MGet(keys ...string) (result []string, err error)
|
MGet(v interface{}, keys ...string) (err error)
|
||||||
INCRBY(key string, amount int64) (result int64, err error)
|
INCRBY(key string, amount int64) (result int64, err error)
|
||||||
/*List*/
|
/*List*/
|
||||||
Lindex(key string, value interface{}) (err error)
|
Lindex(key string, value interface{}) (err error)
|
||||||
@ -53,7 +52,7 @@ type (
|
|||||||
LPop(key string, value interface{}) (err error)
|
LPop(key string, value interface{}) (err error)
|
||||||
LPush(key string, values ...interface{}) (err error)
|
LPush(key string, values ...interface{}) (err error)
|
||||||
LPushX(key string, values ...interface{}) (err error)
|
LPushX(key string, values ...interface{}) (err error)
|
||||||
LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error)
|
LRange(key string, start, end int, v interface{}) (err error)
|
||||||
LRem(key string, count int, target interface{}) (err error)
|
LRem(key string, count int, target interface{}) (err error)
|
||||||
LSet(key string, index int, value interface{}) (err error)
|
LSet(key string, index int, value interface{}) (err error)
|
||||||
Ltrim(key string, start, stop int) (err error)
|
Ltrim(key string, start, stop int) (err error)
|
||||||
@ -70,24 +69,24 @@ type (
|
|||||||
HIncrByFloat(key string, field string, value float32) (err error)
|
HIncrByFloat(key string, field string, value float32) (err error)
|
||||||
Hkeys(key string) (result []string, err error)
|
Hkeys(key string) (result []string, err error)
|
||||||
Hlen(key string) (result int, err error)
|
Hlen(key string) (result int, err error)
|
||||||
HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error)
|
HMGet(key string, v interface{}, fields ...string) (err error)
|
||||||
HMSet(key string, v interface{}) (err error)
|
HMSet(key string, v interface{}) (err error)
|
||||||
HSet(key string, field string, value interface{}) (err error)
|
HSet(key string, field string, value interface{}) (err error)
|
||||||
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)
|
SAdd(key string, values ...interface{}) (err error)
|
||||||
SCard(key string) (result int64, err error)
|
SCard(key string) (result int64, err error)
|
||||||
SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error)
|
SDiff(v interface{}, keys ...string) (err error)
|
||||||
SDiffStore(destination string, keys ...string) (result int64, err error)
|
SDiffStore(destination string, keys ...string) (result int64, err error)
|
||||||
SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error)
|
SInter(v interface{}, keys ...string) (err error)
|
||||||
SInterStore(destination string, keys ...string) (result int64, err error)
|
SInterStore(destination string, keys ...string) (result int64, err error)
|
||||||
Sismember(key string, value interface{}) (iskeep bool, err error)
|
Sismember(key string, value interface{}) (iskeep bool, err error)
|
||||||
SMembers(valuetype reflect.Type, key string) (result []interface{}, err error)
|
SMembers(v interface{}, key string) (err error)
|
||||||
SMove(source string, destination string, member interface{}) (result bool, err error)
|
SMove(source string, destination string, member interface{}) (result bool, err error)
|
||||||
Spop(key string) (result string, err error)
|
Spop(key string) (result string, err error)
|
||||||
Srandmember(key string) (result string, err error)
|
Srandmember(key string) (result string, err error)
|
||||||
SRem(key string, members ...interface{}) (result int64, err error)
|
SRem(key string, members ...interface{}) (result int64, err error)
|
||||||
SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error)
|
SUnion(v interface{}, keys ...string) (err error)
|
||||||
Sunionstore(destination string, keys ...string) (result int64, err error)
|
Sunionstore(destination string, keys ...string) (result int64, err error)
|
||||||
Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error)
|
Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error)
|
||||||
/*ZSet*/
|
/*ZSet*/
|
||||||
@ -97,16 +96,16 @@ type (
|
|||||||
ZIncrBy(key string, increment float64, member string) (result float64, err error)
|
ZIncrBy(key string, increment float64, member string) (result float64, err error)
|
||||||
ZInterStore(destination string, store *redis.ZStore) (result int64, err error)
|
ZInterStore(destination string, store *redis.ZStore) (result int64, err error)
|
||||||
ZLexCount(key string, min string, max string) (result int64, err error)
|
ZLexCount(key string, min string, max string) (result int64, err error)
|
||||||
ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error)
|
ZRange(key string, start int64, stop int64, v interface{}) (err error)
|
||||||
ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error)
|
ZRangeByLex(key string, opt *redis.ZRangeBy, v interface{}) (err error)
|
||||||
ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error)
|
ZRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error)
|
||||||
ZRank(key string, member string) (result int64, err error)
|
ZRank(key string, member string) (result int64, err error)
|
||||||
ZRem(key string, members ...interface{}) (result int64, err error)
|
ZRem(key string, members ...interface{}) (result int64, err error)
|
||||||
ZRemRangeByLex(key string, min string, max string) (result int64, err error)
|
ZRemRangeByLex(key string, min string, max string) (result int64, err error)
|
||||||
ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error)
|
ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error)
|
||||||
ZRemRangeByScore(key string, min string, max string) (result int64, err error)
|
ZRemRangeByScore(key string, min string, max string) (result int64, err error)
|
||||||
ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error)
|
ZRevRange(key string, start int64, stop int64, v interface{}) (err error)
|
||||||
ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error)
|
ZRevRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error)
|
||||||
ZRevRank(key string, member string) (result int64, err error)
|
ZRevRank(key string, member string) (result int64, err error)
|
||||||
ZScore(key string, member string) (result float64, err error)
|
ZScore(key string, member string) (result float64, err error)
|
||||||
ZUnionStore(dest string, store *redis.ZStore) (result int64, err error)
|
ZUnionStore(dest string, store *redis.ZStore) (result int64, err error)
|
||||||
@ -232,8 +231,8 @@ func Get(key string, value interface{}) (err error) {
|
|||||||
func GetSet(key string, value interface{}, result interface{}) (err error) {
|
func GetSet(key string, value interface{}, result interface{}) (err error) {
|
||||||
return defsys.GetSet(key, value, result)
|
return defsys.GetSet(key, value, result)
|
||||||
}
|
}
|
||||||
func MGet(keys ...string) (result []string, err error) {
|
func MGet(v interface{}, keys ...string) (err error) {
|
||||||
return defsys.MGet(keys...)
|
return defsys.MGet(v, keys...)
|
||||||
}
|
}
|
||||||
func INCRBY(key string, amount int64) (result int64, err error) {
|
func INCRBY(key string, amount int64) (result int64, err error) {
|
||||||
return defsys.INCRBY(key, amount)
|
return defsys.INCRBY(key, amount)
|
||||||
@ -270,8 +269,8 @@ func LPush(key string, values ...interface{}) (err error) {
|
|||||||
func LPushX(key string, values ...interface{}) (err error) {
|
func LPushX(key string, values ...interface{}) (err 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, end int, v interface{}) (err error) {
|
||||||
return defsys.LRange(key, start, end, valuetype)
|
return defsys.LRange(key, start, end, v)
|
||||||
}
|
}
|
||||||
func LRem(key string, count int, target interface{}) (err error) {
|
func LRem(key string, count int, target interface{}) (err error) {
|
||||||
return defsys.LRem(key, count, target)
|
return defsys.LRem(key, count, target)
|
||||||
@ -320,8 +319,8 @@ func Hkeys(key string) (result []string, err error) {
|
|||||||
func Hlen(key string) (result int, err error) {
|
func Hlen(key string) (result int, 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, v interface{}, fields ...string) (err error) {
|
||||||
return defsys.HMGet(key, valuetype, fields...)
|
return defsys.HMGet(key, v, fields...)
|
||||||
}
|
}
|
||||||
func HMSet(key string, v interface{}) (err error) {
|
func HMSet(key string, v interface{}) (err error) {
|
||||||
return defsys.HMSet(key, v)
|
return defsys.HMSet(key, v)
|
||||||
@ -340,14 +339,14 @@ func SAdd(key string, values ...interface{}) (err error) {
|
|||||||
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(v interface{}, keys ...string) (err error) {
|
||||||
return defsys.SDiff(valuetype, keys...)
|
return defsys.SDiff(v, 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(v interface{}, keys ...string) (err error) {
|
||||||
return defsys.SInter(valuetype, keys...)
|
return defsys.SInter(v, 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...)
|
||||||
@ -355,8 +354,8 @@ func SInterStore(destination string, keys ...string) (result int64, err error) {
|
|||||||
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(v interface{}, key string) (err error) {
|
||||||
return defsys.SMembers(valuetype, key)
|
return defsys.SMembers(v, 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)
|
||||||
@ -370,8 +369,8 @@ func Srandmember(key string) (result string, err error) {
|
|||||||
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(v interface{}, keys ...string) (err error) {
|
||||||
return defsys.SUnion(valuetype, keys...)
|
return defsys.SUnion(v, keys...)
|
||||||
}
|
}
|
||||||
func Sunionstore(destination string, keys ...string) (result int64, err error) {
|
func Sunionstore(destination string, keys ...string) (result int64, err error) {
|
||||||
return defsys.Sunionstore(destination, keys...)
|
return defsys.Sunionstore(destination, keys...)
|
||||||
@ -399,14 +398,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, v interface{}) (err error) {
|
||||||
return defsys.ZRange(valuetype, key, start, stop)
|
return defsys.ZRange(key, start, stop, v)
|
||||||
}
|
}
|
||||||
func ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) {
|
func ZRangeByLex(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
|
||||||
return defsys.ZRangeByLex(valuetype, key, opt)
|
return defsys.ZRangeByLex(key, opt, v)
|
||||||
}
|
}
|
||||||
func ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) {
|
func ZRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
|
||||||
return defsys.ZRangeByScore(valuetype, key, opt)
|
return defsys.ZRangeByScore(key, opt, v)
|
||||||
}
|
}
|
||||||
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)
|
||||||
@ -423,11 +422,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, v interface{}) (err error) {
|
||||||
return defsys.ZRevRange(valuetype, key, start, stop)
|
return defsys.ZRevRange(key, start, stop, v)
|
||||||
}
|
}
|
||||||
func ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) {
|
func ZRevRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
|
||||||
return defsys.ZRevRangeByScore(valuetype, key, opt)
|
return defsys.ZRevRangeByScore(key, opt, v)
|
||||||
}
|
}
|
||||||
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)
|
||||||
|
@ -3,7 +3,6 @@ package redis
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
@ -164,8 +163,8 @@ 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(v interface{}, keys ...string) (err error) {
|
||||||
return this.client.MGet(keys...)
|
return this.client.MGet(v, keys...)
|
||||||
}
|
}
|
||||||
func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) {
|
func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) {
|
||||||
return this.client.INCRBY(key, amount)
|
return this.client.INCRBY(key, amount)
|
||||||
@ -190,8 +189,8 @@ func (this *Redis) LPush(key string, values ...interface{}) (err error) {
|
|||||||
func (this *Redis) LPushX(key string, values ...interface{}) (err error) {
|
func (this *Redis) LPushX(key string, values ...interface{}) (err 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, end int, v interface{}) (err error) {
|
||||||
return this.client.LRange(key, start, end, valuetype)
|
return this.client.LRange(key, start, end, v)
|
||||||
}
|
}
|
||||||
func (this *Redis) LRem(key string, count int, target interface{}) (err error) {
|
func (this *Redis) LRem(key string, count int, target interface{}) (err error) {
|
||||||
return this.client.LRem(key, count, target)
|
return this.client.LRem(key, count, target)
|
||||||
@ -240,8 +239,8 @@ func (this *Redis) Hkeys(key string) (result []string, err error) {
|
|||||||
func (this *Redis) Hlen(key string) (result int, err error) {
|
func (this *Redis) Hlen(key string) (result int, 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, v interface{}, fields ...string) (err error) {
|
||||||
return this.client.HMGet(key, valuetype, fields...)
|
return this.client.HMGet(key, v, fields...)
|
||||||
}
|
}
|
||||||
func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
||||||
return this.client.HMSet(key, v)
|
return this.client.HMSet(key, v)
|
||||||
@ -260,14 +259,14 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
|
|||||||
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(v interface{}, keys ...string) (err error) {
|
||||||
return this.client.SDiff(valuetype, keys...)
|
return this.client.SDiff(v, 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(v interface{}, keys ...string) (err error) {
|
||||||
return this.client.SInter(valuetype, keys...)
|
return this.client.SInter(v, 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...)
|
||||||
@ -275,8 +274,8 @@ func (this *Redis) SInterStore(destination string, keys ...string) (result int64
|
|||||||
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(v interface{}, key string) (err error) {
|
||||||
return this.client.SMembers(valuetype, key)
|
return this.client.SMembers(v, 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)
|
||||||
@ -290,8 +289,8 @@ func (this *Redis) Srandmember(key string) (result string, err error) {
|
|||||||
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(v interface{}, keys ...string) (err error) {
|
||||||
return this.client.SUnion(valuetype, keys...)
|
return this.client.SUnion(v, 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...)
|
||||||
@ -319,14 +318,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, v interface{}) (err error) {
|
||||||
return this.client.ZRange(valuetype, key, start, stop)
|
return this.client.ZRange(key, start, stop, v)
|
||||||
}
|
}
|
||||||
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, v interface{}) (err error) {
|
||||||
return this.client.ZRangeByLex(valuetype, key, opt)
|
return this.client.ZRangeByLex(key, opt, v)
|
||||||
}
|
}
|
||||||
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, v interface{}) (err error) {
|
||||||
return this.client.ZRangeByScore(valuetype, key, opt)
|
return this.client.ZRangeByScore(key, opt, v)
|
||||||
}
|
}
|
||||||
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)
|
||||||
@ -343,11 +342,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, v interface{}) (err error) {
|
||||||
return this.client.ZRevRange(valuetype, key, start, stop)
|
return this.client.ZRevRange(key, start, stop, v)
|
||||||
}
|
}
|
||||||
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, v interface{}) (err error) {
|
||||||
return this.client.ZRevRangeByScore(valuetype, key, opt)
|
return this.client.ZRevRangeByScore(key, opt, v)
|
||||||
}
|
}
|
||||||
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)
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package single
|
package single
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,18 +35,30 @@ func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "HMSET")
|
agrs = append(agrs, "HMSET")
|
||||||
agrs = append(agrs, key)
|
agrs = append(agrs, key)
|
||||||
var data map[string][]byte
|
var data map[string]string
|
||||||
if data, err = this.encode.EncoderToMap(v); err != nil {
|
if data, err = this.encode.EncoderToMapString(v); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for k, v := range data {
|
for k, v := range data {
|
||||||
result, _ := this.encode.Encoder(v)
|
agrs = append(agrs, k, v)
|
||||||
agrs = append(agrs, k, result)
|
|
||||||
}
|
}
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Redis Hget 命令用于返回哈希表中指定字段的值
|
||||||
|
*/
|
||||||
|
func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
|
||||||
|
cmd := redis.NewStringCmd(this.getContext(), "HGET", key, field)
|
||||||
|
this.client.Process(this.getContext(), cmd)
|
||||||
|
var _result string
|
||||||
|
if _result, err = cmd.Result(); err == nil {
|
||||||
|
err = this.decode.DecoderString(_result, v)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
|
Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
|
||||||
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍
|
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍
|
||||||
@ -58,31 +68,7 @@ func (this *Redis) HGetAll(key string, v interface{}) (err error) {
|
|||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
var _result map[string]string
|
var _result map[string]string
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = this.decode.DecoderMap(_result, v)
|
err = this.decode.DecoderMapString(_result, v)
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Redis Hset 命令用于为哈希表中的字段赋值
|
|
||||||
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
|
||||||
如果字段已经存在于哈希表中,旧值将被覆盖
|
|
||||||
*/
|
|
||||||
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
|
||||||
var resultvalue []byte
|
|
||||||
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
|
||||||
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Redis Hget 命令用于返回哈希表中指定字段的值
|
|
||||||
*/
|
|
||||||
func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
|
|
||||||
var resultvalue string
|
|
||||||
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
|
|
||||||
err = this.decode.Decoder([]byte(resultvalue), value)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -131,24 +117,31 @@ 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, v interface{}, fields ...string) (err error) {
|
||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "HMGET")
|
agrs = append(agrs, "HMGET")
|
||||||
agrs = append(agrs, key)
|
agrs = append(agrs, key)
|
||||||
for _, v := range fields {
|
for _, v := range fields {
|
||||||
agrs = append(agrs, v)
|
agrs = append(agrs, v)
|
||||||
}
|
}
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
|
cmd := redis.NewStringStringMapCmd(this.getContext(), agrs...)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
var _result []string
|
var _result map[string]string
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderMapString(_result, v)
|
||||||
for i, v := range _result {
|
}
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
return
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
}
|
||||||
result[i] = temp
|
|
||||||
}
|
/*
|
||||||
}
|
Redis Hset 命令用于为哈希表中的字段赋值
|
||||||
|
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
||||||
|
如果字段已经存在于哈希表中,旧值将被覆盖
|
||||||
|
*/
|
||||||
|
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
||||||
|
var resultvalue []byte
|
||||||
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
|
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,18 @@
|
|||||||
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, v interface{}) (err error) {
|
||||||
var data string
|
cmd := redis.NewStringCmd(this.getContext(), "LINDEX", key)
|
||||||
if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -27,17 +24,19 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定
|
|||||||
*/
|
*/
|
||||||
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) {
|
func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) {
|
||||||
var (
|
var (
|
||||||
tagervalue []byte
|
tagervalue string
|
||||||
resultvalue []byte
|
resultvalue string
|
||||||
)
|
)
|
||||||
if tagervalue, err = this.encode.Encoder(tager); err == nil {
|
if tagervalue, err = this.encode.EncoderString(tager); err != nil {
|
||||||
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
return
|
||||||
if isbefore {
|
}
|
||||||
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
|
if resultvalue, err = this.encode.EncoderString(value); err != nil {
|
||||||
} else {
|
return
|
||||||
err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err()
|
}
|
||||||
}
|
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
|
return
|
||||||
}
|
}
|
||||||
@ -53,12 +52,12 @@ func (this *Redis) Llen(key string) (result int, err error) {
|
|||||||
/*
|
/*
|
||||||
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
|
cmd := redis.NewStringCmd(this.getContext(), "LPOP", key)
|
||||||
if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -70,7 +69,7 @@ func (this *Redis) LPush(key string, values ...interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "LPUSH")
|
agrs = append(agrs, "LPUSH")
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -84,7 +83,7 @@ func (this *Redis) LPushX(key string, values ...interface{}) (err 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.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -95,18 +94,12 @@ func (this *Redis) LPushX(key string, values ...interface{}) (err error) {
|
|||||||
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 int, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end)
|
cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -119,10 +112,11 @@ count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,
|
|||||||
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 int, target interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue string
|
||||||
if resultvalue, err = this.encode.Encoder(target); err == nil {
|
if resultvalue, err = this.encode.EncoderString(target); err != nil {
|
||||||
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,10 +125,11 @@ Redis Lset 通过索引来设置元素的值。
|
|||||||
当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误
|
当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误
|
||||||
*/
|
*/
|
||||||
func (this *Redis) LSet(key string, index int, value interface{}) (err error) {
|
func (this *Redis) LSet(key string, index int, value interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue string
|
||||||
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
if resultvalue, err = this.encode.EncoderString(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,12 +146,12 @@ func (this *Redis) Ltrim(key string, start, stop int) (err error) {
|
|||||||
/*
|
/*
|
||||||
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
|
cmd := redis.NewStringCmd(this.getContext(), "RPOP", key)
|
||||||
if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -164,12 +159,12 @@ func (this *Redis) Rpop(key string, value interface{}) (err error) {
|
|||||||
/*
|
/*
|
||||||
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
|
cmd := redis.NewStringCmd(this.getContext(), "RPOPLPUSH", oldkey, newkey)
|
||||||
if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) {
|
this.client.Process(this.getContext(), cmd)
|
||||||
err = this.decode.Decoder([]byte(data), value)
|
var _result string
|
||||||
} else {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = this.decode.DecoderString(_result, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -183,7 +178,7 @@ func (this *Redis) RPush(key string, values ...interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "RPUSH")
|
agrs = append(agrs, "RPUSH")
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -197,7 +192,7 @@ func (this *Redis) RPushX(key string, values ...interface{}) (err error) {
|
|||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "RPUSHX")
|
agrs = append(agrs, "RPUSHX")
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package single
|
package single
|
||||||
|
|
||||||
import "reflect"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。
|
Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。
|
||||||
假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。
|
假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。
|
||||||
@ -12,7 +10,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
|
|||||||
agrs = append(agrs, "SADD")
|
agrs = append(agrs, "SADD")
|
||||||
agrs = append(agrs, key)
|
agrs = append(agrs, key)
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(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()
|
||||||
@ -32,17 +30,11 @@ 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(v interface{}, keys ...string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SDiff(this.getContext(), keys...)
|
cmd := this.client.SDiff(this.getContext(), keys...)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -58,17 +50,11 @@ func (this *Redis) SDiffStore(destination string, keys ...string) (result int64,
|
|||||||
/*
|
/*
|
||||||
Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
|
Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) {
|
func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SInter(this.getContext(), keys...)
|
cmd := this.client.SInter(this.getContext(), keys...)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -92,17 +78,11 @@ func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err er
|
|||||||
/*
|
/*
|
||||||
Redis Smembers 号召返回集合中的所有成员。
|
Redis Smembers 号召返回集合中的所有成员。
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) {
|
func (this *Redis) SMembers(v interface{}, key string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SMembers(this.getContext(), key)
|
cmd := this.client.SMembers(this.getContext(), key)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -153,17 +133,11 @@ func (this *Redis) SRem(key string, members ...interface{}) (result int64, err e
|
|||||||
/*
|
/*
|
||||||
Redis Sunion 命令返回给定集合的并集。
|
Redis Sunion 命令返回给定集合的并集。
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) {
|
func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.SUnion(this.getContext(), keys...)
|
cmd := this.client.SUnion(this.getContext(), keys...)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
@ -12,10 +11,11 @@ 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, value interface{}, expiration time.Duration) (err error) {
|
||||||
var result []byte
|
var result string
|
||||||
if result, err = this.encode.Encoder(value); err == nil {
|
if result, err = this.encode.EncoderString(value); err != nil {
|
||||||
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Set(this.getContext(), key, result, expiration).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,9 +23,6 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
|
|||||||
指定的 key 不存在时,为 key 设置指定的值
|
指定的 key 不存在时,为 key 设置指定的值
|
||||||
*/
|
*/
|
||||||
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
|
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
|
||||||
// var _value []byte
|
|
||||||
// if result, err = this.Encode(value); err == nil {
|
|
||||||
// err = this.client.Do(this.getContext(), "SETNX", key, result).Err()
|
|
||||||
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value)
|
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
result, err = cmd.Result()
|
result, err = cmd.Result()
|
||||||
@ -36,11 +33,11 @@ func (this *Redis) SetNX(key string, value interface{}) (result int64, err error
|
|||||||
/*
|
/*
|
||||||
同时设置一个或多个 key-value 对
|
同时设置一个或多个 key-value 对
|
||||||
*/
|
*/
|
||||||
func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) {
|
func (this *Redis) MSet(v map[string]interface{}) (err 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 v {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(v)
|
||||||
agrs = append(agrs, k, result)
|
agrs = append(agrs, k, result)
|
||||||
}
|
}
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
@ -50,11 +47,11 @@ func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) {
|
|||||||
/*
|
/*
|
||||||
命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对
|
命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对
|
||||||
*/
|
*/
|
||||||
func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) {
|
func (this *Redis) MSetNX(v map[string]interface{}) (err 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 v {
|
||||||
result, _ := this.encode.Encoder(v)
|
result, _ := this.encode.EncoderString(v)
|
||||||
agrs = append(agrs, k, result)
|
agrs = append(agrs, k, result)
|
||||||
}
|
}
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
@ -120,10 +117,11 @@ Redis Append 命令用于为指定的 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 interface{}) (err error) {
|
||||||
var result []byte
|
var result string
|
||||||
if result, err = this.encode.Encoder(value); err == nil {
|
if result, err = this.encode.EncoderString(value); err != nil {
|
||||||
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
return
|
||||||
}
|
}
|
||||||
|
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,9 +129,9 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
|
|||||||
命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型
|
命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型
|
||||||
*/
|
*/
|
||||||
func (this *Redis) Get(key string, value interface{}) (err error) {
|
func (this *Redis) Get(key string, value interface{}) (err error) {
|
||||||
var result []byte
|
var result string
|
||||||
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
|
if result, err = this.client.Get(this.getContext(), key).Result(); err == nil {
|
||||||
err = this.decode.Decoder(result, value)
|
err = this.decode.DecoderString(result, value)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -143,14 +141,14 @@ 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) {
|
||||||
var (
|
var (
|
||||||
data string
|
_value string
|
||||||
_value []byte
|
|
||||||
)
|
)
|
||||||
if _value, err = this.encode.Encoder(value); err == nil {
|
if _value, err = this.encode.EncoderString(value); err == nil {
|
||||||
if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) {
|
cmd := redis.NewStringCmd(this.getContext(), "GETSET", key, _value)
|
||||||
err = this.decode.Decoder([]byte(data), result)
|
this.client.Process(this.getContext(), cmd)
|
||||||
} else {
|
var _result string
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
if _result, err = cmd.Result(); err == nil {
|
||||||
|
err = this.decode.DecoderString(_result, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -159,7 +157,7 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
|
|||||||
/*
|
/*
|
||||||
返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil
|
返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil
|
||||||
*/
|
*/
|
||||||
func (this *Redis) MGet(keys ...string) (result []string, err error) {
|
func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
|
||||||
agrs := make([]interface{}, 0)
|
agrs := make([]interface{}, 0)
|
||||||
agrs = append(agrs, "MGET")
|
agrs = append(agrs, "MGET")
|
||||||
for _, v := range keys {
|
for _, v := range keys {
|
||||||
@ -167,7 +165,11 @@ func (this *Redis) MGet(keys ...string) (result []string, err error) {
|
|||||||
}
|
}
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
|
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
|
||||||
this.client.Process(this.getContext(), cmd)
|
this.client.Process(this.getContext(), cmd)
|
||||||
result, err = cmd.Result()
|
var result []string
|
||||||
|
if result, err = cmd.Result(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = this.decode.DecoderSliceString(result, v)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package single
|
package single
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,17 +55,11 @@ func (this *Redis) ZLexCount(key string, min string, max string) (result int64,
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRange(this.getContext(), key, start, stop)
|
cmd := this.client.ZRange(this.getContext(), key, start, stop)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -75,17 +67,11 @@ func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRangeByLex(this.getContext(), key, opt)
|
cmd := this.client.ZRangeByLex(this.getContext(), key, opt)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -93,17 +79,11 @@ func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZR
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRangeByScore(this.getContext(), key, opt)
|
cmd := this.client.ZRangeByScore(this.getContext(), key, opt)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -151,17 +131,11 @@ func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRevRange(this.getContext(), key, start, stop)
|
cmd := this.client.ZRevRange(this.getContext(), key, start, stop)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -169,17 +143,11 @@ func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, st
|
|||||||
/*
|
/*
|
||||||
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, v interface{}) (err error) {
|
||||||
var _result []string
|
var _result []string
|
||||||
cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt)
|
cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt)
|
||||||
if _result, err = cmd.Result(); err == nil {
|
if _result, err = cmd.Result(); err == nil {
|
||||||
result = make([]interface{}, len(_result))
|
err = this.decode.DecoderSliceString(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -177,11 +177,11 @@ func Test_Redis_Encoder_int(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Redis_Encoder_Hash(t *testing.T) {
|
func Test_Redis_Encoder_Hash(t *testing.T) {
|
||||||
err := redis.HMSet("test:1003", &TestData{Name: "liwei1dao", Agr: 12})
|
// err := redis.HMSet("test:1003", &TestData{Name: "liwei1dao", Agr: 12})
|
||||||
fmt.Printf("err:%v\n", err)
|
// fmt.Printf("err:%v\n", err)
|
||||||
// data := &TestData{}
|
data := &TestData{}
|
||||||
// err = redis.HGetAll("test:1003", data)
|
err := redis.HGetAll("test:1003", data)
|
||||||
// fmt.Printf("data:%v err:%v\n", data, err)
|
fmt.Printf("data:%v err:%v\n", data, err)
|
||||||
|
|
||||||
// name := ""
|
// name := ""
|
||||||
// err := redis.HGet("test:1003", "Name", &name)
|
// err := redis.HGet("test:1003", "Name", &name)
|
||||||
|
@ -24,6 +24,6 @@ func Test_Encoder(t *testing.T) {
|
|||||||
func Test_Decoder(t *testing.T) {
|
func Test_Decoder(t *testing.T) {
|
||||||
decoder := &Decoder{}
|
decoder := &Decoder{}
|
||||||
data := &TestData{}
|
data := &TestData{}
|
||||||
err := decoder.DecoderMap(map[string]string{"Fild_1": "liwei1dao"}, data)
|
err := decoder.DecoderMapString(map[string]string{"Fild_1": "liwei1dao"}, data)
|
||||||
fmt.Printf("DecoderMap data1:%v err:%v", data, err)
|
fmt.Printf("DecoderMap data1:%v err:%v", data, err)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package codec
|
|||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math"
|
"math"
|
||||||
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,11 +12,17 @@ const host32bit = ^uint(0)>>32 == 0
|
|||||||
type (
|
type (
|
||||||
IDecoder interface {
|
IDecoder interface {
|
||||||
Decoder(buf []byte, v interface{}) error
|
Decoder(buf []byte, v interface{}) error
|
||||||
DecoderMap(data map[string]string, v interface{}) error
|
DecoderMap(data map[string][]byte, v interface{}) error
|
||||||
|
DecoderString(buf string, v interface{}) error
|
||||||
|
DecoderMapString(data map[string]string, v interface{}) error
|
||||||
|
DecoderSliceString(data []string, v interface{}) error
|
||||||
}
|
}
|
||||||
IEncoder interface {
|
IEncoder interface {
|
||||||
Encoder(v interface{}) (data []byte, err error)
|
Encoder(v interface{}) (data []byte, err error)
|
||||||
EncoderToMap(v interface{}) (data map[string][]byte, err error)
|
EncoderToMap(v interface{}) (data map[string][]byte, err error)
|
||||||
|
EncoderString(v interface{}) (data string, err error)
|
||||||
|
EncoderToMapString(v interface{}) (data map[string]string, err error)
|
||||||
|
EncoderToSliceString(v interface{}) (data []string, err error)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -179,3 +186,31 @@ func BytesToBool(buf []byte) bool {
|
|||||||
var data bool = buf[0] != 0
|
var data bool = buf[0] != 0
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Atoi(b []byte) (int, error) {
|
||||||
|
return strconv.Atoi(BytesToString(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseInt(b []byte, base int, bitSize int) (int64, error) {
|
||||||
|
return strconv.ParseInt(BytesToString(b), base, bitSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseUint(b []byte, base int, bitSize int) (uint64, error) {
|
||||||
|
return strconv.ParseUint(BytesToString(b), base, bitSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseFloat(b []byte, bitSize int) (float64, error) {
|
||||||
|
return strconv.ParseFloat(BytesToString(b), bitSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UintToString(n uint64) string {
|
||||||
|
return BytesToString(strconv.AppendUint([]byte{}, n, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
func IntToString(n int64) string {
|
||||||
|
return BytesToString(strconv.AppendInt([]byte{}, n, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
func FloatToString(f float64) string {
|
||||||
|
return BytesToString(strconv.AppendFloat([]byte{}, f, 'f', -1, 64))
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package codec
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ func (this *Decoder) Decoder(buf []byte, v interface{}) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Decoder) DecoderMap(data map[string]string, v interface{}) error {
|
func (this *Decoder) DecoderMap(data map[string][]byte, v interface{}) error {
|
||||||
vof := reflect.ValueOf(v)
|
vof := reflect.ValueOf(v)
|
||||||
if !vof.IsValid() {
|
if !vof.IsValid() {
|
||||||
return fmt.Errorf("Decoder: DecoderMap(nil)")
|
return fmt.Errorf("Decoder: DecoderMap(nil)")
|
||||||
@ -100,12 +101,12 @@ func (this *Decoder) DecoderMap(data map[string]string, v interface{}) error {
|
|||||||
}
|
}
|
||||||
value := reflect.New(vt).Elem()
|
value := reflect.New(vt).Elem()
|
||||||
if value.Kind() != reflect.Ptr {
|
if value.Kind() != reflect.Ptr {
|
||||||
if err := this.Decoder(StringToBytes(v), value.Addr().Interface()); err != nil {
|
if err := this.Decoder(v, value.Addr().Interface()); err != nil {
|
||||||
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
value.Interface()
|
value.Interface()
|
||||||
if err := this.Decoder(StringToBytes(v), value.Addr().Interface()); err != nil {
|
if err := this.Decoder(v, value.Addr().Interface()); err != nil {
|
||||||
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,12 +125,12 @@ func (this *Decoder) DecoderMap(data map[string]string, v interface{}) error {
|
|||||||
if value, ok := data[name]; ok {
|
if value, ok := data[name]; ok {
|
||||||
v := reflect.New(fieldInfo.Type).Elem()
|
v := reflect.New(fieldInfo.Type).Elem()
|
||||||
if fieldInfo.Type.Kind() != reflect.Ptr {
|
if fieldInfo.Type.Kind() != reflect.Ptr {
|
||||||
if err := this.Decoder(StringToBytes(value), v.Addr().Interface()); err != nil {
|
if err := this.Decoder(value, v.Addr().Interface()); err != nil {
|
||||||
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
}
|
}
|
||||||
elem.FieldByName(fieldInfo.Name).Set(v)
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
||||||
} else {
|
} else {
|
||||||
if err := this.Decoder(StringToBytes(value), v); err != nil {
|
if err := this.Decoder(value, v); err != nil {
|
||||||
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
}
|
}
|
||||||
elem.FieldByName(fieldInfo.Name).Set(v)
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
||||||
@ -139,3 +140,217 @@ func (this *Decoder) DecoderMap(data map[string]string, v interface{}) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//redis 存储解析 针对 string 转换
|
||||||
|
func (this *Decoder) DecoderString(buf string, v interface{}) (err error) {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return fmt.Errorf("decoder: Decoder(nil)")
|
||||||
|
case *string:
|
||||||
|
*v = buf
|
||||||
|
return nil
|
||||||
|
case *[]byte:
|
||||||
|
*v = StringToBytes(buf)
|
||||||
|
return nil
|
||||||
|
case *int:
|
||||||
|
*v, err = Atoi(StringToBytes(buf))
|
||||||
|
return
|
||||||
|
case *int8:
|
||||||
|
var n int64
|
||||||
|
n, err = ParseInt(StringToBytes(buf), 10, 8)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = int8(n)
|
||||||
|
return
|
||||||
|
case *int16:
|
||||||
|
var n int64
|
||||||
|
n, err = ParseInt(StringToBytes(buf), 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = int16(n)
|
||||||
|
return
|
||||||
|
case *int32:
|
||||||
|
var n int64
|
||||||
|
n, err = ParseInt(StringToBytes(buf), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = int32(n)
|
||||||
|
return
|
||||||
|
case *int64:
|
||||||
|
var n int64
|
||||||
|
n, err = ParseInt(StringToBytes(buf), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = n
|
||||||
|
return
|
||||||
|
case *uint:
|
||||||
|
var n uint64
|
||||||
|
n, err = ParseUint(StringToBytes(buf), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = uint(n)
|
||||||
|
return
|
||||||
|
case *uint8:
|
||||||
|
var n uint64
|
||||||
|
n, err = ParseUint(StringToBytes(buf), 10, 8)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = uint8(n)
|
||||||
|
return
|
||||||
|
case *uint16:
|
||||||
|
var n uint64
|
||||||
|
n, err = ParseUint(StringToBytes(buf), 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = uint16(n)
|
||||||
|
return
|
||||||
|
case *uint32:
|
||||||
|
var n uint64
|
||||||
|
n, err = ParseUint(StringToBytes(buf), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = uint32(n)
|
||||||
|
return
|
||||||
|
case *uint64:
|
||||||
|
var n uint64
|
||||||
|
n, err = ParseUint(StringToBytes(buf), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = n
|
||||||
|
return
|
||||||
|
case *float32:
|
||||||
|
var f float64
|
||||||
|
f, err = strconv.ParseFloat(buf, 32)
|
||||||
|
*v = float32(f)
|
||||||
|
return
|
||||||
|
case *float64:
|
||||||
|
*v, err = strconv.ParseFloat(buf, 64)
|
||||||
|
return
|
||||||
|
case *bool:
|
||||||
|
*v, err = strconv.ParseBool(buf)
|
||||||
|
return
|
||||||
|
case *time.Time:
|
||||||
|
var err error
|
||||||
|
*v, err = time.Parse(time.RFC3339Nano, buf)
|
||||||
|
return err
|
||||||
|
case *time.Duration:
|
||||||
|
var n int64
|
||||||
|
n, err = ParseInt(StringToBytes(buf), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = time.Duration(n)
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
if this.DefDecoder != nil {
|
||||||
|
return this.DefDecoder(StringToBytes(buf), v)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"decoder: can't marshal %T (implement decoder.DefDecoder)", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//redis 存储解析 针对 string 转换
|
||||||
|
func (this *Decoder) DecoderMapString(data map[string]string, v interface{}) error {
|
||||||
|
vof := reflect.ValueOf(v)
|
||||||
|
if !vof.IsValid() {
|
||||||
|
return fmt.Errorf("Decoder: DecoderMap(nil)")
|
||||||
|
}
|
||||||
|
if vof.Kind() != reflect.Ptr && vof.Kind() != reflect.Map && vof.Kind() != reflect.Slice {
|
||||||
|
return fmt.Errorf("Decoder: DecoderMap(non-pointer %T)", v)
|
||||||
|
}
|
||||||
|
if vof.Kind() == reflect.Map {
|
||||||
|
kt, vt := vof.Type().Key(), vof.Type().Elem()
|
||||||
|
for k, v := range data {
|
||||||
|
key := reflect.New(kt).Elem()
|
||||||
|
if key.Kind() != reflect.Ptr {
|
||||||
|
if err := this.DecoderString(k, key.Addr().Interface()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", key, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := this.DecoderString(k, key.Addr()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", key, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
value := reflect.New(vt).Elem()
|
||||||
|
if value.Kind() != reflect.Ptr {
|
||||||
|
if err := this.DecoderString(v, value.Addr().Interface()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
value.Interface()
|
||||||
|
if err := this.DecoderString(v, value.Addr().Interface()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vof.SetMapIndex(key, value)
|
||||||
|
}
|
||||||
|
} else if vof.Kind() == reflect.Ptr {
|
||||||
|
elem := vof.Elem()
|
||||||
|
relType := elem.Type()
|
||||||
|
for i := 0; i < relType.NumField(); i++ {
|
||||||
|
fieldInfo := relType.Field(i)
|
||||||
|
tag := fieldInfo.Tag
|
||||||
|
name := tag.Get("json")
|
||||||
|
if len(name) == 0 {
|
||||||
|
name = fieldInfo.Name
|
||||||
|
}
|
||||||
|
if value, ok := data[name]; ok {
|
||||||
|
v := reflect.New(fieldInfo.Type).Elem()
|
||||||
|
if fieldInfo.Type.Kind() != reflect.Ptr {
|
||||||
|
if err := this.DecoderString(value, v.Addr().Interface()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
||||||
|
} else {
|
||||||
|
if err := this.DecoderString(value, v); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//redis 存储解析 针对 string 转换
|
||||||
|
func (this *Decoder) DecoderSliceString(data []string, v interface{}) error {
|
||||||
|
vof := reflect.ValueOf(v)
|
||||||
|
if !vof.IsValid() {
|
||||||
|
return fmt.Errorf("Decoder: DecoderMap(nil)")
|
||||||
|
}
|
||||||
|
if vof.Kind() != reflect.Ptr && vof.Kind() != reflect.Map && vof.Kind() != reflect.Slice {
|
||||||
|
return fmt.Errorf("Decoder: DecoderMap(non-pointer %T)", v)
|
||||||
|
}
|
||||||
|
if vof.Kind() == reflect.Slice {
|
||||||
|
elemType := vof.Type().Elem()
|
||||||
|
for _, buf := range data {
|
||||||
|
if vof.Len() < vof.Cap() {
|
||||||
|
vof.Set(vof.Slice(0, vof.Len()+1))
|
||||||
|
elem := vof.Index(vof.Len() - 1)
|
||||||
|
if elem.IsNil() {
|
||||||
|
elem.Set(reflect.New(elemType))
|
||||||
|
}
|
||||||
|
this.DecoderString(buf, elem.Elem().Addr().Interface())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
elem := reflect.New(elemType)
|
||||||
|
vof.Set(reflect.Append(vof, elem))
|
||||||
|
this.DecoderString(buf, elem.Elem().Addr().Interface())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("Decoder: DecoderSliceString(invalid type %T)", v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -116,3 +116,144 @@ func (this *Encoder) EncoderToMap(v interface{}) (data map[string][]byte, err er
|
|||||||
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T)", v)
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T)", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//redis 存储编码 针对 string 转换
|
||||||
|
func (this *Encoder) EncoderString(v interface{}) (data string, err error) {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return "", nil
|
||||||
|
case string:
|
||||||
|
return v, nil
|
||||||
|
case []byte:
|
||||||
|
return BytesToString(v), nil
|
||||||
|
case int:
|
||||||
|
return IntToString(int64(v)), nil
|
||||||
|
case int8:
|
||||||
|
return IntToString(int64(v)), nil
|
||||||
|
case int16:
|
||||||
|
return IntToString(int64(v)), nil
|
||||||
|
case int32:
|
||||||
|
return IntToString(int64(v)), nil
|
||||||
|
case int64:
|
||||||
|
return IntToString(int64(v)), nil
|
||||||
|
case uint:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case uint8:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case uint16:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case uint32:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case uint64:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case float32:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case float64:
|
||||||
|
return UintToString(uint64(v)), nil
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
return IntToString(1), nil
|
||||||
|
}
|
||||||
|
return IntToString(0), nil
|
||||||
|
case time.Time:
|
||||||
|
return BytesToString(v.AppendFormat([]byte{}, time.RFC3339Nano)), nil
|
||||||
|
case time.Duration:
|
||||||
|
return IntToString(v.Nanoseconds()), nil
|
||||||
|
default:
|
||||||
|
if this.DefEncoder != nil {
|
||||||
|
var b []byte
|
||||||
|
b, err = this.DefEncoder(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return BytesToString(b), nil
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"encoder: can't marshal %T (implement encoder.DefEncoder)", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//redis 存储编码 针对 string 转换
|
||||||
|
func (this *Encoder) EncoderToMapString(v interface{}) (data map[string]string, err error) {
|
||||||
|
vof := reflect.ValueOf(v)
|
||||||
|
if !vof.IsValid() {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(nil)")
|
||||||
|
}
|
||||||
|
if vof.Kind() != reflect.Ptr && vof.Kind() != reflect.Map && vof.Kind() != reflect.Slice {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(non-pointer %T)", v)
|
||||||
|
}
|
||||||
|
// vof = vof.Elem()
|
||||||
|
data = make(map[string]string)
|
||||||
|
if vof.Kind() == reflect.Map {
|
||||||
|
keys := vof.MapKeys()
|
||||||
|
for _, k := range keys {
|
||||||
|
value := vof.MapIndex(k)
|
||||||
|
var keydata string
|
||||||
|
var valuedata string
|
||||||
|
if keydata, err = this.EncoderString(k.Interface()); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", value.Interface(), err)
|
||||||
|
}
|
||||||
|
if valuedata, err = this.EncoderString(value.Interface()); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", value.Interface(), err)
|
||||||
|
}
|
||||||
|
data[keydata] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else if vof.Kind() == reflect.Slice {
|
||||||
|
for i := 0; i < vof.Len(); i++ {
|
||||||
|
value := vof.Index(i).Interface()
|
||||||
|
var valuedata string
|
||||||
|
if valuedata, err = this.EncoderString(value); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
data[IntToString(int64(i))] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else if vof.Kind() == reflect.Ptr {
|
||||||
|
elem := vof.Elem()
|
||||||
|
relType := elem.Type()
|
||||||
|
for i := 0; i < relType.NumField(); i++ {
|
||||||
|
fieldInfo := relType.Field(i)
|
||||||
|
tag := fieldInfo.Tag
|
||||||
|
name := tag.Get("json")
|
||||||
|
if len(name) == 0 {
|
||||||
|
name = fieldInfo.Name
|
||||||
|
}
|
||||||
|
field := elem.Field(i).Interface()
|
||||||
|
var valuedata string
|
||||||
|
if valuedata, err = this.EncoderString(field); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", field, err)
|
||||||
|
}
|
||||||
|
data[name] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T)", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//redis 存储编码 针对 string 转换
|
||||||
|
func (this *Encoder) EncoderToSliceString(v interface{}) (data []string, err error) {
|
||||||
|
vof := reflect.ValueOf(v)
|
||||||
|
if !vof.IsValid() {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToSliceString(nil)")
|
||||||
|
}
|
||||||
|
if vof.Kind() != reflect.Ptr && vof.Kind() != reflect.Map && vof.Kind() != reflect.Slice {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToSliceString(non-pointer %T)", v)
|
||||||
|
}
|
||||||
|
// vof = vof.Elem()
|
||||||
|
if vof.Kind() == reflect.Slice {
|
||||||
|
for i := 0; i < vof.Len(); i++ {
|
||||||
|
value := vof.Index(i).Interface()
|
||||||
|
var valuedata string
|
||||||
|
if valuedata, err = this.EncoderString(value); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToSliceString(invalid type %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
data[i] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToSliceString(invalid type %T)", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user