上传redis 编解码库代码
This commit is contained in:
parent
1ea06ac54d
commit
a646325524
@ -13,7 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go_dreamfactory/lego/utils/convert"
|
"go_dreamfactory/lego/utils/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -236,9 +236,9 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
|
|||||||
case time.Time:
|
case time.Time:
|
||||||
return setTimeField(val, field, value)
|
return setTimeField(val, field, value)
|
||||||
}
|
}
|
||||||
return json.Unmarshal(convert.StringToBytes(val), value.Addr().Interface())
|
return json.Unmarshal(codec.StringToBytes(val), value.Addr().Interface())
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return json.Unmarshal(convert.StringToBytes(val), value.Addr().Interface())
|
return json.Unmarshal(codec.StringToBytes(val), value.Addr().Interface())
|
||||||
default:
|
default:
|
||||||
return errUnknownType
|
return errUnknownType
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"go_dreamfactory/lego/sys/gin/render"
|
"go_dreamfactory/lego/sys/gin/render"
|
||||||
"go_dreamfactory/lego/utils/convert"
|
"go_dreamfactory/lego/utils/codec"
|
||||||
|
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
"golang.org/x/net/http2/h2c"
|
"golang.org/x/net/http2/h2c"
|
||||||
@ -418,7 +418,7 @@ func (this *Engine) redirectFixedPath(c *Context, root *node, trailingSlash bool
|
|||||||
rPath := req.URL.Path
|
rPath := req.URL.Path
|
||||||
|
|
||||||
if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok {
|
if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok {
|
||||||
req.URL.Path = convert.BytesToString(fixedPath)
|
req.URL.Path = codec.BytesToString(fixedPath)
|
||||||
this.redirectRequest(c)
|
this.redirectRequest(c)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"go_dreamfactory/lego/utils/convert"
|
"go_dreamfactory/lego/utils/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -107,7 +107,7 @@ walk:
|
|||||||
|
|
||||||
n.children = []*node{&child}
|
n.children = []*node{&child}
|
||||||
// []byte for proper unicode char conversion, see #65
|
// []byte for proper unicode char conversion, see #65
|
||||||
n.indices = convert.BytesToString([]byte{n.path[i]})
|
n.indices = codec.BytesToString([]byte{n.path[i]})
|
||||||
n.path = path[:i]
|
n.path = path[:i]
|
||||||
n.handlers = nil
|
n.handlers = nil
|
||||||
n.wildChild = false
|
n.wildChild = false
|
||||||
@ -140,7 +140,7 @@ walk:
|
|||||||
// Otherwise insert it
|
// Otherwise insert it
|
||||||
if c != ':' && c != '*' && n.nType != catchAll {
|
if c != ':' && c != '*' && n.nType != catchAll {
|
||||||
// []byte for proper unicode char conversion, see #65
|
// []byte for proper unicode char conversion, see #65
|
||||||
n.indices += convert.BytesToString([]byte{c})
|
n.indices += codec.BytesToString([]byte{c})
|
||||||
child := &node{
|
child := &node{
|
||||||
fullPath: fullPath,
|
fullPath: fullPath,
|
||||||
}
|
}
|
||||||
@ -820,14 +820,14 @@ func findWildcard(path string) (wildcard string, i int, valid bool) {
|
|||||||
|
|
||||||
func countParams(path string) uint16 {
|
func countParams(path string) uint16 {
|
||||||
var n uint16
|
var n uint16
|
||||||
s := convert.StringToBytes(path)
|
s := codec.StringToBytes(path)
|
||||||
n += uint16(bytes.Count(s, strColon))
|
n += uint16(bytes.Count(s, strColon))
|
||||||
n += uint16(bytes.Count(s, strStar))
|
n += uint16(bytes.Count(s, strStar))
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func countSections(path string) uint16 {
|
func countSections(path string) uint16 {
|
||||||
s := convert.StringToBytes(path)
|
s := codec.StringToBytes(path)
|
||||||
return uint16(bytes.Count(s, strSlash))
|
return uint16(bytes.Count(s, strSlash))
|
||||||
}
|
}
|
||||||
func min(a, b int) int {
|
func min(a, b int) int {
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"go_dreamfactory/lego/utils/convert"
|
"go_dreamfactory/lego/utils/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// JSON contains the given interface object.
|
// JSON contains the given interface object.
|
||||||
@ -96,9 +96,9 @@ func (r SecureJSON) Render(w http.ResponseWriter) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// if the jsonBytes is array values
|
// if the jsonBytes is array values
|
||||||
if bytes.HasPrefix(jsonBytes, convert.StringToBytes("[")) && bytes.HasSuffix(jsonBytes,
|
if bytes.HasPrefix(jsonBytes, codec.StringToBytes("[")) && bytes.HasSuffix(jsonBytes,
|
||||||
convert.StringToBytes("]")) {
|
codec.StringToBytes("]")) {
|
||||||
if _, err = w.Write(convert.StringToBytes(r.Prefix)); err != nil {
|
if _, err = w.Write(codec.StringToBytes(r.Prefix)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,11 +125,11 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
callback := template.JSEscapeString(r.Callback)
|
callback := template.JSEscapeString(r.Callback)
|
||||||
if _, err = w.Write(convert.StringToBytes(callback)); err != nil {
|
if _, err = w.Write(codec.StringToBytes(callback)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = w.Write(convert.StringToBytes("(")); err != nil {
|
if _, err = w.Write(codec.StringToBytes("(")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = w.Write(convert.StringToBytes(");")); err != nil {
|
if _, err = w.Write(codec.StringToBytes(");")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
for _, r := range convert.BytesToString(ret) {
|
for _, r := range codec.BytesToString(ret) {
|
||||||
cvt := string(r)
|
cvt := string(r)
|
||||||
if r >= 128 {
|
if r >= 128 {
|
||||||
cvt = fmt.Sprintf("\\u%04x", int64(r))
|
cvt = fmt.Sprintf("\\u%04x", int64(r))
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"go_dreamfactory/lego/utils/convert"
|
"go_dreamfactory/lego/utils/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// String contains the given interface object slice and its format.
|
// String contains the given interface object slice and its format.
|
||||||
@ -32,6 +32,6 @@ func WriteString(w http.ResponseWriter, format string, data []interface{}) (err
|
|||||||
_, err = fmt.Fprintf(w, format, data...)
|
_, err = fmt.Fprintf(w, format, data...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = w.Write(convert.StringToBytes(format))
|
_, err = w.Write(codec.StringToBytes(format))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,15 @@ package cluster
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"go_dreamfactory/lego/utils/codec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSys(RedisUrl []string, RedisPassword string, timeOut time.Duration,
|
func NewSys(RedisUrl []string, RedisPassword string, timeOut time.Duration,
|
||||||
encode func(value interface{}) (result []byte, err error),
|
encode codec.IEncoder,
|
||||||
decode func(value []byte, result interface{}) (err error),
|
decode codec.IDecoder,
|
||||||
) (sys *Redis, err error) {
|
) (sys *Redis, err error) {
|
||||||
var (
|
var (
|
||||||
client *redis.ClusterClient
|
client *redis.ClusterClient
|
||||||
@ -21,8 +22,8 @@ func NewSys(RedisUrl []string, RedisPassword string, timeOut time.Duration,
|
|||||||
sys = &Redis{
|
sys = &Redis{
|
||||||
client: client,
|
client: client,
|
||||||
timeOut: timeOut,
|
timeOut: timeOut,
|
||||||
Encode: encode,
|
encode: encode,
|
||||||
Decode: decode,
|
decode: decode,
|
||||||
}
|
}
|
||||||
_, err = sys.Ping()
|
_, err = sys.Ping()
|
||||||
return
|
return
|
||||||
@ -31,8 +32,8 @@ func NewSys(RedisUrl []string, RedisPassword string, timeOut time.Duration,
|
|||||||
type Redis struct {
|
type Redis struct {
|
||||||
client *redis.ClusterClient
|
client *redis.ClusterClient
|
||||||
timeOut time.Duration
|
timeOut time.Duration
|
||||||
Encode func(value interface{}) (result []byte, err error)
|
encode codec.IEncoder
|
||||||
Decode func(value []byte, result interface{}) (err error)
|
decode codec.IDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Redis) getContext() (ctx context.Context) {
|
func (this *Redis) getContext() (ctx context.Context) {
|
||||||
|
@ -28,13 +28,34 @@ func (this *Redis) HExists(key string, field string) (result bool, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。
|
||||||
|
此命令会覆盖哈希表中已存在的字段。
|
||||||
|
如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作
|
||||||
|
*/
|
||||||
|
func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
||||||
|
agrs := make([]interface{}, 0)
|
||||||
|
agrs = append(agrs, "HMSET")
|
||||||
|
agrs = append(agrs, key)
|
||||||
|
var data map[string][]byte
|
||||||
|
if data, err = this.encode.EncoderToMap(v); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for k, v := range data {
|
||||||
|
result, _ := this.encode.Encoder(v)
|
||||||
|
agrs = append(agrs, k, result)
|
||||||
|
}
|
||||||
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Hget 命令用于返回哈希表中指定字段的值
|
Redis Hget 命令用于返回哈希表中指定字段的值
|
||||||
*/
|
*/
|
||||||
func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
|
||||||
var resultvalue string
|
var resultvalue string
|
||||||
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
|
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(resultvalue), value)
|
err = this.decode.Decoder([]byte(resultvalue), value)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -43,18 +64,12 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error)
|
|||||||
Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
|
Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
|
||||||
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍
|
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍
|
||||||
*/
|
*/
|
||||||
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) {
|
func (this *Redis) HGetAll(key string, v interface{}) (err error) {
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key)
|
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key)
|
||||||
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.DecoderMap(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -117,7 +132,7 @@ func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,23 +140,6 @@ func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。
|
|
||||||
此命令会覆盖哈希表中已存在的字段。
|
|
||||||
如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作
|
|
||||||
*/
|
|
||||||
func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) {
|
|
||||||
agrs := make([]interface{}, 0)
|
|
||||||
agrs = append(agrs, "HMSET")
|
|
||||||
agrs = append(agrs, key)
|
|
||||||
for k, v := range value {
|
|
||||||
result, _ := this.Encode(v)
|
|
||||||
agrs = append(agrs, k, result)
|
|
||||||
}
|
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Hset 命令用于为哈希表中的字段赋值
|
Redis Hset 命令用于为哈希表中的字段赋值
|
||||||
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
||||||
@ -149,7 +147,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
|
|||||||
*/
|
*/
|
||||||
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue []byte
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -163,7 +161,7 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
|
|||||||
*/
|
*/
|
||||||
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue []byte
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -13,7 +13,7 @@ Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使
|
|||||||
func (this *Redis) Lindex(key string, value interface{}) (err error) {
|
func (this *Redis) Lindex(key string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -30,8 +30,8 @@ func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value i
|
|||||||
tagervalue []byte
|
tagervalue []byte
|
||||||
resultvalue []byte
|
resultvalue []byte
|
||||||
)
|
)
|
||||||
if tagervalue, err = this.Encode(tager); err == nil {
|
if tagervalue, err = this.encode.Encoder(tager); err == nil {
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
if isbefore {
|
if isbefore {
|
||||||
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
|
||||||
} else {
|
} else {
|
||||||
@ -56,7 +56,7 @@ Redis Lpop 命令用于移除并返回列表的第一个元素
|
|||||||
func (this *Redis) LPop(key string, value interface{}) (err error) {
|
func (this *Redis) LPop(key string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -70,7 +70,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(v)
|
result, _ := this.encode.Encoder(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 +84,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(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -103,7 +103,7 @@ func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (r
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,7 +120,7 @@ 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 []byte
|
||||||
if resultvalue, err = this.Encode(target); err == nil {
|
if resultvalue, err = this.encode.Encoder(target); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -132,7 +132,7 @@ Redis 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 []byte
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -154,7 +154,7 @@ Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的
|
|||||||
func (this *Redis) Rpop(key string, value interface{}) (err error) {
|
func (this *Redis) Rpop(key string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素
|
|||||||
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) {
|
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -183,7 +183,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(v)
|
result, _ := this.encode.Encoder(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 +197,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(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
|
@ -12,7 +12,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(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -39,7 +39,7 @@ func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []inter
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []inte
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interf
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,7 +160,7 @@ func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []inte
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
*/
|
*/
|
||||||
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 []byte
|
||||||
if result, err = this.Encode(value); err == nil {
|
if result, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
|
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -40,7 +40,7 @@ func (this *Redis) MSet(keyvalues 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 keyvalues {
|
||||||
result, _ := this.Encode(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -54,7 +54,7 @@ func (this *Redis) MSetNX(keyvalues 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 keyvalues {
|
||||||
result, _ := this.Encode(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -121,7 +121,7 @@ Redis Append 命令用于为指定的 key 追加值。
|
|||||||
*/
|
*/
|
||||||
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 []byte
|
||||||
if result, err = this.Encode(value); err == nil {
|
if result, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -133,7 +133,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
|
|||||||
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 []byte
|
||||||
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
|
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
|
||||||
err = this.Decode(result, value)
|
err = this.decode.Decoder(result, value)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -146,9 +146,9 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
|
|||||||
data string
|
data string
|
||||||
_value []byte
|
_value []byte
|
||||||
)
|
)
|
||||||
if _value, err = this.Encode(value); err == nil {
|
if _value, err = this.encode.Encoder(value); err == nil {
|
||||||
if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), result)
|
err = this.decode.Decoder([]byte(data), result)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,7 +82,7 @@ func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZR
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, st
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,7 +176,7 @@ func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *red
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,13 +65,13 @@ type (
|
|||||||
HDel(key string, fields ...string) (err error)
|
HDel(key string, fields ...string) (err error)
|
||||||
HExists(key string, field string) (result bool, err error)
|
HExists(key string, field string) (result bool, err error)
|
||||||
HGet(key string, field string, value interface{}) (err error)
|
HGet(key string, field string, value interface{}) (err error)
|
||||||
HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error)
|
HGetAll(key string, v interface{}) (err error)
|
||||||
HIncrBy(key string, field string, value int) (err error)
|
HIncrBy(key string, field string, value int) (err error)
|
||||||
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, valuetype reflect.Type, fields ...string) (result []interface{}, err error)
|
||||||
HMSet(key string, value map[string]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*/
|
||||||
@ -115,8 +115,6 @@ type (
|
|||||||
|
|
||||||
ISys interface {
|
ISys interface {
|
||||||
IRedis
|
IRedis
|
||||||
Encode(value interface{}) (result []byte, err error)
|
|
||||||
Decode(value []byte, result interface{}) (err error)
|
|
||||||
/*Lock*/
|
/*Lock*/
|
||||||
NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error)
|
NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error)
|
||||||
}
|
}
|
||||||
@ -155,19 +153,11 @@ func TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err
|
|||||||
func Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) {
|
func Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) {
|
||||||
return defsys.Watch(ctx, fn)
|
return defsys.Watch(ctx, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Encode(value interface{}) (result []byte, err error) {
|
|
||||||
return defsys.Encode(value)
|
|
||||||
}
|
|
||||||
func Decode(value []byte, result interface{}) (err error) {
|
|
||||||
return defsys.Decode(value, result)
|
|
||||||
}
|
|
||||||
func Delete(key string) (err error) {
|
func Delete(key string) (err error) {
|
||||||
return defsys.Delete(key)
|
return defsys.Delete(key)
|
||||||
}
|
}
|
||||||
func ExistsKey(key string) (iskeep bool, err error) {
|
func ExistsKey(key string) (iskeep bool, err error) {
|
||||||
return defsys.ExistsKey(key)
|
return defsys.ExistsKey(key)
|
||||||
|
|
||||||
}
|
}
|
||||||
func ExpireKey(key string, expire int) (err error) {
|
func ExpireKey(key string, expire int) (err error) {
|
||||||
return defsys.ExpireKey(key, expire)
|
return defsys.ExpireKey(key, expire)
|
||||||
@ -315,8 +305,8 @@ func HExists(key string, field string) (result bool, err error) {
|
|||||||
func HGet(key string, field string, value interface{}) (err error) {
|
func HGet(key string, field string, value interface{}) (err error) {
|
||||||
return defsys.HGet(key, field, value)
|
return defsys.HGet(key, field, value)
|
||||||
}
|
}
|
||||||
func HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) {
|
func HGetAll(key string, v interface{}) (err error) {
|
||||||
return defsys.HGetAll(key, valuetype)
|
return defsys.HGetAll(key, v)
|
||||||
}
|
}
|
||||||
func HIncrBy(key string, field string, value int) (err error) {
|
func HIncrBy(key string, field string, value int) (err error) {
|
||||||
return defsys.HIncrBy(key, field, value)
|
return defsys.HIncrBy(key, field, value)
|
||||||
@ -333,8 +323,8 @@ func Hlen(key string) (result int, err error) {
|
|||||||
func HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) {
|
func HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) {
|
||||||
return defsys.HMGet(key, valuetype, fields...)
|
return defsys.HMGet(key, valuetype, fields...)
|
||||||
}
|
}
|
||||||
func HMSet(key string, value map[string]interface{}) (err error) {
|
func HMSet(key string, v interface{}) (err error) {
|
||||||
return defsys.HMSet(key, value)
|
return defsys.HMSet(key, v)
|
||||||
}
|
}
|
||||||
func HSet(key string, field string, value interface{}) (err error) {
|
func HSet(key string, field string, value interface{}) (err error) {
|
||||||
return defsys.HSet(key, field, value)
|
return defsys.HSet(key, field, value)
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"go_dreamfactory/lego/sys/redis/cluster"
|
"go_dreamfactory/lego/sys/redis/cluster"
|
||||||
"go_dreamfactory/lego/sys/redis/single"
|
"go_dreamfactory/lego/sys/redis/single"
|
||||||
|
"go_dreamfactory/lego/utils/codec"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
@ -17,6 +18,13 @@ import (
|
|||||||
|
|
||||||
func newSys(options Options) (sys *Redis, err error) {
|
func newSys(options Options) (sys *Redis, err error) {
|
||||||
sys = &Redis{options: options}
|
sys = &Redis{options: options}
|
||||||
|
if options.RedisStorageType == JsonData {
|
||||||
|
sys.decoder = &codec.Decoder{DefDecoder: jsoniter.Unmarshal}
|
||||||
|
sys.encoder = &codec.Encoder{DefEncoder: jsoniter.Marshal}
|
||||||
|
} else {
|
||||||
|
sys.decoder = &codec.Decoder{DefDecoder: func(buf []byte, v interface{}) error { return proto.Unmarshal(buf, v.(proto.Message)) }}
|
||||||
|
sys.encoder = &codec.Encoder{DefEncoder: func(v interface{}) (data []byte, err error) { return proto.Marshal(v.(proto.Message)) }}
|
||||||
|
}
|
||||||
err = sys.init()
|
err = sys.init()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -24,6 +32,8 @@ func newSys(options Options) (sys *Redis, err error) {
|
|||||||
type Redis struct {
|
type Redis struct {
|
||||||
options Options
|
options Options
|
||||||
client IRedis
|
client IRedis
|
||||||
|
decoder codec.IDecoder
|
||||||
|
encoder codec.IEncoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Redis) init() (err error) {
|
func (this *Redis) init() (err error) {
|
||||||
@ -34,16 +44,16 @@ func (this *Redis) init() (err error) {
|
|||||||
this.options.Redis_Single_DB,
|
this.options.Redis_Single_DB,
|
||||||
this.options.Redis_Single_PoolSize,
|
this.options.Redis_Single_PoolSize,
|
||||||
this.options.TimeOut,
|
this.options.TimeOut,
|
||||||
this.Encode,
|
this.encoder,
|
||||||
this.Decode,
|
this.decoder,
|
||||||
)
|
)
|
||||||
} else if this.options.RedisType == Redis_Cluster {
|
} else if this.options.RedisType == Redis_Cluster {
|
||||||
this.client, err = cluster.NewSys(
|
this.client, err = cluster.NewSys(
|
||||||
this.options.Redis_Cluster_Addr,
|
this.options.Redis_Cluster_Addr,
|
||||||
this.options.Redis_Cluster_Password,
|
this.options.Redis_Cluster_Password,
|
||||||
this.options.TimeOut,
|
this.options.TimeOut,
|
||||||
this.Encode,
|
this.encoder,
|
||||||
this.Decode,
|
this.decoder,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("init Redis err:RedisType - %d", this.options.RedisType)
|
err = fmt.Errorf("init Redis err:RedisType - %d", this.options.RedisType)
|
||||||
@ -73,33 +83,6 @@ func (this *Redis) UnLock(key string) (err error) {
|
|||||||
return this.client.UnLock(key)
|
return this.client.UnLock(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
///数据编码
|
|
||||||
func (this *Redis) Encode(value interface{}) (result []byte, err error) {
|
|
||||||
if this.options.RedisStorageType == JsonData {
|
|
||||||
result, err = jsoniter.Marshal(value)
|
|
||||||
} else {
|
|
||||||
if _, ok := value.(proto.Message); ok {
|
|
||||||
result, err = proto.Marshal(value.(proto.Message))
|
|
||||||
} else {
|
|
||||||
result, err = jsoniter.Marshal(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Redis) Decode(value []byte, result interface{}) (err error) {
|
|
||||||
if this.options.RedisStorageType == JsonData {
|
|
||||||
err = jsoniter.Unmarshal(value, result)
|
|
||||||
} else {
|
|
||||||
if _, ok := result.(proto.Message); ok {
|
|
||||||
err = proto.Unmarshal(value, result.(proto.Message))
|
|
||||||
} else {
|
|
||||||
err = jsoniter.Unmarshal(value, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Redis) Delete(key string) (err error) {
|
func (this *Redis) Delete(key string) (err error) {
|
||||||
return this.client.Delete(key)
|
return this.client.Delete(key)
|
||||||
}
|
}
|
||||||
@ -242,8 +225,8 @@ func (this *Redis) HExists(key string, field string) (result bool, err error) {
|
|||||||
func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
|
||||||
return this.client.HGet(key, field, value)
|
return this.client.HGet(key, field, value)
|
||||||
}
|
}
|
||||||
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) {
|
func (this *Redis) HGetAll(key string, v interface{}) (err error) {
|
||||||
return this.client.HGetAll(key, valuetype)
|
return this.client.HGetAll(key, v)
|
||||||
}
|
}
|
||||||
func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
|
func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
|
||||||
return this.client.HIncrBy(key, field, value)
|
return this.client.HIncrBy(key, field, value)
|
||||||
@ -260,8 +243,8 @@ func (this *Redis) Hlen(key string) (result int, err error) {
|
|||||||
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) {
|
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) {
|
||||||
return this.client.HMGet(key, valuetype, fields...)
|
return this.client.HMGet(key, valuetype, fields...)
|
||||||
}
|
}
|
||||||
func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) {
|
func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
||||||
return this.client.HMSet(key, value)
|
return this.client.HMSet(key, v)
|
||||||
}
|
}
|
||||||
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
||||||
return this.client.HSet(key, field, value)
|
return this.client.HSet(key, field, value)
|
||||||
|
@ -2,14 +2,15 @@ package single
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"go_dreamfactory/lego/utils/codec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.Duration,
|
func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.Duration,
|
||||||
encode func(value interface{}) (result []byte, err error),
|
encode codec.IEncoder,
|
||||||
decode func(value []byte, result interface{}) (err error),
|
decode codec.IDecoder,
|
||||||
) (sys *Redis, err error) {
|
) (sys *Redis, err error) {
|
||||||
var (
|
var (
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
@ -23,8 +24,8 @@ func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.
|
|||||||
sys = &Redis{
|
sys = &Redis{
|
||||||
client: client,
|
client: client,
|
||||||
timeOut: timeOut,
|
timeOut: timeOut,
|
||||||
Encode: encode,
|
encode: encode,
|
||||||
Decode: decode,
|
decode: decode,
|
||||||
}
|
}
|
||||||
_, err = sys.Ping()
|
_, err = sys.Ping()
|
||||||
return
|
return
|
||||||
@ -33,8 +34,8 @@ func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.
|
|||||||
type Redis struct {
|
type Redis struct {
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
timeOut time.Duration
|
timeOut time.Duration
|
||||||
Encode func(value interface{}) (result []byte, err error)
|
encode codec.IEncoder
|
||||||
Decode func(value []byte, result interface{}) (err error)
|
decode codec.IDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Redis) getContext() (ctx context.Context) {
|
func (this *Redis) getContext() (ctx context.Context) {
|
||||||
|
@ -29,13 +29,23 @@ func (this *Redis) HExists(key string, field string) (result bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Hget 命令用于返回哈希表中指定字段的值
|
Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。
|
||||||
|
此命令会覆盖哈希表中已存在的字段。
|
||||||
|
如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作
|
||||||
*/
|
*/
|
||||||
func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HMSet(key string, v interface{}) (err error) {
|
||||||
var resultvalue string
|
agrs := make([]interface{}, 0)
|
||||||
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
|
agrs = append(agrs, "HMSET")
|
||||||
err = this.Decode([]byte(resultvalue), value)
|
agrs = append(agrs, key)
|
||||||
|
var data map[string][]byte
|
||||||
|
if data, err = this.encode.EncoderToMap(v); err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
for k, v := range data {
|
||||||
|
result, _ := this.encode.Encoder(v)
|
||||||
|
agrs = append(agrs, k, result)
|
||||||
|
}
|
||||||
|
err = this.client.Do(this.getContext(), agrs...).Err()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,18 +53,36 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error)
|
|||||||
Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
|
Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
|
||||||
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍
|
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍
|
||||||
*/
|
*/
|
||||||
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) {
|
func (this *Redis) HGetAll(key string, v interface{}) (err error) {
|
||||||
cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key)
|
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key)
|
||||||
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.DecoderMap(_result, v)
|
||||||
for i, v := range _result {
|
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
|
||||||
result[i] = temp
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Redis 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
|
||||||
}
|
}
|
||||||
@ -117,7 +145,7 @@ func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,36 +153,6 @@ func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。
|
|
||||||
此命令会覆盖哈希表中已存在的字段。
|
|
||||||
如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作
|
|
||||||
*/
|
|
||||||
func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) {
|
|
||||||
agrs := make([]interface{}, 0)
|
|
||||||
agrs = append(agrs, "HMSET")
|
|
||||||
agrs = append(agrs, key)
|
|
||||||
for k, v := range value {
|
|
||||||
result, _ := this.Encode(v)
|
|
||||||
agrs = append(agrs, k, result)
|
|
||||||
}
|
|
||||||
err = this.client.Do(this.getContext(), agrs...).Err()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Redis Hset 命令用于为哈希表中的字段赋值
|
|
||||||
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
|
||||||
如果字段已经存在于哈希表中,旧值将被覆盖
|
|
||||||
*/
|
|
||||||
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
|
|
||||||
var resultvalue []byte
|
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
|
||||||
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
|
Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
|
||||||
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作
|
||||||
@ -163,7 +161,7 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
|
|||||||
*/
|
*/
|
||||||
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
|
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
|
||||||
var resultvalue []byte
|
var resultvalue []byte
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -13,7 +13,7 @@ Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使
|
|||||||
func (this *Redis) Lindex(key string, value interface{}) (err error) {
|
func (this *Redis) Lindex(key string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -30,8 +30,8 @@ func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value i
|
|||||||
tagervalue []byte
|
tagervalue []byte
|
||||||
resultvalue []byte
|
resultvalue []byte
|
||||||
)
|
)
|
||||||
if tagervalue, err = this.Encode(tager); err == nil {
|
if tagervalue, err = this.encode.Encoder(tager); err == nil {
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
if isbefore {
|
if isbefore {
|
||||||
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
|
||||||
} else {
|
} else {
|
||||||
@ -56,7 +56,7 @@ Redis Lpop 命令用于移除并返回列表的第一个元素
|
|||||||
func (this *Redis) LPop(key string, value interface{}) (err error) {
|
func (this *Redis) LPop(key string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -70,7 +70,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(v)
|
result, _ := this.encode.Encoder(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 +84,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(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -103,7 +103,7 @@ func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (r
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,7 +120,7 @@ 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 []byte
|
||||||
if resultvalue, err = this.Encode(target); err == nil {
|
if resultvalue, err = this.encode.Encoder(target); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -132,7 +132,7 @@ Redis 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 []byte
|
||||||
if resultvalue, err = this.Encode(value); err == nil {
|
if resultvalue, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -154,7 +154,7 @@ Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的
|
|||||||
func (this *Redis) Rpop(key string, value interface{}) (err error) {
|
func (this *Redis) Rpop(key string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素
|
|||||||
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) {
|
func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) {
|
||||||
var data string
|
var data string
|
||||||
if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), value)
|
err = this.decode.Decoder([]byte(data), value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
@ -183,7 +183,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(v)
|
result, _ := this.encode.Encoder(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 +197,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(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
|
@ -12,7 +12,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(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -39,7 +39,7 @@ func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []inter
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []inte
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interf
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,7 +160,7 @@ func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []inte
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
*/
|
*/
|
||||||
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 []byte
|
||||||
if result, err = this.Encode(value); err == nil {
|
if result, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
|
err = this.client.Set(this.getContext(), string(key), result, expiration).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -40,7 +40,7 @@ func (this *Redis) MSet(keyvalues 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 keyvalues {
|
||||||
result, _ := this.Encode(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -54,7 +54,7 @@ func (this *Redis) MSetNX(keyvalues 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 keyvalues {
|
||||||
result, _ := this.Encode(v)
|
result, _ := this.encode.Encoder(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()
|
||||||
@ -121,7 +121,7 @@ Redis Append 命令用于为指定的 key 追加值。
|
|||||||
*/
|
*/
|
||||||
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 []byte
|
||||||
if result, err = this.Encode(value); err == nil {
|
if result, err = this.encode.Encoder(value); err == nil {
|
||||||
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -133,7 +133,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
|
|||||||
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 []byte
|
||||||
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
|
if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil {
|
||||||
err = this.Decode(result, value)
|
err = this.decode.Decoder(result, value)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -146,9 +146,9 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
|
|||||||
data string
|
data string
|
||||||
_value []byte
|
_value []byte
|
||||||
)
|
)
|
||||||
if _value, err = this.Encode(value); err == nil {
|
if _value, err = this.encode.Encoder(value); err == nil {
|
||||||
if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) {
|
if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) {
|
||||||
err = this.Decode([]byte(data), result)
|
err = this.decode.Decoder([]byte(data), result)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(string(redis.Nil))
|
err = fmt.Errorf(string(redis.Nil))
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,7 +82,7 @@ func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZR
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, st
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,7 +176,7 @@ func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *red
|
|||||||
result = make([]interface{}, len(_result))
|
result = make([]interface{}, len(_result))
|
||||||
for i, v := range _result {
|
for i, v := range _result {
|
||||||
temp := reflect.New(valuetype.Elem()).Interface()
|
temp := reflect.New(valuetype.Elem()).Interface()
|
||||||
if err = this.Decode([]byte(v), &temp); err == nil {
|
if err = this.decode.Decoder([]byte(v), &temp); err == nil {
|
||||||
result[i] = temp
|
result[i] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package redis_test
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -10,6 +11,23 @@ import (
|
|||||||
"go_dreamfactory/lego/sys/redis"
|
"go_dreamfactory/lego/sys/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
if err := redis.OnInit(nil,
|
||||||
|
redis.SetRedisType(redis.Redis_Cluster),
|
||||||
|
redis.SetRedis_Cluster_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}),
|
||||||
|
redis.SetRedis_Cluster_Password(""),
|
||||||
|
redis.SetRedisStorageType(redis.JsonData),
|
||||||
|
); err != nil {
|
||||||
|
fmt.Println("err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.Exit(m.Run())
|
||||||
|
// if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
|
||||||
|
// fmt.Printf("err:%v\n", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
func Test_SysIPV6(t *testing.T) {
|
func Test_SysIPV6(t *testing.T) {
|
||||||
err := redis.OnInit(map[string]interface{}{
|
err := redis.OnInit(map[string]interface{}{
|
||||||
"Redis_Single_Addr": "172.27.100.143:6382",
|
"Redis_Single_Addr": "172.27.100.143:6382",
|
||||||
@ -139,5 +157,36 @@ func Test_Redis_Type(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Printf("Test_Redis_Type:%s \n", ty)
|
fmt.Printf("Test_Redis_Type:%s \n", ty)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestData struct {
|
||||||
|
Name string
|
||||||
|
Agr int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Redis_Encoder_Struct(t *testing.T) {
|
||||||
|
err := redis.Set("test:1001", &TestData{Name: "liwei1dao", Agr: 12}, -1)
|
||||||
|
fmt.Printf("err:%v\n", err)
|
||||||
|
}
|
||||||
|
func Test_Redis_Encoder_int(t *testing.T) {
|
||||||
|
err := redis.Set("test:1002", 856, -1)
|
||||||
|
fmt.Printf("err:%v \n", err)
|
||||||
|
data := 0
|
||||||
|
err = redis.Get("test:1002", &data)
|
||||||
|
fmt.Printf("data:%d err:%v\n", data, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Redis_Encoder_Hash(t *testing.T) {
|
||||||
|
// err := redis.HMSet("test:1003", &TestData{Name: "liwei1dao", Agr: 12})
|
||||||
|
// fmt.Printf("err:%v\n", err)
|
||||||
|
// data := &TestData{}
|
||||||
|
// err = redis.HGetAll("test:1003", data)
|
||||||
|
// fmt.Printf("data:%v err:%v\n", data, err)
|
||||||
|
|
||||||
|
data1 := map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}}
|
||||||
|
err := redis.HMSet("test:1004", data1)
|
||||||
|
fmt.Printf("err:%v\n", err)
|
||||||
|
data2 := make(map[string]*TestData)
|
||||||
|
err = redis.HGetAll("test:1004", data2)
|
||||||
|
fmt.Printf("data2:%v err:%v\n", data2, err)
|
||||||
}
|
}
|
||||||
|
29
lego/utils/codec/codec_test.go
Normal file
29
lego/utils/codec/codec_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package codec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestData struct {
|
||||||
|
Fild_1 string
|
||||||
|
Fild_3 int
|
||||||
|
Fild_4 float32
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Encoder(t *testing.T) {
|
||||||
|
encoder := &Encoder{}
|
||||||
|
// data, err := encoder.EncoderToMap(map[string]interface{}{"liwei": 106, "sasd": "2564"})
|
||||||
|
// fmt.Printf("EncoderToMap data1:%v err:%v", data, err)
|
||||||
|
data, err := encoder.EncoderToMap([]interface{}{"liwei", 106, "sasd", "2564"})
|
||||||
|
fmt.Printf("EncoderToMap data1:%v err:%v", data, err)
|
||||||
|
// data, err := encoder.EncoderToMap(&TestData{Fild_1: "liwei1dao", Fild_3: 25, Fild_4: 3.54})
|
||||||
|
// fmt.Printf("EncoderToMap data2:%v err:%v", data, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Decoder(t *testing.T) {
|
||||||
|
decoder := &Decoder{}
|
||||||
|
data := &TestData{}
|
||||||
|
err := decoder.DecoderMap(map[string]string{"Fild_1": "liwei1dao"}, data)
|
||||||
|
fmt.Printf("DecoderMap data1:%v err:%v", data, err)
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package convert
|
package codec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
@ -6,68 +6,22 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ByteToBytes(v byte) []byte {
|
const host32bit = ^uint(0)>>32 == 0
|
||||||
return []byte{v}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BoolToBytes(v bool) []byte {
|
type (
|
||||||
var buf = make([]byte, 1)
|
IDecoder interface {
|
||||||
if v {
|
Decoder(buf []byte, v interface{}) error
|
||||||
buf[0] = 1
|
DecoderMap(data map[string]string, v interface{}) error
|
||||||
} else {
|
|
||||||
buf[0] = 0
|
|
||||||
}
|
}
|
||||||
return buf
|
IEncoder interface {
|
||||||
}
|
Encoder(v interface{}) (data []byte, err error)
|
||||||
func Int8ToBytes(v int8) []byte {
|
EncoderToMap(v interface{}) (data map[string][]byte, err error)
|
||||||
return []byte{(byte(v))}
|
}
|
||||||
}
|
)
|
||||||
func Int16ToBytes(v int16) []byte {
|
|
||||||
var buf = make([]byte, 2)
|
// string
|
||||||
binary.BigEndian.PutUint16(buf, uint16(v))
|
func BytesToString(b []byte) string {
|
||||||
return buf
|
return *(*string)(unsafe.Pointer(&b))
|
||||||
}
|
|
||||||
func UInt16ToBytes(v uint16) []byte {
|
|
||||||
var buf = make([]byte, 2)
|
|
||||||
binary.BigEndian.PutUint16(buf, v)
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
func IntToBytes(v int) []byte {
|
|
||||||
var buf = make([]byte, 4)
|
|
||||||
binary.BigEndian.PutUint32(buf, uint32(v))
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
func Int32ToBytes(v int32) []byte {
|
|
||||||
var buf = make([]byte, 4)
|
|
||||||
binary.BigEndian.PutUint32(buf, uint32(v))
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
func UInt32ToBytes(v uint32) []byte {
|
|
||||||
var buf = make([]byte, 4)
|
|
||||||
binary.BigEndian.PutUint32(buf, v)
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
func Int64ToBytes(v int64) []byte {
|
|
||||||
var buf = make([]byte, 8)
|
|
||||||
binary.BigEndian.PutUint64(buf, uint64(v))
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
func UInt64ToBytes(v uint64) []byte {
|
|
||||||
var buf = make([]byte, 8)
|
|
||||||
binary.BigEndian.PutUint64(buf, v)
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
func Float32ToBytes(v float32) []byte {
|
|
||||||
bits := math.Float32bits(v)
|
|
||||||
bytes := make([]byte, 4)
|
|
||||||
binary.LittleEndian.PutUint32(bytes, bits)
|
|
||||||
return bytes
|
|
||||||
}
|
|
||||||
func Float64ToBytes(v float64) []byte {
|
|
||||||
bits := math.Float64bits(v)
|
|
||||||
bytes := make([]byte, 8)
|
|
||||||
binary.LittleEndian.PutUint64(bytes, bits)
|
|
||||||
return bytes
|
|
||||||
}
|
}
|
||||||
func StringToBytes(s string) []byte {
|
func StringToBytes(s string) []byte {
|
||||||
return *(*[]byte)(unsafe.Pointer(
|
return *(*[]byte)(unsafe.Pointer(
|
||||||
@ -78,48 +32,150 @@ func StringToBytes(s string) []byte {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
func BytesTobyte(buf []byte) byte {
|
// int
|
||||||
var data byte = buf[0]
|
func IntToBytes(v int) []byte {
|
||||||
return data
|
if host32bit {
|
||||||
|
return Int32ToBytes(int32(v))
|
||||||
|
} else {
|
||||||
|
return Int64ToBytes(int64(v))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func BytesToBool(buf []byte) bool {
|
func BytesToInt(buf []byte) int {
|
||||||
var data bool = buf[0] != 0
|
if host32bit {
|
||||||
return data
|
return int(BytesToInt32(buf))
|
||||||
|
} else {
|
||||||
|
return int(BytesToInt64(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//int8
|
||||||
|
func Int8ToBytes(v int8) []byte {
|
||||||
|
return []byte{(byte(v))}
|
||||||
}
|
}
|
||||||
func BytesToInt8(buf []byte) int8 {
|
func BytesToInt8(buf []byte) int8 {
|
||||||
var data int8 = int8(buf[0])
|
return int8(buf[0])
|
||||||
return data
|
}
|
||||||
|
|
||||||
|
//int16
|
||||||
|
func Int16ToBytes(v int16) []byte {
|
||||||
|
var buf = make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(buf, uint16(v))
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
func BytesToInt16(buf []byte) int16 {
|
func BytesToInt16(buf []byte) int16 {
|
||||||
return int16(binary.BigEndian.Uint16(buf))
|
return int16(binary.BigEndian.Uint16(buf))
|
||||||
}
|
}
|
||||||
func BytesToUInt16(buf []byte) uint16 {
|
|
||||||
return binary.BigEndian.Uint16(buf)
|
//int32
|
||||||
}
|
func Int32ToBytes(v int32) []byte {
|
||||||
func BytesToInt(buf []byte) int {
|
var buf = make([]byte, 4)
|
||||||
return int(binary.BigEndian.Uint32(buf))
|
binary.BigEndian.PutUint32(buf, uint32(v))
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
func BytesToInt32(buf []byte) int32 {
|
func BytesToInt32(buf []byte) int32 {
|
||||||
return int32(binary.BigEndian.Uint32(buf))
|
return int32(binary.BigEndian.Uint32(buf))
|
||||||
}
|
}
|
||||||
func BytesToUInt32(buf []byte) uint32 {
|
|
||||||
return binary.BigEndian.Uint32(buf)
|
//int64
|
||||||
|
func Int64ToBytes(v int64) []byte {
|
||||||
|
var buf = make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(buf, uint64(v))
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
func BytesToInt64(buf []byte) int64 {
|
func BytesToInt64(buf []byte) int64 {
|
||||||
return int64(binary.BigEndian.Uint64(buf))
|
return int64(binary.BigEndian.Uint64(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//uint
|
||||||
|
func UIntToBytes(v uint) []byte {
|
||||||
|
if host32bit {
|
||||||
|
return Int32ToBytes(int32(v))
|
||||||
|
} else {
|
||||||
|
return Int64ToBytes(int64(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BytesToUInt(buf []byte) uint {
|
||||||
|
if host32bit {
|
||||||
|
return uint(BytesToUInt32(buf))
|
||||||
|
} else {
|
||||||
|
return uint(BytesToUInt64(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//uint8
|
||||||
|
func UInt8ToBytes(v uint8) []byte {
|
||||||
|
return []byte{v}
|
||||||
|
}
|
||||||
|
func BytesToUInt8(buf []byte) uint8 {
|
||||||
|
var data uint8 = uint8(buf[0])
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
//uint16
|
||||||
|
func UInt16ToBytes(v uint16) []byte {
|
||||||
|
var buf = make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(buf, v)
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
func BytesToUInt16(buf []byte) uint16 {
|
||||||
|
return binary.BigEndian.Uint16(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
//uint32
|
||||||
|
func UInt32ToBytes(v uint32) []byte {
|
||||||
|
var buf = make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(buf, v)
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
func BytesToUInt32(buf []byte) uint32 {
|
||||||
|
return binary.BigEndian.Uint32(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
//uint64
|
||||||
func BytesToUInt64(buf []byte) uint64 {
|
func BytesToUInt64(buf []byte) uint64 {
|
||||||
return binary.BigEndian.Uint64(buf)
|
return binary.BigEndian.Uint64(buf)
|
||||||
}
|
}
|
||||||
|
func UInt64ToBytes(v uint64) []byte {
|
||||||
|
var buf = make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(buf, v)
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
//float32
|
||||||
|
func Float32ToBytes(v float32) []byte {
|
||||||
|
bits := math.Float32bits(v)
|
||||||
|
bytes := make([]byte, 4)
|
||||||
|
binary.LittleEndian.PutUint32(bytes, bits)
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
func BytesToFloat32(buf []byte) float32 {
|
func BytesToFloat32(buf []byte) float32 {
|
||||||
bits := binary.LittleEndian.Uint32(buf)
|
bits := binary.LittleEndian.Uint32(buf)
|
||||||
return math.Float32frombits(bits)
|
return math.Float32frombits(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//float64
|
||||||
|
func Float64ToBytes(v float64) []byte {
|
||||||
|
bits := math.Float64bits(v)
|
||||||
|
bytes := make([]byte, 8)
|
||||||
|
binary.LittleEndian.PutUint64(bytes, bits)
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
func BytesToFloat64(buf []byte) float64 {
|
func BytesToFloat64(buf []byte) float64 {
|
||||||
bits := binary.LittleEndian.Uint64(buf)
|
bits := binary.LittleEndian.Uint64(buf)
|
||||||
return math.Float64frombits(bits)
|
return math.Float64frombits(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BytesToString(b []byte) string {
|
//bool
|
||||||
return *(*string)(unsafe.Pointer(&b))
|
func BoolToBytes(v bool) []byte {
|
||||||
|
var buf = make([]byte, 1)
|
||||||
|
if v {
|
||||||
|
buf[0] = 1
|
||||||
|
} else {
|
||||||
|
buf[0] = 0
|
||||||
|
}
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
func BytesToBool(buf []byte) bool {
|
||||||
|
var data bool = buf[0] != 0
|
||||||
|
return data
|
||||||
}
|
}
|
141
lego/utils/codec/decoder.go
Normal file
141
lego/utils/codec/decoder.go
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
package codec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
//解码器
|
||||||
|
type Decoder struct {
|
||||||
|
DefDecoder func(buf []byte, v interface{}) error //默认编码 扩展自定义编码方式
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Decoder) Decoder(buf []byte, v interface{}) error {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return fmt.Errorf("decoder: Decoder(nil)")
|
||||||
|
case *string:
|
||||||
|
*v = BytesToString(buf)
|
||||||
|
return nil
|
||||||
|
case *[]byte:
|
||||||
|
*v = buf
|
||||||
|
return nil
|
||||||
|
case *int:
|
||||||
|
*v = BytesToInt(buf)
|
||||||
|
return nil
|
||||||
|
case *int8:
|
||||||
|
*v = BytesToInt8(buf)
|
||||||
|
return nil
|
||||||
|
case *int16:
|
||||||
|
*v = BytesToInt16(buf)
|
||||||
|
return nil
|
||||||
|
case *int32:
|
||||||
|
*v = BytesToInt32(buf)
|
||||||
|
return nil
|
||||||
|
case *int64:
|
||||||
|
*v = BytesToInt64(buf)
|
||||||
|
return nil
|
||||||
|
case *uint:
|
||||||
|
*v = BytesToUInt(buf)
|
||||||
|
return nil
|
||||||
|
case *uint8:
|
||||||
|
*v = BytesToUInt8(buf)
|
||||||
|
return nil
|
||||||
|
case *uint16:
|
||||||
|
*v = BytesToUInt16(buf)
|
||||||
|
return nil
|
||||||
|
case *uint32:
|
||||||
|
*v = BytesToUInt32(buf)
|
||||||
|
return nil
|
||||||
|
case *uint64:
|
||||||
|
*v = BytesToUInt64(buf)
|
||||||
|
return nil
|
||||||
|
case *float32:
|
||||||
|
*v = BytesToFloat32(buf)
|
||||||
|
return nil
|
||||||
|
case *float64:
|
||||||
|
*v = BytesToFloat64(buf)
|
||||||
|
return nil
|
||||||
|
case *bool:
|
||||||
|
*v = BytesToBool(buf)
|
||||||
|
return nil
|
||||||
|
case *time.Time:
|
||||||
|
var err error
|
||||||
|
*v, err = time.Parse(time.RFC3339Nano, BytesToString(buf))
|
||||||
|
return err
|
||||||
|
case *time.Duration:
|
||||||
|
*v = time.Duration(BytesToInt64(buf))
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
if this.DefDecoder != nil {
|
||||||
|
return this.DefDecoder(buf, v)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"decoder: can't marshal %T (implement decoder.DefDecoder)", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Decoder) DecoderMap(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.Decoder(StringToBytes(k), key.Addr().Interface()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", key, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := this.Decoder(StringToBytes(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.Decoder(StringToBytes(v), value.Addr().Interface()); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
value.Interface()
|
||||||
|
if err := this.Decoder(StringToBytes(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.Decoder(StringToBytes(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.Decoder(StringToBytes(value), v); err != nil {
|
||||||
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
112
lego/utils/codec/encoder.go
Normal file
112
lego/utils/codec/encoder.go
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
package codec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
//编码器
|
||||||
|
type Encoder struct {
|
||||||
|
DefEncoder func(v interface{}) (data []byte, err error) //默认编码 扩展自定义编码方式
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Encoder) Encoder(v interface{}) (data []byte, err error) {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return StringToBytes(""), nil
|
||||||
|
case string:
|
||||||
|
return StringToBytes(v), nil
|
||||||
|
case []byte:
|
||||||
|
return v, nil
|
||||||
|
case int:
|
||||||
|
return IntToBytes(v), nil
|
||||||
|
case int8:
|
||||||
|
return Int8ToBytes(v), nil
|
||||||
|
case int16:
|
||||||
|
return Int16ToBytes(v), nil
|
||||||
|
case int32:
|
||||||
|
return Int32ToBytes(v), nil
|
||||||
|
case int64:
|
||||||
|
return Int64ToBytes(v), nil
|
||||||
|
case uint:
|
||||||
|
return UIntToBytes(v), nil
|
||||||
|
case uint8:
|
||||||
|
return UInt8ToBytes(v), nil
|
||||||
|
case uint16:
|
||||||
|
return UInt16ToBytes(v), nil
|
||||||
|
case uint32:
|
||||||
|
return UInt32ToBytes(v), nil
|
||||||
|
case uint64:
|
||||||
|
return UInt64ToBytes(v), nil
|
||||||
|
case float32:
|
||||||
|
return Float32ToBytes(v), nil
|
||||||
|
case float64:
|
||||||
|
return Float64ToBytes(v), nil
|
||||||
|
case bool:
|
||||||
|
return BoolToBytes(v), nil
|
||||||
|
case time.Time:
|
||||||
|
return v.AppendFormat([]byte{}, time.RFC3339Nano), nil
|
||||||
|
case time.Duration:
|
||||||
|
return Int64ToBytes(v.Nanoseconds()), nil
|
||||||
|
default:
|
||||||
|
if this.DefEncoder != nil {
|
||||||
|
return this.DefEncoder(v)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"encoder: can't marshal %T (implement encoder.DefEncoder)", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Encoder) EncoderToMap(v interface{}) (data map[string][]byte, 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][]byte)
|
||||||
|
if vof.Kind() == reflect.Map {
|
||||||
|
keys := vof.MapKeys()
|
||||||
|
for _, k := range keys {
|
||||||
|
value := vof.MapIndex(k)
|
||||||
|
var keydata []byte
|
||||||
|
var valuedata []byte
|
||||||
|
if keydata, err = this.Encoder(k.Interface()); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", value.Interface(), err)
|
||||||
|
}
|
||||||
|
if valuedata, err = this.Encoder(value.Interface()); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", value.Interface(), err)
|
||||||
|
}
|
||||||
|
data[BytesToString(keydata)] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else if vof.Kind() == reflect.Slice {
|
||||||
|
for i := 0; i < vof.Len(); i++ {
|
||||||
|
value := vof.Index(i).Interface()
|
||||||
|
var valuedata []byte
|
||||||
|
if valuedata, err = this.Encoder(value); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", value, err)
|
||||||
|
}
|
||||||
|
data[BytesToString(IntToBytes(i))] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else if vof.Kind() == reflect.Ptr {
|
||||||
|
elem := vof.Elem()
|
||||||
|
relType := elem.Type()
|
||||||
|
for i := 0; i < relType.NumField(); i++ {
|
||||||
|
field := elem.Field(i).Interface()
|
||||||
|
var valuedata []byte
|
||||||
|
if valuedata, err = this.Encoder(field); err != nil {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T) err:%v", field, err)
|
||||||
|
}
|
||||||
|
data[relType.Field(i).Name] = valuedata
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Encoder: EncoderToMap(invalid type %T)", v)
|
||||||
|
}
|
||||||
|
}
|
@ -5,9 +5,9 @@ import "google/protobuf/any.proto";
|
|||||||
|
|
||||||
//用户消息流结构
|
//用户消息流结构
|
||||||
message UserMessage {
|
message UserMessage {
|
||||||
string MainType =1;
|
string MainType =1; //用户消息处理 模块名 例如:user 对应项目中 user的模块
|
||||||
string SubType = 2;
|
string SubType = 2; //用户消息处理函数名 例如:login 对应项目中 user的模块中 api_login 的处理函数
|
||||||
ErrorCode Code = 3;
|
ErrorCode Code = 3; //统一消息
|
||||||
google.protobuf.Any data = 4;
|
google.protobuf.Any data = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user