-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
168 lines (148 loc) · 3.96 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package redis
import (
`encoding/json`
`encoding/xml`
`reflect`
`strconv`
`sync`
`time`
`github.com/go-redis/redis/v8`
`github.com/golang/protobuf/proto`
`github.com/vmihailenco/msgpack/v5`
)
// Client Redis客户端
type Client struct {
clientCache map[string]*redis.Client
optionsCache map[string]*redis.Options
serializerCache map[string]serializer
mutex sync.Mutex
}
func (c *Client) Redis(opts ...option) *redis.Client {
_options := defaultOptions()
for _, opt := range opts {
opt.apply(_options)
}
return c.getClient(_options)
}
func (c *Client) getClient(options *options) (client *redis.Client) {
c.mutex.Lock()
defer func() {
options.label = defaultLabel
c.mutex.Unlock()
}()
var exist bool
if client, exist = c.clientCache[options.label]; exist {
return
}
client = redis.NewClient(c.optionsCache[options.label])
c.clientCache[options.label] = client
return
}
func (c *Client) marshal(from interface{}, label string, serializer serializer) (to interface{}, err error) {
serializer = c.getSerializer(label, serializer)
switch serializer {
case serializerProto:
to, err = proto.Marshal(from.(proto.Message))
case serializerJson:
to, err = json.Marshal(from)
case serializerXml:
to, err = xml.Marshal(from)
case serializerMsgpack:
to, err = msgpack.Marshal(from)
case serializerBytes:
to = from.([]byte)
case serializerString:
to = from.(string)
case serializerInt:
to = from.(int)
case serializerInt64:
to = from.(int64)
case serializerUint64:
to = from.(uint64)
case serializerBool:
to = from.(bool)
case serializerFloat32:
to = from.(float32)
case serializerFloat64:
to = from.(float64)
case serializerTime:
to = from.(time.Time)
}
return
}
func (c *Client) unmarshal(from string, to interface{}, label string, serializer serializer) (err error) {
serializer = c.getSerializer(label, serializer)
switch serializer {
case serializerProto:
err = proto.Unmarshal(stringToBytes(from), to.(proto.Message))
case serializerJson:
err = json.Unmarshal(stringToBytes(from), to)
case serializerXml:
err = xml.Unmarshal(stringToBytes(from), to)
case serializerMsgpack:
err = msgpack.Unmarshal(stringToBytes(from), to)
case serializerBytes:
toSlice := to.(*[]byte)
*toSlice = stringToBytes(from)
case serializerString:
toString := to.(*string)
*toString = from
case serializerInt:
toInt := to.(*int)
*toInt, err = strconv.Atoi(from)
case serializerInt64:
toInt64 := to.(*int64)
*toInt64, err = strconv.ParseInt(from, 10, 64)
case serializerUint64:
toUint64 := to.(*uint64)
*toUint64, err = strconv.ParseUint(from, 10, 64)
case serializerBool:
toBool := to.(*bool)
*toBool, err = strconv.ParseBool(from)
case serializerFloat32:
toFloat32 := to.(*float32)
if value, float32Err := strconv.ParseFloat(from, 32); nil == err {
*toFloat32 = float32(value)
} else {
err = float32Err
}
case serializerFloat64:
toFloat64 := to.(*float64)
*toFloat64, err = strconv.ParseFloat(from, 64)
case serializerTime:
to, err = time.Parse(time.RFC3339Nano, from)
}
return
}
func (c *Client) unmarshalSlice(strings []string, to interface{}, label string, serializer serializer) (err error) {
sliceType := reflect.TypeOf(to).Elem()
elementType := sliceType.Elem()
newTo := reflect.MakeSlice(sliceType, 0, len(strings))
for _, str := range strings {
value := reflect.New(elementType).Interface()
if err = c.unmarshal(str, value, label, serializer); nil != err {
return
}
newTo = reflect.Append(newTo, reflect.ValueOf(value).Elem())
}
reflect.ValueOf(to).Elem().Set(newTo)
return
}
func (c *Client) checkNil(redisErr error) (exist bool, err error) {
if nil == redisErr {
exist = true
} else if redis.Nil == redisErr {
exist = false
} else {
err = redisErr
}
return
}
func (c *Client) getSerializer(label string, original serializer) (serializer serializer) {
if serializerUnknown == original {
serializer = c.serializerCache[label]
} else {
serializer = original
}
return
}